mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-26 00:27:30 +00:00
fix favourites logic
This commit is contained in:
parent
a983f44769
commit
484b0f9657
@ -81,7 +81,6 @@ public class FavoritesFragment extends Fragment {
|
||||
@Override
|
||||
public void onSuccess(final List<Favorite> 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<Favorite> allFavorites) {
|
||||
final Runnable runnable = () -> {
|
||||
final List<Favorite> 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<Void>() {
|
||||
@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<Void>() {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
@ -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<Void>() {
|
||||
@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<Void>() {
|
||||
@Override
|
||||
|
@ -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<Void>() {
|
||||
@Override
|
||||
public void onSuccess(final Void result) {}
|
||||
|
||||
@Override
|
||||
public void onDataNotAvailable() {}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<Void>() {
|
||||
@Override
|
||||
public void onSuccess(final Void result) {}
|
||||
|
||||
@Override
|
||||
public void onDataNotAvailable() {}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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" />
|
||||
|
||||
<com.google.android.material.chip.Chip
|
||||
android:id="@+id/mainStatus"
|
||||
@ -41,13 +42,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/mainPostCount"
|
||||
app:layout_constraintTop_toTopOf="@id/mainPostCount"
|
||||
tools:text="omg what do u expect" />
|
||||
tools:text="omg what do u expect"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<com.google.android.material.chip.Chip
|
||||
android:id="@+id/mainFollowers"
|
||||
@ -55,14 +57,15 @@
|
||||
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/fav_chip"
|
||||
app:layout_constraintStart_toEndOf="@id/mainProfileImage"
|
||||
app:layout_constraintTop_toBottomOf="@id/mainPostCount"
|
||||
app:rippleColor="@color/grey_400"
|
||||
tools:text="10 Followers" />
|
||||
tools:text="10 Followers"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<com.google.android.material.chip.Chip
|
||||
android:id="@+id/mainFollowing"
|
||||
@ -70,14 +73,15 @@
|
||||
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/fav_chip"
|
||||
app:layout_constraintStart_toEndOf="@id/mainFollowers"
|
||||
app:layout_constraintTop_toBottomOf="@id/mainPostCount"
|
||||
app:rippleColor="@color/grey_400"
|
||||
tools:text="10 Following" />
|
||||
tools:text="10 Following"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<com.google.android.material.chip.Chip
|
||||
android:id="@+id/fav_chip"
|
||||
@ -86,6 +90,7 @@
|
||||
android:layout_marginStart="8dp"
|
||||
android:text="@string/add_to_favorites"
|
||||
android:visibility="gone"
|
||||
app:chipBackgroundColor="@null"
|
||||
app:chipIcon="@drawable/ic_outline_star_plus_24"
|
||||
app:chipIconTint="@color/yellow_800"
|
||||
app:layout_constraintBottom_toBottomOf="@id/mainProfileImage"
|
||||
@ -101,6 +106,7 @@
|
||||
android:layout_marginStart="4dp"
|
||||
android:text="@string/tagged"
|
||||
android:visibility="gone"
|
||||
app:chipBackgroundColor="@null"
|
||||
app:chipIcon="@drawable/ic_outline_person_pin_24"
|
||||
app:chipIconTint="@color/deep_orange_800"
|
||||
app:layout_constraintBottom_toBottomOf="@id/fav_chip"
|
||||
|
Loading…
Reference in New Issue
Block a user