mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 06:37:30 +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) {
|
||||||
hashtag,
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
FavoriteType.HASHTAG,
|
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
hashtagModel.getName(),
|
return;
|
||||||
"res:/" + R.drawable.ic_hashtag,
|
|
||||||
result.getDateAdded()
|
|
||||||
), new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
|
||||||
}
|
}
|
||||||
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
@Override
|
new Favorite(
|
||||||
public void onDataNotAvailable() {}
|
favorite.getId(),
|
||||||
});
|
hashtag,
|
||||||
}
|
FavoriteType.HASHTAG,
|
||||||
|
hashtagModel.getName(),
|
||||||
@Override
|
"res:/" + R.drawable.ic_hashtag,
|
||||||
public void onDataNotAvailable() {
|
favorite.getDateAdded()
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
),
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
}
|
if (throwable1 != null) {
|
||||||
});
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
hashtagDetailsBinding.favChip.setOnClickListener(
|
return;
|
||||||
v -> favoriteRepository.getFavorite(hashtag, FavoriteType.HASHTAG, new RepositoryCallback<Favorite>() {
|
}
|
||||||
@Override
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
public void onSuccess(final Favorite result) {
|
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
favoriteRepository.deleteFavorite(hashtag, FavoriteType.HASHTAG, new RepositoryCallback<Void>() {
|
}), Dispatchers.getIO())
|
||||||
@Override
|
);
|
||||||
public void onSuccess(final Void result) {
|
}), 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) {
|
||||||
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
|
new Favorite(
|
||||||
|
0,
|
||||||
|
hashtag,
|
||||||
|
FavoriteType.HASHTAG,
|
||||||
|
hashtagModel.getName(),
|
||||||
|
"res:/" + R.drawable.ic_hashtag,
|
||||||
|
LocalDateTime.now()
|
||||||
|
),
|
||||||
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
|
hashtag,
|
||||||
|
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.setText(R.string.add_to_favorites);
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
}
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
@Override
|
}), Dispatchers.getIO())
|
||||||
public void onDataNotAvailable() {}
|
)
|
||||||
});
|
);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
|
||||||
0,
|
|
||||||
hashtag,
|
|
||||||
FavoriteType.HASHTAG,
|
|
||||||
hashtagModel.getName(),
|
|
||||||
"res:/" + R.drawable.ic_hashtag,
|
|
||||||
LocalDateTime.now()
|
|
||||||
), new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
hashtagDetailsBinding.favChip.setText(R.string.favorite_short);
|
|
||||||
hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
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,
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
if (throwable != null || favorite == null) {
|
||||||
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
result.getId(),
|
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
String.valueOf(locationId),
|
Log.e(TAG, "setupLocationDetails: ", throwable);
|
||||||
FavoriteType.LOCATION,
|
return;
|
||||||
locationModel.getName(),
|
}
|
||||||
"res:/" + R.drawable.ic_location,
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
result.getDateAdded()
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
), new RepositoryCallback<Void>() {
|
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
@Override
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
public void onSuccess(final Void result) {}
|
new Favorite(
|
||||||
|
favorite.getId(),
|
||||||
@Override
|
String.valueOf(locationId),
|
||||||
public void onDataNotAvailable() {}
|
FavoriteType.LOCATION,
|
||||||
});
|
locationModel.getName(),
|
||||||
}
|
"res:/" + R.drawable.ic_location,
|
||||||
|
favorite.getDateAdded()
|
||||||
@Override
|
),
|
||||||
public void onDataNotAvailable() {
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
if (throwable1 != null) {
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
}
|
||||||
}
|
}), Dispatchers.getIO())
|
||||||
});
|
);
|
||||||
locationDetailsBinding.favChip.setOnClickListener(v -> {
|
}), Dispatchers.getIO())
|
||||||
favoriteRepository.getFavorite(String.valueOf(locationId), FavoriteType.LOCATION, new RepositoryCallback<Favorite>() {
|
);
|
||||||
@Override
|
locationDetailsBinding.favChip.setOnClickListener(v -> favoriteRepository.getFavorite(
|
||||||
public void onSuccess(final Favorite result) {
|
String.valueOf(locationId),
|
||||||
favoriteRepository.deleteFavorite(String.valueOf(locationId), FavoriteType.LOCATION, new RepositoryCallback<Void>() {
|
FavoriteType.LOCATION,
|
||||||
@Override
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
public void onSuccess(final Void result) {
|
if (throwable != null) {
|
||||||
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
Log.e(TAG, "setupLocationDetails: ", throwable);
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
return;
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
}
|
||||||
}
|
if (favorite == null) {
|
||||||
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
@Override
|
new Favorite(
|
||||||
public void onDataNotAvailable() {}
|
0,
|
||||||
});
|
String.valueOf(locationId),
|
||||||
}
|
FavoriteType.LOCATION,
|
||||||
|
locationModel.getName(),
|
||||||
@Override
|
"res:/" + R.drawable.ic_location,
|
||||||
public void onDataNotAvailable() {
|
LocalDateTime.now()
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
),
|
||||||
0,
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
String.valueOf(locationId),
|
String.valueOf(locationId),
|
||||||
FavoriteType.LOCATION,
|
FavoriteType.LOCATION,
|
||||||
locationModel.getName(),
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
"res:/" + R.drawable.ic_location,
|
if (throwable1 != null) {
|
||||||
LocalDateTime.now()
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
), new RepositoryCallback<Void>() {
|
return;
|
||||||
@Override
|
}
|
||||||
public void onSuccess(final Void result) {
|
locationDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
locationDetailsBinding.favChip.setText(R.string.favorite_short);
|
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
locationDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
}), Dispatchers.getIO())
|
||||||
}
|
);
|
||||||
|
}), Dispatchers.getIO())
|
||||||
@Override
|
));
|
||||||
public void onDataNotAvailable() {}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
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,
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
CoroutineUtilsKt.getContinuation((favorite, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
if (throwable != null || favorite == null) {
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
result.getId(),
|
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
||||||
profileModel.getUsername(),
|
Log.e(TAG, "setProfileDetails: ", throwable);
|
||||||
FavoriteType.USER,
|
return;
|
||||||
profileModel.getFullName(),
|
|
||||||
profileModel.getProfilePicUrl(),
|
|
||||||
result.getDateAdded()
|
|
||||||
), new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
}
|
}
|
||||||
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
@Override
|
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
public void onDataNotAvailable() {
|
favoriteRepository.insertOrUpdateFavorite(
|
||||||
|
new Favorite(
|
||||||
|
favorite.getId(),
|
||||||
|
profileModel.getUsername(),
|
||||||
|
FavoriteType.USER,
|
||||||
|
profileModel.getFullName(),
|
||||||
|
profileModel.getProfilePicUrl(),
|
||||||
|
favorite.getDateAdded()
|
||||||
|
),
|
||||||
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
|
if (throwable1 != null) {
|
||||||
|
Log.e(TAG, "onSuccess: ", throwable1);
|
||||||
|
}
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
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
|
0,
|
||||||
public void onDataNotAvailable() {
|
profileModel.getUsername(),
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
FavoriteType.USER,
|
||||||
profileDetailsBinding.favChip.setText(R.string.add_to_favorites);
|
profileModel.getFullName(),
|
||||||
}
|
profileModel.getProfilePicUrl(),
|
||||||
});
|
LocalDateTime.now()
|
||||||
profileDetailsBinding.favChip.setOnClickListener(
|
),
|
||||||
v -> favoriteRepository.getFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback<Favorite>() {
|
CoroutineUtilsKt.getContinuation((unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
@Override
|
if (throwable1 != null) {
|
||||||
public void onSuccess(final Favorite result) {
|
Log.e(TAG, "onDataNotAvailable: ", throwable1);
|
||||||
favoriteRepository.deleteFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback<Void>() {
|
return;
|
||||||
@Override
|
}
|
||||||
public void onSuccess(final Void result) {
|
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
||||||
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
||||||
|
showSnackbar(getString(R.string.added_to_favs));
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
favoriteRepository.deleteFavorite(
|
||||||
|
profileModel.getUsername(),
|
||||||
|
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.setText(R.string.add_to_favorites);
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_outline_star_plus_24);
|
||||||
showSnackbar(getString(R.string.removed_from_favs));
|
showSnackbar(getString(R.string.removed_from_favs));
|
||||||
}
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
@Override
|
}), Dispatchers.getIO())
|
||||||
public void onDataNotAvailable() {
|
));
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
favoriteRepository.insertOrUpdateFavorite(new Favorite(
|
|
||||||
0,
|
|
||||||
profileModel.getUsername(),
|
|
||||||
FavoriteType.USER,
|
|
||||||
profileModel.getFullName(),
|
|
||||||
profileModel.getProfilePicUrl(),
|
|
||||||
LocalDateTime.now()
|
|
||||||
), new RepositoryCallback<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final Void result) {
|
|
||||||
profileDetailsBinding.favChip.setText(R.string.favorite_short);
|
|
||||||
profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24);
|
|
||||||
showSnackbar(getString(R.string.added_to_favs));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataNotAvailable() {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
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,
|
||||||
// local has priority since it's more frequently updated
|
CoroutineUtilsKt.getContinuation((favorite1, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
||||||
}
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "importFavorites: ", throwable);
|
||||||
@Override
|
return;
|
||||||
public void onDataNotAvailable() {
|
}
|
||||||
favRepo.insertOrUpdateFavorite(favorite, null);
|
if (favorite1 == null) {
|
||||||
}
|
favRepo.insertOrUpdateFavorite(favorite, CoroutineUtilsKt.getContinuation((unit, throwable1) -> {}, Dispatchers.getIO()));
|
||||||
});
|
}
|
||||||
|
// local has priority since it's more frequently updated
|
||||||
|
}), Dispatchers.getIO())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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,33 +366,32 @@ 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) {
|
||||||
final JSONArray jsonArray = new JSONArray();
|
future.set(new JSONArray());
|
||||||
try {
|
Log.e(TAG, "getFavorites: ", throwable);
|
||||||
for (final Favorite favorite : favorites) {
|
return;
|
||||||
final JSONObject jsonObject = new JSONObject();
|
|
||||||
jsonObject.put("q", favorite.getQuery());
|
|
||||||
jsonObject.put("type", favorite.getType().toString());
|
|
||||||
jsonObject.put("s", favorite.getDisplayName());
|
|
||||||
jsonObject.put("pic_url", favorite.getPicUrl());
|
|
||||||
jsonObject.put("d", favorite.getDateAdded().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
|
||||||
jsonArray.put(jsonObject);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
final JSONArray jsonArray = new JSONArray();
|
||||||
if (BuildConfig.DEBUG) {
|
try {
|
||||||
Log.e(TAG, "Error exporting favorites", e);
|
for (final Favorite favorite : favorites) {
|
||||||
|
final JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("q", favorite.getQuery());
|
||||||
|
jsonObject.put("type", favorite.getType().toString());
|
||||||
|
jsonObject.put("s", favorite.getDisplayName());
|
||||||
|
jsonObject.put("pic_url", favorite.getPicUrl());
|
||||||
|
jsonObject.put("d", favorite.getDateAdded().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||||
|
jsonArray.put(jsonObject);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
Log.e(TAG, "Error exporting favorites", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
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);
|
favoritesFuture.set(Collections.emptyList());
|
||||||
}
|
Log.e(TAG, "showRecentSearchesAndFavorites: ", throwable);
|
||||||
|
return;
|
||||||
@Override
|
}
|
||||||
public void onDataNotAvailable() {
|
//noinspection unchecked
|
||||||
favoritesFuture.set(Collections.emptyList());
|
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