mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 14:47:29 +00:00
Convert FavoriteDao, FavoriteDataSource and FavoriteRepository to kotlin
This commit is contained in:
parent
c647e22293
commit
76b246573d
@ -12,7 +12,7 @@ interface AccountDao {
|
|||||||
suspend fun findAccountByUid(uid: String): Account?
|
suspend fun findAccountByUid(uid: String): Account?
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun insertAccounts(vararg accounts: Account): List<Long>
|
suspend fun insertAccounts(vararg accounts: Account)
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
suspend fun updateAccounts(vararg accounts: Account)
|
suspend fun updateAccounts(vararg accounts: Account)
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
package awais.instagrabber.db.dao;
|
|
||||||
|
|
||||||
import androidx.room.Dao;
|
|
||||||
import androidx.room.Delete;
|
|
||||||
import androidx.room.Insert;
|
|
||||||
import androidx.room.OnConflictStrategy;
|
|
||||||
import androidx.room.Query;
|
|
||||||
import androidx.room.Update;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import awais.instagrabber.db.entities.Favorite;
|
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
public interface FavoriteDao {
|
|
||||||
|
|
||||||
@Query("SELECT * FROM favorites")
|
|
||||||
List<Favorite> getAllFavorites();
|
|
||||||
|
|
||||||
@Query("SELECT * FROM favorites WHERE query_text = :query and type = :type")
|
|
||||||
Favorite findFavoriteByQueryAndType(String query, FavoriteType type);
|
|
||||||
|
|
||||||
@Insert
|
|
||||||
List<Long> insertFavorites(Favorite... favorites);
|
|
||||||
|
|
||||||
@Update
|
|
||||||
void updateFavorites(Favorite... favorites);
|
|
||||||
|
|
||||||
@Delete
|
|
||||||
void deleteFavorites(Favorite... favorites);
|
|
||||||
|
|
||||||
@Query("DELETE from favorites")
|
|
||||||
void deleteAllFavorites();
|
|
||||||
}
|
|
26
app/src/main/java/awais/instagrabber/db/dao/FavoriteDao.kt
Normal file
26
app/src/main/java/awais/instagrabber/db/dao/FavoriteDao.kt
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package awais.instagrabber.db.dao
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import awais.instagrabber.db.entities.Favorite
|
||||||
|
import awais.instagrabber.models.enums.FavoriteType
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface FavoriteDao {
|
||||||
|
@Query("SELECT * FROM favorites")
|
||||||
|
suspend fun getAllFavorites(): List<Favorite>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM favorites WHERE query_text = :query and type = :type")
|
||||||
|
suspend fun findFavoriteByQueryAndType(query: String, type: FavoriteType): Favorite?
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insertFavorites(vararg favorites: Favorite)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun updateFavorites(vararg favorites: Favorite)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun deleteFavorites(vararg favorites: Favorite)
|
||||||
|
|
||||||
|
@Query("DELETE from favorites")
|
||||||
|
suspend fun deleteAllFavorites()
|
||||||
|
}
|
@ -1,62 +0,0 @@
|
|||||||
package awais.instagrabber.db.datasources;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import awais.instagrabber.db.AppDatabase;
|
|
||||||
import awais.instagrabber.db.dao.FavoriteDao;
|
|
||||||
import awais.instagrabber.db.entities.Favorite;
|
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
|
||||||
|
|
||||||
public class FavoriteDataSource {
|
|
||||||
private static final String TAG = FavoriteDataSource.class.getSimpleName();
|
|
||||||
|
|
||||||
private static FavoriteDataSource INSTANCE;
|
|
||||||
|
|
||||||
private final FavoriteDao favoriteDao;
|
|
||||||
|
|
||||||
private FavoriteDataSource(final FavoriteDao favoriteDao) {
|
|
||||||
this.favoriteDao = favoriteDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static synchronized FavoriteDataSource getInstance(@NonNull Context context) {
|
|
||||||
if (INSTANCE == null) {
|
|
||||||
synchronized (FavoriteDataSource.class) {
|
|
||||||
if (INSTANCE == null) {
|
|
||||||
final AppDatabase database = AppDatabase.getDatabase(context);
|
|
||||||
INSTANCE = new FavoriteDataSource(database.favoriteDao());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public final Favorite getFavorite(@NonNull final String query, @NonNull final FavoriteType type) {
|
|
||||||
return favoriteDao.findFavoriteByQueryAndType(query, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public final List<Favorite> getAllFavorites() {
|
|
||||||
return favoriteDao.getAllFavorites();
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void insertOrUpdateFavorite(@NonNull final Favorite favorite) {
|
|
||||||
if (favorite.getId() != 0) {
|
|
||||||
favoriteDao.updateFavorites(favorite);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
favoriteDao.insertFavorites(favorite);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void deleteFavorite(@NonNull final String query, @NonNull final FavoriteType type) {
|
|
||||||
final Favorite favorite = getFavorite(query, type);
|
|
||||||
if (favorite == null) return;
|
|
||||||
favoriteDao.deleteFavorites(favorite);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,44 @@
|
|||||||
|
package awais.instagrabber.db.datasources
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import awais.instagrabber.db.AppDatabase
|
||||||
|
import awais.instagrabber.db.dao.FavoriteDao
|
||||||
|
import awais.instagrabber.db.entities.Favorite
|
||||||
|
import awais.instagrabber.models.enums.FavoriteType
|
||||||
|
|
||||||
|
class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDao) {
|
||||||
|
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDao.findFavoriteByQueryAndType(query, type)
|
||||||
|
|
||||||
|
suspend fun getAllFavorites(): List<Favorite> = favoriteDao.getAllFavorites()
|
||||||
|
|
||||||
|
suspend fun insertOrUpdateFavorite(favorite: Favorite) {
|
||||||
|
if (favorite.id != 0) {
|
||||||
|
favoriteDao.updateFavorites(favorite)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
favoriteDao.insertFavorites(favorite)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun deleteFavorite(query: String, type: FavoriteType) {
|
||||||
|
val favorite = getFavorite(query, type) ?: return
|
||||||
|
favoriteDao.deleteFavorites(favorite)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private lateinit var INSTANCE: FavoriteDataSource
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@Synchronized
|
||||||
|
fun getInstance(context: Context): FavoriteDataSource {
|
||||||
|
if (!this::INSTANCE.isInitialized) {
|
||||||
|
synchronized(FavoriteDataSource::class.java) {
|
||||||
|
if (!this::INSTANCE.isInitialized) {
|
||||||
|
val database = AppDatabase.getDatabase(context)
|
||||||
|
INSTANCE = FavoriteDataSource(database.favoriteDao())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INSTANCE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,88 +0,0 @@
|
|||||||
package awais.instagrabber.db.repositories;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import awais.instagrabber.db.datasources.FavoriteDataSource;
|
|
||||||
import awais.instagrabber.db.entities.Favorite;
|
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
|
||||||
import awais.instagrabber.utils.AppExecutors;
|
|
||||||
|
|
||||||
public class FavoriteRepository {
|
|
||||||
private static final String TAG = FavoriteRepository.class.getSimpleName();
|
|
||||||
|
|
||||||
private static FavoriteRepository instance;
|
|
||||||
|
|
||||||
private final AppExecutors appExecutors;
|
|
||||||
private final FavoriteDataSource favoriteDataSource;
|
|
||||||
|
|
||||||
private FavoriteRepository(final AppExecutors appExecutors, final FavoriteDataSource favoriteDataSource) {
|
|
||||||
this.appExecutors = appExecutors;
|
|
||||||
this.favoriteDataSource = favoriteDataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static FavoriteRepository getInstance(final FavoriteDataSource favoriteDataSource) {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new FavoriteRepository(AppExecutors.INSTANCE, favoriteDataSource);
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void getFavorite(final String query, final FavoriteType type, final RepositoryCallback<Favorite> callback) {
|
|
||||||
// request on the I/O thread
|
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
|
||||||
final Favorite favorite = favoriteDataSource.getFavorite(query, type);
|
|
||||||
// notify on the main thread
|
|
||||||
appExecutors.getMainThread().execute(() -> {
|
|
||||||
if (callback == null) return;
|
|
||||||
if (favorite == null) {
|
|
||||||
callback.onDataNotAvailable();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
callback.onSuccess(favorite);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void getAllFavorites(final RepositoryCallback<List<Favorite>> callback) {
|
|
||||||
// request on the I/O thread
|
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
|
||||||
final List<Favorite> favorites = favoriteDataSource.getAllFavorites();
|
|
||||||
// notify on the main thread
|
|
||||||
appExecutors.getMainThread().execute(() -> {
|
|
||||||
if (callback == null) return;
|
|
||||||
if (favorites == null) {
|
|
||||||
callback.onDataNotAvailable();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
callback.onSuccess(favorites);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void insertOrUpdateFavorite(final Favorite favorite,
|
|
||||||
final RepositoryCallback<Void> callback) {
|
|
||||||
// request on the I/O thread
|
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
|
||||||
favoriteDataSource.insertOrUpdateFavorite(favorite);
|
|
||||||
// notify on the main thread
|
|
||||||
appExecutors.getMainThread().execute(() -> {
|
|
||||||
if (callback == null) return;
|
|
||||||
callback.onSuccess(null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteFavorite(final String query,
|
|
||||||
final FavoriteType type,
|
|
||||||
final RepositoryCallback<Void> callback) {
|
|
||||||
// request on the I/O thread
|
|
||||||
appExecutors.getDiskIO().execute(() -> {
|
|
||||||
favoriteDataSource.deleteFavorite(query, type);
|
|
||||||
// notify on the main thread
|
|
||||||
appExecutors.getMainThread().execute(() -> {
|
|
||||||
if (callback == null) return;
|
|
||||||
callback.onSuccess(null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package awais.instagrabber.db.repositories
|
||||||
|
|
||||||
|
import awais.instagrabber.db.datasources.FavoriteDataSource
|
||||||
|
import awais.instagrabber.db.entities.Favorite
|
||||||
|
import awais.instagrabber.models.enums.FavoriteType
|
||||||
|
|
||||||
|
class FavoriteRepository private constructor(private val favoriteDataSource: FavoriteDataSource) {
|
||||||
|
|
||||||
|
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDataSource.getFavorite(query, type)
|
||||||
|
|
||||||
|
suspend fun getAllFavorites(): List<Favorite> = favoriteDataSource.getAllFavorites()
|
||||||
|
|
||||||
|
suspend fun insertOrUpdateFavorite(favorite: Favorite) = favoriteDataSource.insertOrUpdateFavorite(favorite)
|
||||||
|
|
||||||
|
suspend fun deleteFavorite(query: String, type: FavoriteType) = favoriteDataSource.deleteFavorite(query, type)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private lateinit var instance: FavoriteRepository
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun getInstance(favoriteDataSource: FavoriteDataSource): FavoriteRepository {
|
||||||
|
if (!this::instance.isInitialized) {
|
||||||
|
instance = FavoriteRepository(favoriteDataSource)
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -51,7 +51,6 @@ import awais.instagrabber.databinding.LayoutHashtagDetailsBinding;
|
|||||||
import awais.instagrabber.db.datasources.FavoriteDataSource;
|
import awais.instagrabber.db.datasources.FavoriteDataSource;
|
||||||
import awais.instagrabber.db.entities.Favorite;
|
import awais.instagrabber.db.entities.Favorite;
|
||||||
import awais.instagrabber.db.repositories.FavoriteRepository;
|
import awais.instagrabber.db.repositories.FavoriteRepository;
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback;
|
|
||||||
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
||||||
import awais.instagrabber.models.PostsLayoutPreferences;
|
import awais.instagrabber.models.PostsLayoutPreferences;
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
import awais.instagrabber.models.enums.FavoriteType;
|
||||||
@ -480,73 +479,81 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
if (context == null) return;
|
if (context == null) return;
|
||||||
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
||||||
favoriteRepository.getFavorite(hashtag, FavoriteType.HASHTAG, new RepositoryCallback<Favorite>() {
|
favoriteRepository.getFavorite(
|
||||||
@Override
|
hashtag,
|
||||||
public void onSuccess(final Favorite result) {
|
FavoriteType.HASHTAG,
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
result.getId(),
|
if (throwable != null || favorite == null) {
|
||||||
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
|
new Favorite(
|
||||||
|
favorite.getId(),
|
||||||
hashtag,
|
hashtag,
|
||||||
FavoriteType.HASHTAG,
|
FavoriteType.HASHTAG,
|
||||||
hashtagModel.getName(),
|
hashtagModel.getName(),
|
||||||
"res:/" + R.drawable.ic_hashtag,
|
"res:/" + R.drawable.ic_hashtag,
|
||||||
result.getDateAdded()
|
favorite.getDateAdded()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
hashtagDetailsBinding.favChip.setOnClickListener(v -> favoriteRepository.getFavorite(
|
||||||
|
hashtag,
|
||||||
|
FavoriteType.HASHTAG,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "setHashtagDetails: ", throwable);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
if (favorite == null) {
|
||||||
@Override
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
public void onDataNotAvailable() {}
|
new Favorite(
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
hashtagDetailsBinding.favChip.setOnClickListener(
|
|
||||||
v -> favoriteRepository.getFavorite(hashtag, FavoriteType.HASHTAG, new RepositoryCallback<Favorite>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Favorite result) {
|
|
||||||
favoriteRepository.deleteFavorite(hashtag, FavoriteType.HASHTAG, new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
|
||||||
0,
|
0,
|
||||||
hashtag,
|
hashtag,
|
||||||
FavoriteType.HASHTAG,
|
FavoriteType.HASHTAG,
|
||||||
hashtagModel.getName(),
|
hashtagModel.getName(),
|
||||||
"res:/" + R.drawable.ic_hashtag,
|
"res:/" + R.drawable.ic_hashtag,
|
||||||
LocalDateTime.now()
|
LocalDateTime.now()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
@Override
|
hashtag,
|
||||||
public void onDataNotAvailable() {}
|
FavoriteType.HASHTAG,
|
||||||
});
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}));
|
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
)
|
||||||
|
);
|
||||||
hashtagDetailsBinding.mainHashtagImage.setImageURI("res:/" + R.drawable.ic_hashtag);
|
hashtagDetailsBinding.mainHashtagImage.setImageURI("res:/" + R.drawable.ic_hashtag);
|
||||||
final String postCount = String.valueOf(hashtagModel.getMediaCount());
|
final String postCount = String.valueOf(hashtagModel.getMediaCount());
|
||||||
final SpannableStringBuilder span = new SpannableStringBuilder(getResources().getQuantityString(R.plurals.main_posts_count_inline,
|
final SpannableStringBuilder span = new SpannableStringBuilder(getResources().getQuantityString(R.plurals.main_posts_count_inline,
|
||||||
|
@ -49,7 +49,6 @@ import awais.instagrabber.databinding.LayoutLocationDetailsBinding;
|
|||||||
import awais.instagrabber.db.datasources.FavoriteDataSource;
|
import awais.instagrabber.db.datasources.FavoriteDataSource;
|
||||||
import awais.instagrabber.db.entities.Favorite;
|
import awais.instagrabber.db.entities.Favorite;
|
||||||
import awais.instagrabber.db.repositories.FavoriteRepository;
|
import awais.instagrabber.db.repositories.FavoriteRepository;
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback;
|
|
||||||
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
||||||
import awais.instagrabber.models.PostsLayoutPreferences;
|
import awais.instagrabber.models.PostsLayoutPreferences;
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
import awais.instagrabber.models.enums.FavoriteType;
|
||||||
@ -493,75 +492,82 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR
|
|||||||
final FavoriteDataSource dataSource = FavoriteDataSource.getInstance(context);
|
final FavoriteDataSource dataSource = FavoriteDataSource.getInstance(context);
|
||||||
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(dataSource);
|
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(dataSource);
|
||||||
locationDetailsBinding.favChip.setVisibility(View.VISIBLE);
|
locationDetailsBinding.favChip.setVisibility(View.VISIBLE);
|
||||||
favoriteRepository.getFavorite(String.valueOf(locationId), FavoriteType.LOCATION, new RepositoryCallback<Favorite>() {
|
favoriteRepository.getFavorite(
|
||||||
@Override
|
String.valueOf(locationId),
|
||||||
public void onSuccess(final Favorite result) {
|
FavoriteType.LOCATION,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null || favorite == null) {
|
||||||
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
|
Log.e(TAG, "setupLocationDetails: ", throwable);
|
||||||
|
return;
|
||||||
|
}
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
result.getId(),
|
new Favorite(
|
||||||
|
favorite.getId(),
|
||||||
String.valueOf(locationId),
|
String.valueOf(locationId),
|
||||||
FavoriteType.LOCATION,
|
FavoriteType.LOCATION,
|
||||||
locationModel.getName(),
|
locationModel.getName(),
|
||||||
"res:/" + R.drawable.ic_location,
|
"res:/" + R.drawable.ic_location,
|
||||||
result.getDateAdded()
|
favorite.getDateAdded()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {}
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}), Dispatchers.getIO())
|
||||||
@Override
|
);
|
||||||
public void onDataNotAvailable() {
|
}), Dispatchers.getIO())
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
);
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
locationDetailsBinding.favChip.setOnClickListener(v -> favoriteRepository.getFavorite(
|
||||||
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
String.valueOf(locationId),
|
||||||
|
FavoriteType.LOCATION,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "setupLocationDetails: ", throwable);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
if (favorite == null) {
|
||||||
locationDetailsBinding.favChip.setOnClickListener(v -> {
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
favoriteRepository.getFavorite(String.valueOf(locationId), FavoriteType.LOCATION, new RepositoryCallback<Favorite>() {
|
new Favorite(
|
||||||
@Override
|
|
||||||
public void onSuccess(final Favorite result) {
|
|
||||||
favoriteRepository.deleteFavorite(String.valueOf(locationId), FavoriteType.LOCATION, new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
|
||||||
0,
|
0,
|
||||||
String.valueOf(locationId),
|
String.valueOf(locationId),
|
||||||
FavoriteType.LOCATION,
|
FavoriteType.LOCATION,
|
||||||
locationModel.getName(),
|
locationModel.getName(),
|
||||||
"res:/" + R.drawable.ic_location,
|
"res:/" + R.drawable.ic_location,
|
||||||
LocalDateTime.now()
|
LocalDateTime.now()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
@Override
|
String.valueOf(locationId),
|
||||||
public void onDataNotAvailable() {}
|
FavoriteType.LOCATION,
|
||||||
});
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
});
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
));
|
||||||
locationDetailsBinding.mainLocationImage.setOnClickListener(v -> {
|
locationDetailsBinding.mainLocationImage.setOnClickListener(v -> {
|
||||||
if (hasStories) {
|
if (hasStories) {
|
||||||
// show stories
|
// show stories
|
||||||
|
@ -65,7 +65,6 @@ import awais.instagrabber.db.datasources.FavoriteDataSource;
|
|||||||
import awais.instagrabber.db.entities.Favorite;
|
import awais.instagrabber.db.entities.Favorite;
|
||||||
import awais.instagrabber.db.repositories.AccountRepository;
|
import awais.instagrabber.db.repositories.AccountRepository;
|
||||||
import awais.instagrabber.db.repositories.FavoriteRepository;
|
import awais.instagrabber.db.repositories.FavoriteRepository;
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback;
|
|
||||||
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
||||||
import awais.instagrabber.dialogs.ProfilePicDialogFragment;
|
import awais.instagrabber.dialogs.ProfilePicDialogFragment;
|
||||||
import awais.instagrabber.fragments.PostViewV2Fragment;
|
import awais.instagrabber.fragments.PostViewV2Fragment;
|
||||||
@ -702,76 +701,80 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
|
|
||||||
setupButtons(profileId);
|
setupButtons(profileId);
|
||||||
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext()));
|
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext()));
|
||||||
favoriteRepository.getFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback<Favorite>() {
|
favoriteRepository.getFavorite(
|
||||||
@Override
|
profileModel.getUsername(),
|
||||||
public void onSuccess(final Favorite result) {
|
FavoriteType.USER,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null || favorite == null) {
|
||||||
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
|
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
|
Log.e(TAG, "setProfileDetails: ", throwable);
|
||||||
|
return;
|
||||||
|
}
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
result.getId(),
|
new Favorite(
|
||||||
|
favorite.getId(),
|
||||||
profileModel.getUsername(),
|
profileModel.getUsername(),
|
||||||
FavoriteType.USER,
|
FavoriteType.USER,
|
||||||
profileModel.getFullName(),
|
profileModel.getFullName(),
|
||||||
profileModel.getProfilePicUrl(),
|
profileModel.getProfilePicUrl(),
|
||||||
result.getDateAdded()
|
favorite.getDateAdded()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
}
|
}
|
||||||
|
}), Dispatchers.getIO())
|
||||||
@Override
|
);
|
||||||
public void onDataNotAvailable() {
|
}))
|
||||||
|
);
|
||||||
|
profileDetailsBinding.favChip.setOnClickListener(v -> favoriteRepository.getFavorite(
|
||||||
|
profileModel.getUsername(),
|
||||||
|
FavoriteType.USER,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "setProfileDetails: ", throwable);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
if (favorite == null) {
|
||||||
}
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
|
new Favorite(
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
|
||||||
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
profileDetailsBinding.favChip.setOnClickListener(
|
|
||||||
v -> favoriteRepository.getFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback<Favorite>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Favorite result) {
|
|
||||||
favoriteRepository.deleteFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
|
||||||
0,
|
0,
|
||||||
profileModel.getUsername(),
|
profileModel.getUsername(),
|
||||||
FavoriteType.USER,
|
FavoriteType.USER,
|
||||||
profileModel.getFullName(),
|
profileModel.getFullName(),
|
||||||
profileModel.getProfilePicUrl(),
|
profileModel.getProfilePicUrl(),
|
||||||
LocalDateTime.now()
|
LocalDateTime.now()
|
||||||
), new RepositoryCallback<Void>() {
|
),
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
@Override
|
profileModel.getUsername(),
|
||||||
public void onDataNotAvailable() {
|
FavoriteType.USER,
|
||||||
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
}
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
}));
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
));
|
||||||
profileDetailsBinding.mainProfileImage.setImageURI(profileModel.getProfilePicUrl());
|
profileDetailsBinding.mainProfileImage.setImageURI(profileModel.getProfilePicUrl());
|
||||||
profileDetailsBinding.mainProfileImage.setVisibility(View.VISIBLE);
|
profileDetailsBinding.mainProfileImage.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
|
@ -38,7 +38,6 @@ import awais.instagrabber.db.entities.Account;
|
|||||||
import awais.instagrabber.db.entities.Favorite;
|
import awais.instagrabber.db.entities.Favorite;
|
||||||
import awais.instagrabber.db.repositories.AccountRepository;
|
import awais.instagrabber.db.repositories.AccountRepository;
|
||||||
import awais.instagrabber.db.repositories.FavoriteRepository;
|
import awais.instagrabber.db.repositories.FavoriteRepository;
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback;
|
|
||||||
import awais.instagrabber.interfaces.FetchListener;
|
import awais.instagrabber.interfaces.FetchListener;
|
||||||
import awais.instagrabber.models.enums.FavoriteType;
|
import awais.instagrabber.models.enums.FavoriteType;
|
||||||
import awais.instagrabber.utils.PasswordUtils.IncorrectPasswordException;
|
import awais.instagrabber.utils.PasswordUtils.IncorrectPasswordException;
|
||||||
@ -161,17 +160,20 @@ public final class ExportImportUtils {
|
|||||||
);
|
);
|
||||||
// Log.d(TAG, "importJson: favoriteModel: " + favoriteModel);
|
// Log.d(TAG, "importJson: favoriteModel: " + favoriteModel);
|
||||||
final FavoriteRepository favRepo = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
final FavoriteRepository favRepo = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
||||||
favRepo.getFavorite(query, favoriteType, new RepositoryCallback<Favorite>() {
|
favRepo.getFavorite(
|
||||||
@Override
|
query,
|
||||||
public void onSuccess(final Favorite result) {
|
favoriteType,
|
||||||
|
CoroutineUtilsKt.getContinuation((favorite1, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "importFavorites: ", throwable);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (favorite1 == null) {
|
||||||
|
favRepo.insertOrUpdateFavorite(favorite, CoroutineUtilsKt.getContinuation((unit, throwable1) -> {}, Dispatchers.getIO()));
|
||||||
|
}
|
||||||
// local has priority since it's more frequently updated
|
// local has priority since it's more frequently updated
|
||||||
}
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favRepo.insertOrUpdateFavorite(favorite, null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,7 +200,7 @@ public final class ExportImportUtils {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AccountRepository.getInstance(AccountDataSource.getInstance(context))
|
AccountRepository.getInstance(AccountDataSource.getInstance(context))
|
||||||
.insertOrUpdateAccounts(accounts, null);
|
.insertOrUpdateAccounts(accounts, CoroutineUtilsKt.getContinuation((unit, throwable) -> {}, Dispatchers.getIO()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void importSettings(final JSONObject jsonObject) {
|
private static void importSettings(final JSONObject jsonObject) {
|
||||||
@ -364,9 +366,13 @@ public final class ExportImportUtils {
|
|||||||
private static ListenableFuture<JSONArray> getFavorites(final Context context) {
|
private static ListenableFuture<JSONArray> getFavorites(final Context context) {
|
||||||
final SettableFuture<JSONArray> future = SettableFuture.create();
|
final SettableFuture<JSONArray> future = SettableFuture.create();
|
||||||
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
|
||||||
favoriteRepository.getAllFavorites(new RepositoryCallback<List<Favorite>>() {
|
favoriteRepository.getAllFavorites(
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((favorites, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final List<Favorite> favorites) {
|
if (throwable != null) {
|
||||||
|
future.set(new JSONArray());
|
||||||
|
Log.e(TAG, "getFavorites: ", throwable);
|
||||||
|
return;
|
||||||
|
}
|
||||||
final JSONArray jsonArray = new JSONArray();
|
final JSONArray jsonArray = new JSONArray();
|
||||||
try {
|
try {
|
||||||
for (final Favorite favorite : favorites) {
|
for (final Favorite favorite : favorites) {
|
||||||
@ -384,13 +390,8 @@ public final class ExportImportUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
future.set(jsonArray);
|
future.set(jsonArray);
|
||||||
}
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
future.set(new JSONArray());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
package awais.instagrabber.viewmodels
|
package awais.instagrabber.viewmodels
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.util.Log
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import awais.instagrabber.db.datasources.FavoriteDataSource
|
import awais.instagrabber.db.datasources.FavoriteDataSource
|
||||||
import awais.instagrabber.db.entities.Favorite
|
import awais.instagrabber.db.entities.Favorite
|
||||||
import awais.instagrabber.db.repositories.FavoriteRepository
|
import awais.instagrabber.db.repositories.FavoriteRepository
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback
|
import awais.instagrabber.utils.extensions.TAG
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class FavoritesViewModel(application: Application) : AndroidViewModel(application) {
|
class FavoritesViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
private val _list = MutableLiveData<List<Favorite>>()
|
private val _list = MutableLiveData<List<Favorite>>()
|
||||||
@ -20,29 +25,24 @@ class FavoritesViewModel(application: Application) : AndroidViewModel(applicatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun fetch() {
|
fun fetch() {
|
||||||
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
override fun onSuccess(favorites: List<Favorite>?) {
|
try {
|
||||||
_list.postValue(favorites ?: emptyList())
|
_list.postValue(favoriteRepository.getAllFavorites())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "fetch: ", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDataNotAvailable() {}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun delete(favorite: Favorite, onSuccess: () -> Unit) {
|
fun delete(favorite: Favorite, onSuccess: () -> Unit) {
|
||||||
favoriteRepository.deleteFavorite(favorite.query, favorite.type, object : RepositoryCallback<Void> {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
override fun onSuccess(result: Void?) {
|
try {
|
||||||
onSuccess()
|
favoriteRepository.deleteFavorite(favorite.query, favorite.type)
|
||||||
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
|
withContext(Dispatchers.Main) { onSuccess() }
|
||||||
override fun onSuccess(result: List<Favorite>?) {
|
_list.postValue(favoriteRepository.getAllFavorites())
|
||||||
_list.postValue(result ?: emptyList())
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "delete: ", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDataNotAvailable() {}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDataNotAvailable() {}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -33,9 +33,11 @@ import awais.instagrabber.repositories.responses.search.SearchResponse;
|
|||||||
import awais.instagrabber.utils.AppExecutors;
|
import awais.instagrabber.utils.AppExecutors;
|
||||||
import awais.instagrabber.utils.Constants;
|
import awais.instagrabber.utils.Constants;
|
||||||
import awais.instagrabber.utils.CookieUtils;
|
import awais.instagrabber.utils.CookieUtils;
|
||||||
|
import awais.instagrabber.utils.CoroutineUtilsKt;
|
||||||
import awais.instagrabber.utils.Debouncer;
|
import awais.instagrabber.utils.Debouncer;
|
||||||
import awais.instagrabber.utils.TextUtils;
|
import awais.instagrabber.utils.TextUtils;
|
||||||
import awais.instagrabber.webservices.SearchService;
|
import awais.instagrabber.webservices.SearchService;
|
||||||
|
import kotlinx.coroutines.Dispatchers;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
@ -191,17 +193,17 @@ public class SearchFragmentViewModel extends AppStateViewModel {
|
|||||||
recentResultsFuture.set(Collections.emptyList());
|
recentResultsFuture.set(Collections.emptyList());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
favoriteRepository.getAllFavorites(new RepositoryCallback<List<Favorite>>() {
|
favoriteRepository.getAllFavorites(
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((favorites, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final List<Favorite> result) {
|
if (throwable != null) {
|
||||||
favoritesFuture.set(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoritesFuture.set(Collections.emptyList());
|
favoritesFuture.set(Collections.emptyList());
|
||||||
|
Log.e(TAG, "showRecentSearchesAndFavorites: ", throwable);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
//noinspection unchecked
|
||||||
|
favoritesFuture.set((List<Favorite>) favorites);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
//noinspection UnstableApiUsage
|
//noinspection UnstableApiUsage
|
||||||
final ListenableFuture<List<List<?>>> listenableFuture = Futures.allAsList(recentResultsFuture, favoritesFuture);
|
final ListenableFuture<List<List<?>>> listenableFuture = Futures.allAsList(recentResultsFuture, favoritesFuture);
|
||||||
Futures.addCallback(listenableFuture, new FutureCallback<List<List<?>>>() {
|
Futures.addCallback(listenableFuture, new FutureCallback<List<List<?>>>() {
|
||||||
|
Loading…
Reference in New Issue
Block a user