From 484b0f96579ae8a0d4a50d7e547065e1b71b81ed Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 20 Dec 2020 17:04:25 -0500 Subject: [PATCH] fix favourites logic --- .../fragments/FavoritesFragment.java | 106 ------------------ .../fragments/HashTagFragment.java | 16 ++- .../fragments/LocationFragment.java | 14 +++ .../fragments/main/ProfileFragment.java | 14 +++ .../res/layout/layout_profile_details.xml | 30 +++-- 5 files changed, 61 insertions(+), 119 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.java b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.java index 190e9a59..34c81322 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.java @@ -81,7 +81,6 @@ public class FavoritesFragment extends Fragment { @Override public void onSuccess(final List favorites) { favoritesViewModel.getList().postValue(favorites); - fetchMissingInfo(favorites); } @Override @@ -156,109 +155,4 @@ public class FavoritesFragment extends Fragment { binding.favoriteList.setAdapter(adapter); } - - private void fetchMissingInfo(final List allFavorites) { - final Runnable runnable = () -> { - final List updatedList = new ArrayList<>(allFavorites); - // cyclic barrier is to make the async calls synchronous - final CyclicBarrier cyclicBarrier = new CyclicBarrier(2, () -> { - // Log.d(TAG, "fetchMissingInfo: barrier action"); - favoritesViewModel.getList().postValue(new ArrayList<>(updatedList)); - }); - try { - for (final Favorite model : allFavorites) { - cyclicBarrier.reset(); - // if the model has missing pic or display name (for user and location), fetch those details - switch (model.getType()) { - case LOCATION: - if (TextUtils.isEmpty(model.getDisplayName()) - || TextUtils.isEmpty(model.getPicUrl())) { - new LocationFetcher(model.getQuery(), result -> { - if (result == null) return; - final int i = updatedList.indexOf(model); - updatedList.remove(i); - final Favorite updated = new Favorite( - model.getId(), - model.getQuery(), - model.getType(), - result.getName(), - result.getSdProfilePic(), - model.getDateAdded() - ); - favoriteRepository.insertOrUpdateFavorite(updated, new RepositoryCallback() { - @Override - public void onSuccess(final Void result) { - updatedList.add(i, updated); - try { - cyclicBarrier.await(); - } catch (BrokenBarrierException | InterruptedException e) { - Log.e(TAG, "fetchMissingInfo: ", e); - } - } - - @Override - public void onDataNotAvailable() { - try { - cyclicBarrier.await(); - } catch (BrokenBarrierException | InterruptedException e) { - Log.e(TAG, "fetchMissingInfo: ", e); - } - } - }); - }).execute(); - cyclicBarrier.await(); - } - break; - case USER: - if (TextUtils.isEmpty(model.getDisplayName()) - || TextUtils.isEmpty(model.getPicUrl())) { - new ProfileFetcher(model.getQuery(), result -> { - if (result == null) return; - final int i = updatedList.indexOf(model); - updatedList.remove(i); - final Favorite updated = new Favorite( - model.getId(), - model.getQuery(), - model.getType(), - result.getName(), - result.getSdProfilePic(), - model.getDateAdded() - ); - favoriteRepository.insertOrUpdateFavorite(updated, new RepositoryCallback() { - @Override - public void onSuccess(final Void result) { - try { - cyclicBarrier.await(); - } catch (BrokenBarrierException | InterruptedException e) { - Log.e(TAG, "fetchMissingInfo: ", e); - } - } - - @Override - public void onDataNotAvailable() { - try { - cyclicBarrier.await(); - } catch (BrokenBarrierException | InterruptedException e) { - Log.e(TAG, "fetchMissingInfo: ", e); - } - } - }); - updatedList.add(i, updated); - }).execute(); - cyclicBarrier.await(); - } - break; - case HASHTAG: - default: - // hashtags don't require displayName or pic - // updatedList.add(model); - } - } - } catch (Exception e) { - Log.e(TAG, "fetchMissingInfo: ", e); - } - favoritesViewModel.getList().postValue(updatedList); - }; - new Thread(runnable).start(); - } } diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index c89f7970..ac20984d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -464,6 +464,20 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onSuccess(final Favorite result) { hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24); hashtagDetailsBinding.favChip.setText(R.string.favorite_short); + favoriteRepository.insertOrUpdateFavorite(new Favorite( + result.getId(), + hashtag.substring(1), + FavoriteType.HASHTAG, + hashtagModel.getName(), + hashtagModel.getSdProfilePic(), + result.getDateAdded() + ), new RepositoryCallback() { + @Override + public void onSuccess(final Void result) {} + + @Override + public void onDataNotAvailable() {} + }); } @Override @@ -496,7 +510,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe hashtag.substring(1), FavoriteType.HASHTAG, hashtagModel.getName(), - null, + hashtagModel.getSdProfilePic(), new Date() ), new RepositoryCallback() { @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index d857c083..ba9cd08a 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -456,6 +456,20 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR 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); + favoriteRepository.insertOrUpdateFavorite(new Favorite( + result.getId(), + locationId, + FavoriteType.LOCATION, + locationModel.getName(), + locationModel.getSdProfilePic(), + result.getDateAdded() + ), new RepositoryCallback() { + @Override + public void onSuccess(final Void result) {} + + @Override + public void onDataNotAvailable() {} + }); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index f9e85d3e..45bcc334 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -578,6 +578,20 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onSuccess(final Favorite result) { profileDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24); profileDetailsBinding.favChip.setText(R.string.added_to_favs); + favoriteRepository.insertOrUpdateFavorite(new Favorite( + result.getId(), + finalUsername, + FavoriteType.USER, + profileModel.getName(), + profileModel.getSdProfilePic(), + result.getDateAdded() + ), new RepositoryCallback() { + @Override + public void onSuccess(final Void result) {} + + @Override + public void onDataNotAvailable() {} + }); } @Override diff --git a/app/src/main/res/layout/layout_profile_details.xml b/app/src/main/res/layout/layout_profile_details.xml index eb14c1f9..527d5952 100644 --- a/app/src/main/res/layout/layout_profile_details.xml +++ b/app/src/main/res/layout/layout_profile_details.xml @@ -27,13 +27,14 @@ android:layout_height="@dimen/profile_chip_size" android:layout_marginStart="4dp" android:clickable="false" - android:visibility="gone" - tools:visibility="visible" android:gravity="center" + android:visibility="gone" + app:chipBackgroundColor="@null" app:layout_constraintBottom_toTopOf="@id/mainFollowers" app:layout_constraintStart_toEndOf="@id/mainProfileImage" app:layout_constraintTop_toTopOf="@id/mainProfileImage" - tools:text="35 Posts" /> + tools:text="35 Posts" + tools:visibility="visible" /> + tools:text="omg what do u expect" + tools:visibility="visible" /> + tools:text="10 Followers" + tools:visibility="visible" /> + tools:text="10 Following" + tools:visibility="visible" />