Advertisement
Guest User

SetLike

a guest
Apr 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. @Dao
  2. public abstract class FavoriteCarsDao {
  3.  
  4.     @Query("UPDATE ShowcaseFavoriteCars SET is_like = :isLike WHERE ShowcaseFavoriteCars.auto_id LIKE :auto_id")
  5.     public abstract void updateLikeFavoriteCar(long auto_id, boolean isLike);
  6. }
  7.  
  8. public class DatabaseRepository implements DatabaseService {
  9.     @Override
  10.     public void deleteLikeFavoriteCar(long auto_id) {
  11.         Observable.fromCallable(() -> {
  12.             mDatabase.favoriteCarsDao().deleteLikeFavoriteCar(auto_id);
  13.             return null;
  14.         })
  15.                 .compose(RxUtils.async())
  16.                 .subscribe();
  17.     }
  18. }
  19.  
  20. public class CarShowcaseListPresenter {
  21.  
  22.     private List<ShowcaseFavoriteCars> mFavoriteCars;
  23.  
  24. public void getFavoriteCars() {
  25.         mDatabaseRepository.getFavoriteCars()
  26.                 .flatMap(Observable::from)
  27.                 .toList()
  28.                 .subscribe(favoriteCars -> {
  29.                     if (favoriteCars != null && !favoriteCars.isEmpty()) {
  30.                         mFavoriteCars = favoriteCars;
  31.                     } else {
  32.                         mFavoriteCars = new ArrayList<>();
  33.                     }
  34.                 });
  35.     }
  36.  
  37. @Override
  38.     public void onLikeClick(boolean isLike, @NonNull ShowcaseCarsList favoriteCar, int position) {
  39.         getFavoriteCars();
  40.  
  41.         List<Long> carIds = new ArrayList<>();
  42.         Observable.from(mFavoriteCars)
  43.                 .forEach(favoriteCars1 -> {
  44.                     carIds.add(favoriteCars1.getCarId());
  45.                 });
  46.  
  47.  
  48.  
  49.  
  50.         if (!carIds.contains(favoriteCar.getCarId())) {
  51.             mDatabaseRepository.saveFavoriteCar(new ShowcaseFavoriteCars(favoriteCar.getCarId(), true, null));
  52.         } else {
  53.             Observable<ShowcaseFavoriteCars> firstObser = Observable.from(mFavoriteCars).doOnNext(favoriteCars -> {
  54.                 if (favoriteCars.getComment() != null && !favoriteCars.getComment().isEmpty()) {
  55.                     mDatabaseRepository.updateLikeFavoriteCar(favoriteCar.getCarId(), isLike);
  56.                 } else /*if (favoriteCar1.getComment() == null || favoriteCar1.getComment().isEmpty())*/ {
  57.                     mDatabaseRepository.deleteLikeFavoriteCar(favoriteCar.getCarId());
  58.                 }
  59.  
  60.             });
  61.         }
  62.  
  63.  
  64.         mView.setLike(favoriteCar, position);
  65.  
  66.     }
  67. }
  68.  
  69. public void getFavoriteCars() {
  70.         mDatabaseRepository.getFavoriteCars()
  71.                 .flatMap(Observable::from)
  72.                 .toList()
  73.                 .subscribe(favoriteCars -> {
  74.                     if (favoriteCars != null && !favoriteCars.isEmpty()) {
  75.                         mFavoriteCars = favoriteCars;
  76.                     } else {
  77.                         mFavoriteCars = new ArrayList<>();
  78.                     }
  79.                 });
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement