Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. package com.app.youtubers.realm;
  2.  
  3. import android.content.Context;
  4.  
  5. import com.app.youtubers.model.Info;
  6. import com.app.youtubers.model.Video;
  7. import com.app.youtubers.realm.table.RealmFavorite;
  8. import com.app.youtubers.realm.table.RealmInfo;
  9. import com.app.youtubers.utils.OnSaveListener;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import io.realm.Realm;
  15. import io.realm.RealmResults;
  16. import io.realm.Sort;
  17.  
  18. public class RealmController {
  19.  
  20. private Realm realm;
  21. private RealmResults<RealmFavorite> realmResult;
  22. public Context context;
  23.  
  24. public RealmController(Context context) {
  25. realm = Realm.getInstance(context);
  26. this.context = context;
  27. }
  28.  
  29. public int getNextKey() {
  30. try {
  31. Number number = realm.where(RealmFavorite.class).max("favId");
  32. if (number != null) {
  33. return number.intValue() + 1;
  34. } else {
  35. return 0;
  36. }
  37. } catch (ArrayIndexOutOfBoundsException e) {
  38. return 0;
  39. }
  40. }
  41.  
  42. public void addFavorite(String title, String description, String videoId, String thumbnail, OnSaveListener listener) {
  43. RealmFavorite fav = new RealmFavorite();
  44. fav.setId(getNextKey());
  45. fav.setVideoId(videoId);
  46. fav.setTitle(title);
  47. fav.setDescription(description);
  48. fav.setThumbnail(thumbnail);
  49.  
  50. realm.beginTransaction();
  51. realm.copyToRealm(fav);
  52. realm.commitTransaction();
  53.  
  54. if (listener != null) listener.action();
  55. }
  56.  
  57. public void addFavorite(Video video, OnSaveListener listener) {
  58. String thumb = video.snippet.thumbnails != null ? video.snippet.thumbnails.high.url : "";
  59. addFavorite(video.snippet.title, video.snippet.description, video.contentDetails.videoId, thumb, listener);
  60. }
  61.  
  62. public RealmInfo addInfo(Info info) {
  63. RealmInfo rInfo = new RealmInfo();
  64. rInfo.setTitle(info.snippet.title);
  65. rInfo.setDescription(info.snippet.description);
  66. rInfo.setThumbUrl(info.snippet.thumbnails.medium.url);
  67. if(info.brandingSettings != null && info.brandingSettings.image != null && info.brandingSettings.image.bannerMobileImageUrl != null){
  68. rInfo.setBannerUrl(info.brandingSettings.image.bannerMobileImageUrl);
  69. } else {
  70. rInfo.setBannerUrl(null);
  71. }
  72. rInfo.setVideoCount(info.statistics.videoCount);
  73.  
  74. realm.beginTransaction();
  75. realm.copyToRealmOrUpdate(rInfo);
  76. realm.commitTransaction();
  77.  
  78. return rInfo;
  79. }
  80.  
  81. public List<RealmFavorite> showAllFav() {
  82. List<RealmFavorite> data = new ArrayList<>();
  83.  
  84. realmResult = realm.where(RealmFavorite.class).findAll();
  85. realmResult.sort("favId", Sort.DESCENDING);
  86. if (realmResult.size() > 0) {
  87. for (int i = 0; i < realmResult.size(); i++) {
  88. int id;
  89. String title, description, videoId, thumbnail;
  90. id = realmResult.get(i).getId();
  91. title = realmResult.get(i).getTitle();
  92. description = realmResult.get(i).getDescription();
  93. videoId = realmResult.get(i).getVideoId();
  94. thumbnail = realmResult.get(i).getThumbnail();
  95. data.add(new RealmFavorite(videoId, id, title, description, thumbnail));
  96. }
  97.  
  98. }
  99. return data;
  100. }
  101.  
  102. public RealmInfo getInfo() {
  103. RealmInfo rInfo = realm.where(RealmInfo.class).findFirst();
  104. return rInfo;
  105. }
  106.  
  107. public void deleteData(String videoId, OnSaveListener listener) {
  108. RealmResults<RealmFavorite> dataDesults = realm.where(RealmFavorite.class).equalTo("videoId", videoId).findAll();
  109. realm.beginTransaction();
  110. dataDesults.remove(0);
  111. dataDesults.removeLast();
  112. dataDesults.clear();
  113. realm.commitTransaction();
  114.  
  115. listener.action();
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement