Browse Source

Convert db entities to kotlin

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
d4bf5a57c2
  1. 10
      app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt
  2. 7
      app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt
  3. 3
      app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt
  4. 124
      app/src/main/java/awais/instagrabber/db/entities/Account.java
  5. 32
      app/src/main/java/awais/instagrabber/db/entities/Account.kt
  6. 85
      app/src/main/java/awais/instagrabber/db/entities/DMLastNotified.java
  7. 23
      app/src/main/java/awais/instagrabber/db/entities/DMLastNotified.kt
  8. 110
      app/src/main/java/awais/instagrabber/db/entities/Favorite.java
  9. 27
      app/src/main/java/awais/instagrabber/db/entities/Favorite.kt
  10. 185
      app/src/main/java/awais/instagrabber/db/entities/RecentSearch.java
  11. 70
      app/src/main/java/awais/instagrabber/db/entities/RecentSearch.kt
  12. 2
      app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt
  13. 6
      app/src/main/java/awais/instagrabber/db/repositories/RecentSearchRepository.kt
  14. 2
      app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt

10
app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt

@ -11,13 +11,13 @@ class AccountDataSource private constructor(private val accountDao: AccountDao)
suspend fun getAllAccounts(): List<Account> = accountDao.getAllAccounts()
suspend fun insertOrUpdateAccount(
uid: String,
username: String,
cookie: String,
fullName: String,
uid: String?,
username: String?,
cookie: String?,
fullName: String?,
profilePicUrl: String?,
) {
val account = getAccount(uid)
val account = uid?.let { getAccount(it) }
val toUpdate = Account(account?.id ?: 0, uid, username, cookie, fullName, profilePicUrl)
if (account != null) {
accountDao.updateAccounts(toUpdate)

7
app/src/main/java/awais/instagrabber/db/datasources/DMLastNotifiedDataSource.kt

@ -12,10 +12,11 @@ class DMLastNotifiedDataSource private constructor(private val dmLastNotifiedDao
suspend fun getAllDMDmLastNotified(): List<DMLastNotified> = dmLastNotifiedDao.getAllDMDmLastNotified()
suspend fun insertOrUpdateDMLastNotified(
threadId: String,
lastNotifiedMsgTs: LocalDateTime,
lastNotifiedAt: LocalDateTime,
threadId: String?,
lastNotifiedMsgTs: LocalDateTime?,
lastNotifiedAt: LocalDateTime?,
) {
if (threadId == null) return
val dmLastNotified = getDMLastNotified(threadId)
val toUpdate = DMLastNotified(
dmLastNotified?.id ?: 0,

3
app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt

@ -19,7 +19,8 @@ class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDa
favoriteDao.insertFavorites(favorite)
}
suspend fun deleteFavorite(query: String, type: FavoriteType) {
suspend fun deleteFavorite(query: String?, type: FavoriteType?) {
if (query == null || type == null) return
val favorite = getFavorite(query, type) ?: return
favoriteDao.deleteFavorites(favorite)
}

124
app/src/main/java/awais/instagrabber/db/entities/Account.java

@ -1,124 +0,0 @@
package awais.instagrabber.db.entities;
import androidx.annotation.NonNull;
import androidx.core.util.ObjectsCompat;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.TextUtils;
@Entity(tableName = Account.TABLE_NAME)
public class Account {
public final static String TABLE_NAME = "accounts";
public final static String COL_ID = "id";
public final static String COL_USERNAME = Constants.EXTRAS_USERNAME;
public final static String COL_COOKIE = "cookie";
public final static String COL_UID = "uid";
public final static String COL_FULL_NAME = "full_name";
public final static String COL_PROFILE_PIC = "profile_pic";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COL_ID)
private final int id;
@ColumnInfo(name = COL_UID)
private final String uid;
@ColumnInfo(name = COL_USERNAME)
private final String username;
@ColumnInfo(name = COL_COOKIE)
private final String cookie;
@ColumnInfo(name = COL_FULL_NAME)
private final String fullName;
@ColumnInfo(name = COL_PROFILE_PIC)
private final String profilePic;
@Ignore
private boolean selected;
public Account(final int id,
final String uid,
final String username,
final String cookie,
final String fullName,
final String profilePic) {
this.id = id;
this.uid = uid;
this.username = username;
this.cookie = cookie;
this.fullName = fullName;
this.profilePic = profilePic;
}
public int getId() {
return id;
}
public String getUid() {
return uid;
}
public String getUsername() {
return username;
}
public String getCookie() {
return cookie;
}
public String getFullName() {
return fullName;
}
public String getProfilePic() {
return profilePic;
}
public boolean isSelected() {
return selected;
}
public void setSelected(final boolean selected) {
this.selected = selected;
}
public boolean isValid() {
return !TextUtils.isEmpty(uid)
&& !TextUtils.isEmpty(username)
&& !TextUtils.isEmpty(cookie);
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Account that = (Account) o;
return ObjectsCompat.equals(uid, that.uid) &&
ObjectsCompat.equals(username, that.username) &&
ObjectsCompat.equals(cookie, that.cookie);
}
@Override
public int hashCode() {
return ObjectsCompat.hash(uid, username, cookie);
}
@NonNull
@Override
public String toString() {
return "Account{" +
"uid='" + uid + '\'' +
", username='" + username + '\'' +
", cookie='" + cookie + '\'' +
", fullName='" + fullName + '\'' +
", profilePic='" + profilePic + '\'' +
", selected=" + selected +
'}';
}
}

32
app/src/main/java/awais/instagrabber/db/entities/Account.kt

@ -0,0 +1,32 @@
package awais.instagrabber.db.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
@Entity(tableName = Account.TABLE_NAME)
data class Account(
@ColumnInfo(name = COL_ID) @PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = COL_UID) val uid: String?,
@ColumnInfo(name = COL_USERNAME) val username: String?,
@ColumnInfo(name = COL_COOKIE) val cookie: String?,
@ColumnInfo(name = COL_FULL_NAME) val fullName: String?,
@ColumnInfo(name = COL_PROFILE_PIC) val profilePic: String?,
) {
@Ignore
var isSelected = false
val isValid: Boolean
get() = !uid.isNullOrBlank() && !username.isNullOrBlank() && !cookie.isNullOrBlank()
companion object {
const val TABLE_NAME = "accounts"
const val COL_ID = "id"
const val COL_USERNAME = "username"
const val COL_COOKIE = "cookie"
const val COL_UID = "uid"
const val COL_FULL_NAME = "full_name"
const val COL_PROFILE_PIC = "profile_pic"
}
}

85
app/src/main/java/awais/instagrabber/db/entities/DMLastNotified.java

@ -1,85 +0,0 @@
package awais.instagrabber.db.entities;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import java.time.LocalDateTime;
import java.util.Objects;
@Entity(tableName = DMLastNotified.TABLE_NAME, indices = {@Index(value = DMLastNotified.COL_THREAD_ID, unique = true)})
public class DMLastNotified {
public final static String TABLE_NAME = "dm_last_notified";
public final static String COL_ID = "id";
public final static String COL_THREAD_ID = "thread_id";
public final static String COL_LAST_NOTIFIED_MSG_TS = "last_notified_msg_ts";
public final static String COL_LAST_NOTIFIED_AT = "last_notified_at";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COL_ID)
private final int id;
@ColumnInfo(name = COL_THREAD_ID)
private final String threadId;
@ColumnInfo(name = COL_LAST_NOTIFIED_MSG_TS)
private final LocalDateTime lastNotifiedMsgTs;
@ColumnInfo(name = COL_LAST_NOTIFIED_AT)
private final LocalDateTime lastNotifiedAt;
public DMLastNotified(final int id,
final String threadId,
final LocalDateTime lastNotifiedMsgTs,
final LocalDateTime lastNotifiedAt) {
this.id = id;
this.threadId = threadId;
this.lastNotifiedMsgTs = lastNotifiedMsgTs;
this.lastNotifiedAt = lastNotifiedAt;
}
public int getId() {
return id;
}
public String getThreadId() {
return threadId;
}
public LocalDateTime getLastNotifiedMsgTs() {
return lastNotifiedMsgTs;
}
public LocalDateTime getLastNotifiedAt() {
return lastNotifiedAt;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final DMLastNotified that = (DMLastNotified) o;
return id == that.id &&
Objects.equals(threadId, that.threadId) &&
Objects.equals(lastNotifiedMsgTs, that.lastNotifiedMsgTs) &&
Objects.equals(lastNotifiedAt, that.lastNotifiedAt);
}
@Override
public int hashCode() {
return Objects.hash(id, threadId, lastNotifiedMsgTs, lastNotifiedAt);
}
@NonNull
@Override
public String toString() {
return "DMLastNotified{" +
"id=" + id +
", threadId='" + threadId + '\'' +
", lastNotifiedMsgTs='" + lastNotifiedMsgTs + '\'' +
", lastNotifiedAt='" + lastNotifiedAt + '\'' +
'}';
}
}

23
app/src/main/java/awais/instagrabber/db/entities/DMLastNotified.kt

@ -0,0 +1,23 @@
package awais.instagrabber.db.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import java.time.LocalDateTime
@Entity(tableName = DMLastNotified.TABLE_NAME, indices = [Index(value = [DMLastNotified.COL_THREAD_ID], unique = true)])
data class DMLastNotified(
@ColumnInfo(name = COL_ID) @PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = COL_THREAD_ID) val threadId: String?,
@ColumnInfo(name = COL_LAST_NOTIFIED_MSG_TS) val lastNotifiedMsgTs: LocalDateTime?,
@ColumnInfo(name = COL_LAST_NOTIFIED_AT) val lastNotifiedAt: LocalDateTime?,
) {
companion object {
const val TABLE_NAME = "dm_last_notified"
const val COL_ID = "id"
const val COL_THREAD_ID = "thread_id"
const val COL_LAST_NOTIFIED_MSG_TS = "last_notified_msg_ts"
const val COL_LAST_NOTIFIED_AT = "last_notified_at"
}
}

110
app/src/main/java/awais/instagrabber/db/entities/Favorite.java

@ -1,110 +0,0 @@
package awais.instagrabber.db.entities;
import androidx.annotation.NonNull;
import androidx.core.util.ObjectsCompat;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.time.LocalDateTime;
import awais.instagrabber.models.enums.FavoriteType;
@Entity(tableName = Favorite.TABLE_NAME)
public class Favorite {
public final static String TABLE_NAME = "favorites";
public final static String COL_ID = "id";
public final static String COL_QUERY = "query_text";
public final static String COL_TYPE = "type";
public final static String COL_DISPLAY_NAME = "display_name";
public final static String COL_PIC_URL = "pic_url";
public final static String COL_DATE_ADDED = "date_added";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COL_ID)
private final int id;
@ColumnInfo(name = COL_QUERY)
private final String query;
@ColumnInfo(name = COL_TYPE)
private final FavoriteType type;
@ColumnInfo(name = COL_DISPLAY_NAME)
private final String displayName;
@ColumnInfo(name = COL_PIC_URL)
private final String picUrl;
@ColumnInfo(name = COL_DATE_ADDED)
private final LocalDateTime dateAdded;
public Favorite(final int id,
final String query,
final FavoriteType type,
final String displayName,
final String picUrl,
final LocalDateTime dateAdded) {
this.id = id;
this.query = query;
this.type = type;
this.displayName = displayName;
this.picUrl = picUrl;
this.dateAdded = dateAdded;
}
public int getId() {
return id;
}
public String getQuery() {
return query;
}
public FavoriteType getType() {
return type;
}
public String getDisplayName() {
return displayName;
}
public String getPicUrl() {
return picUrl;
}
public LocalDateTime getDateAdded() {
return dateAdded;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Favorite that = (Favorite) o;
return id == that.id &&
ObjectsCompat.equals(query, that.query) &&
type == that.type &&
ObjectsCompat.equals(displayName, that.displayName) &&
ObjectsCompat.equals(picUrl, that.picUrl) &&
ObjectsCompat.equals(dateAdded, that.dateAdded);
}
@Override
public int hashCode() {
return ObjectsCompat.hash(id, query, type, displayName, picUrl, dateAdded);
}
@NonNull
@Override
public String toString() {
return "FavoriteModel{" +
"id=" + id +
", query='" + query + '\'' +
", type=" + type +
", displayName='" + displayName + '\'' +
", picUrl='" + picUrl + '\'' +
", dateAdded=" + dateAdded +
'}';
}
}

27
app/src/main/java/awais/instagrabber/db/entities/Favorite.kt

@ -0,0 +1,27 @@
package awais.instagrabber.db.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import awais.instagrabber.models.enums.FavoriteType
import java.time.LocalDateTime
@Entity(tableName = Favorite.TABLE_NAME)
data class Favorite(
@ColumnInfo(name = COL_ID) @PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = COL_QUERY) val query: String?,
@ColumnInfo(name = COL_TYPE) val type: FavoriteType?,
@ColumnInfo(name = COL_DISPLAY_NAME) val displayName: String?,
@ColumnInfo(name = COL_PIC_URL) val picUrl: String?,
@ColumnInfo(name = COL_DATE_ADDED) val dateAdded: LocalDateTime?,
) {
companion object {
const val TABLE_NAME = "favorites"
const val COL_ID = "id"
const val COL_QUERY = "query_text"
const val COL_TYPE = "type"
const val COL_DISPLAY_NAME = "display_name"
const val COL_PIC_URL = "pic_url"
const val COL_DATE_ADDED = "date_added"
}
}

185
app/src/main/java/awais/instagrabber/db/entities/RecentSearch.java

@ -1,185 +0,0 @@
package awais.instagrabber.db.entities;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import java.time.LocalDateTime;
import java.util.Objects;
import awais.instagrabber.models.enums.FavoriteType;
import awais.instagrabber.repositories.responses.search.SearchItem;
@Entity(tableName = RecentSearch.TABLE_NAME, indices = {@Index(value = {RecentSearch.COL_IG_ID, RecentSearch.COL_TYPE}, unique = true)})
public class RecentSearch {
private static final String TAG = RecentSearch.class.getSimpleName();
public static final String TABLE_NAME = "recent_searches";
private static final String COL_ID = "id";
public static final String COL_IG_ID = "ig_id";
private static final String COL_NAME = "name";
private static final String COL_USERNAME = "username";
private static final String COL_PIC_URL = "pic_url";
public static final String COL_TYPE = "type";
private static final String COL_LAST_SEARCHED_ON = "last_searched_on";
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COL_ID)
private final int id;
@ColumnInfo(name = COL_IG_ID)
@NonNull
private final String igId;
@ColumnInfo(name = COL_NAME)
@NonNull
private final String name;
@ColumnInfo(name = COL_USERNAME)
private final String username;
@ColumnInfo(name = COL_PIC_URL)
private final String picUrl;
@ColumnInfo(name = COL_TYPE)
@NonNull
private final FavoriteType type;
@ColumnInfo(name = COL_LAST_SEARCHED_ON)
@NonNull
private final LocalDateTime lastSearchedOn;
@Ignore
public RecentSearch(final String igId,
final String name,
final String username,
final String picUrl,
final FavoriteType type,
final LocalDateTime lastSearchedOn) {
this(0, igId, name, username, picUrl, type, lastSearchedOn);
}
public RecentSearch(final int id,
@NonNull final String igId,
@NonNull final String name,
final String username,
final String picUrl,
@NonNull final FavoriteType type,
@NonNull final LocalDateTime lastSearchedOn) {
this.id = id;
this.igId = igId;
this.name = name;
this.username = username;
this.picUrl = picUrl;
this.type = type;
this.lastSearchedOn = lastSearchedOn;
}
public int getId() {
return id;
}
@NonNull
public String getIgId() {
return igId;
}
@NonNull
public String getName() {
return name;
}
public String getUsername() {
return username;
}
public String getPicUrl() {
return picUrl;
}
@NonNull
public FavoriteType getType() {
return type;
}
@NonNull
public LocalDateTime getLastSearchedOn() {
return lastSearchedOn;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final RecentSearch that = (RecentSearch) o;
return Objects.equals(igId, that.igId) &&
Objects.equals(name, that.name) &&
Objects.equals(username, that.username) &&
Objects.equals(picUrl, that.picUrl) &&
type == that.type &&
Objects.equals(lastSearchedOn, that.lastSearchedOn);
}
@Override
public int hashCode() {
return Objects.hash(igId, name, username, picUrl, type, lastSearchedOn);
}
@NonNull
@Override
public String toString() {
return "RecentSearch{" +
"id=" + id +
", igId='" + igId + '\'' +
", name='" + name + '\'' +
", username='" + username + '\'' +
", picUrl='" + picUrl + '\'' +
", type=" + type +
", lastSearchedOn=" + lastSearchedOn +
'}';
}
@Nullable
public static RecentSearch fromSearchItem(@NonNull final SearchItem searchItem) {
final FavoriteType type = searchItem.getType();
if (type == null) return null;
try {
final String igId;
final String name;
final String username;
final String picUrl;
switch (type) {
case USER:
igId = String.valueOf(searchItem.getUser().getPk());
name = searchItem.getUser().getFullName();
username = searchItem.getUser().getUsername();
picUrl = searchItem.getUser().getProfilePicUrl();
break;
case HASHTAG:
igId = searchItem.getHashtag().getId();
name = searchItem.getHashtag().getName();
username = null;
picUrl = null;
break;
case LOCATION:
igId = String.valueOf(searchItem.getPlace().getLocation().getPk());
name = searchItem.getPlace().getTitle();
username = null;
picUrl = null;
break;
default:
return null;
}
return new RecentSearch(igId, name, username, picUrl, type, LocalDateTime.now());
} catch (Exception e) {
Log.e(TAG, "fromSearchItem: ", e);
}
return null;
}
}

70
app/src/main/java/awais/instagrabber/db/entities/RecentSearch.kt

@ -0,0 +1,70 @@
package awais.instagrabber.db.entities
import android.util.Log
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import awais.instagrabber.models.enums.FavoriteType
import awais.instagrabber.repositories.responses.search.SearchItem
import awais.instagrabber.utils.extensions.TAG
import java.time.LocalDateTime
@Entity(tableName = RecentSearch.TABLE_NAME, indices = [Index(value = [RecentSearch.COL_IG_ID, RecentSearch.COL_TYPE], unique = true)])
data class RecentSearch(
@ColumnInfo(name = COL_ID) @PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name = COL_IG_ID) val igId: String,
@ColumnInfo(name = COL_NAME) val name: String,
@ColumnInfo(name = COL_USERNAME) val username: String?,
@ColumnInfo(name = COL_PIC_URL) val picUrl: String?,
@ColumnInfo(name = COL_TYPE) val type: FavoriteType,
@ColumnInfo(name = COL_LAST_SEARCHED_ON) val lastSearchedOn: LocalDateTime,
) {
companion object {
const val TABLE_NAME = "recent_searches"
private const val COL_ID = "id"
const val COL_IG_ID = "ig_id"
private const val COL_NAME = "name"
private const val COL_USERNAME = "username"
private const val COL_PIC_URL = "pic_url"
const val COL_TYPE = "type"
private const val COL_LAST_SEARCHED_ON = "last_searched_on"
@JvmStatic
fun fromSearchItem(searchItem: SearchItem): RecentSearch? {
val type = searchItem.type ?: return null
try {
val igId: String
val name: String
val username: String?
val picUrl: String?
when (type) {
FavoriteType.USER -> {
igId = searchItem.user.pk.toString()
name = searchItem.user.fullName ?: ""
username = searchItem.user.username
picUrl = searchItem.user.profilePicUrl
}
FavoriteType.HASHTAG -> {
igId = searchItem.hashtag.id
name = searchItem.hashtag.name
username = null
picUrl = null
}
FavoriteType.LOCATION -> {
igId = searchItem.place.location.pk.toString()
name = searchItem.place.title
username = null
picUrl = null
}
else -> return null
}
return RecentSearch(id = 0, igId, name, username, picUrl, type, LocalDateTime.now())
} catch (e: Exception) {
Log.e(TAG, "fromSearchItem: ", e)
}
return null
}
}
}

2
app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt

@ -12,7 +12,7 @@ class FavoriteRepository private constructor(private val favoriteDataSource: Fav
suspend fun insertOrUpdateFavorite(favorite: Favorite) = favoriteDataSource.insertOrUpdateFavorite(favorite)
suspend fun deleteFavorite(query: String, type: FavoriteType) = favoriteDataSource.deleteFavorite(query, type)
suspend fun deleteFavorite(query: String?, type: FavoriteType?) = favoriteDataSource.deleteFavorite(query, type)
companion object {
private lateinit var instance: FavoriteRepository

6
app/src/main/java/awais/instagrabber/db/repositories/RecentSearchRepository.kt

@ -21,11 +21,7 @@ class RecentSearchRepository private constructor(private val recentSearchDataSou
type: FavoriteType,
) {
var recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type)
recentSearch = if (recentSearch == null) {
RecentSearch(igId, name, username, picUrl, type, LocalDateTime.now())
} else {
RecentSearch(recentSearch.id, igId, name, username, picUrl, type, LocalDateTime.now())
}
recentSearch = RecentSearch(recentSearch?.id ?: 0, igId, name, username, picUrl, type, LocalDateTime.now())
recentSearchDataSource.insertOrUpdateRecentSearch(recentSearch)
}

2
app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt

@ -65,7 +65,7 @@ class FavoritesFragment : Fragment() {
navController.navigate(R.id.action_global_profileFragment, bundle)
}
FavoriteType.LOCATION -> {
val locationId = model.query
val locationId = model.query ?: return@FavoritesAdapter
// Log.d(TAG, "locationId: " + locationId);
val navController = NavHostFragment.findNavController(this)
val bundle = Bundle()

Loading…
Cancel
Save