-
Notifications
You must be signed in to change notification settings - Fork 857
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Realm DB instead SharedPreferences
- Loading branch information
Showing
7 changed files
with
150 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 35 additions & 48 deletions
83
app/src/main/java/com/esoxjem/movieguide/favorites/FavoritesStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,76 @@ | ||
package com.esoxjem.movieguide.favorites; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.text.TextUtils; | ||
|
||
import com.esoxjem.movieguide.Movie; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Moshi; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import io.realm.Realm; | ||
import io.realm.RealmResults; | ||
|
||
/** | ||
* @author arun | ||
*/ | ||
@Singleton | ||
public class FavoritesStore | ||
{ | ||
|
||
private static final String PREF_NAME = "FavoritesStore"; | ||
private SharedPreferences pref; | ||
private Realm realm; | ||
|
||
@Inject | ||
public FavoritesStore(Context context) | ||
public FavoritesStore(Realm realm) | ||
{ | ||
pref = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); | ||
this.realm = realm; | ||
} | ||
|
||
public void setFavorite(Movie movie) | ||
{ | ||
SharedPreferences.Editor editor = pref.edit(); | ||
Moshi moshi = new Moshi.Builder().build(); | ||
JsonAdapter<Movie> jsonAdapter = moshi.adapter(Movie.class); | ||
String movieJson = jsonAdapter.toJson(movie); | ||
editor.putString(movie.getId(), movieJson); | ||
editor.apply(); | ||
realm.beginTransaction(); | ||
realm.copyToRealmOrUpdate(new MovieRealmObject(movie)); | ||
realm.commitTransaction(); | ||
} | ||
|
||
public boolean isFavorite(String id) | ||
{ | ||
String movieJson = pref.getString(id, null); | ||
|
||
if (!TextUtils.isEmpty(movieJson)) | ||
{ | ||
return true; | ||
} else | ||
{ | ||
return false; | ||
} | ||
MovieRealmObject res = realm.where(MovieRealmObject.class).equalTo(MovieRealmObject.ID, id).findFirst(); | ||
return res != null; | ||
} | ||
|
||
public List<Movie> getFavorites() throws IOException | ||
public List<Movie> getFavorites() | ||
{ | ||
Map<String, ?> allEntries = pref.getAll(); | ||
ArrayList<Movie> movies = new ArrayList<>(24); | ||
Moshi moshi = new Moshi.Builder().build(); | ||
RealmResults<MovieRealmObject> res = realm.where(MovieRealmObject.class).findAll(); | ||
List<Movie> movies = new ArrayList<>(); | ||
|
||
for (Map.Entry<String, ?> entry : allEntries.entrySet()) | ||
{ | ||
String movieJson = pref.getString(entry.getKey(), null); | ||
|
||
if (!TextUtils.isEmpty(movieJson)) | ||
{ | ||
JsonAdapter<Movie> jsonAdapter = moshi.adapter(Movie.class); | ||
|
||
Movie movie = jsonAdapter.fromJson(movieJson); | ||
movies.add(movie); | ||
} else | ||
{ | ||
// Do nothing; | ||
} | ||
for(MovieRealmObject i : res){ | ||
movies.add(movieRealmObjectToMovie(i)); | ||
} | ||
|
||
return movies; | ||
} | ||
|
||
public void unfavorite(String id) | ||
{ | ||
SharedPreferences.Editor editor = pref.edit(); | ||
editor.remove(id); | ||
editor.apply(); | ||
realm.beginTransaction(); | ||
MovieRealmObject movie = realm.where(MovieRealmObject.class).equalTo(MovieRealmObject.ID, id).findFirst(); | ||
if(movie != null) | ||
movie.deleteFromRealm(); | ||
realm.commitTransaction(); | ||
} | ||
|
||
private Movie movieRealmObjectToMovie(MovieRealmObject movieRealmObject){ | ||
Movie movie = new Movie(); | ||
movie.setId(movieRealmObject.getId()); | ||
movie.setOverview(movieRealmObject.getOverview()); | ||
movie.setReleaseDate(movieRealmObject.getReleaseDate()); | ||
movie.setPosterPath(movieRealmObject.getPosterPath()); | ||
movie.setBackdropPath(movieRealmObject.getBackdropPath()); | ||
movie.setTitle(movieRealmObject.getTitle()); | ||
movie.setVoteAverage(movieRealmObject.getVoteAverage()); | ||
|
||
return movie; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/esoxjem/movieguide/favorites/MovieRealmObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.esoxjem.movieguide.favorites; | ||
|
||
import com.esoxjem.movieguide.Movie; | ||
|
||
import io.realm.RealmObject; | ||
import io.realm.annotations.PrimaryKey; | ||
|
||
public class MovieRealmObject extends RealmObject { | ||
|
||
@PrimaryKey | ||
private String id; | ||
private String overview; | ||
private String releaseDate; | ||
private String posterPath; | ||
private String backdropPath; | ||
private String title; | ||
private double voteAverage; | ||
|
||
public static final String ID = "id"; | ||
public static final String OVERVIEW = "overview"; | ||
public static final String RELEASE_DATE = "releaseDate"; | ||
public static final String POSTER_PATH = "posterPath"; | ||
public static final String BACKDROP_PATH = "backdropPath"; | ||
public static final String TITLE = "title"; | ||
public static final String VOTE_AVERAGE = "voteAverage"; | ||
|
||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getOverview() { | ||
return overview; | ||
} | ||
|
||
public void setOverview(String overview) { | ||
this.overview = overview; | ||
} | ||
|
||
public String getReleaseDate() { | ||
return releaseDate; | ||
} | ||
|
||
public void setReleaseDate(String releaseDate) { | ||
this.releaseDate = releaseDate; | ||
} | ||
|
||
public String getPosterPath() { | ||
return posterPath; | ||
} | ||
|
||
public void setPosterPath(String posterPath) { | ||
this.posterPath = posterPath; | ||
} | ||
|
||
public String getBackdropPath() { | ||
return backdropPath; | ||
} | ||
|
||
public void setBackdropPath(String backdropPath) { | ||
this.backdropPath = backdropPath; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public double getVoteAverage() { | ||
return voteAverage; | ||
} | ||
|
||
public void setVoteAverage(double voteAverage) { | ||
this.voteAverage = voteAverage; | ||
} | ||
|
||
public MovieRealmObject(Movie movie) { | ||
id = movie.getId(); | ||
overview = movie.getOverview(); | ||
releaseDate = movie.getReleaseDate(); | ||
posterPath = movie.getPosterPath(); | ||
backdropPath = movie.getBackdropPath(); | ||
title = movie.getTitle(); | ||
voteAverage = movie.getVoteAverage(); | ||
} | ||
|
||
public MovieRealmObject(){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters