Skip to content

Commit

Permalink
added Realm DB instead SharedPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
NUTS-COON committed Jun 17, 2018
1 parent b01f10d commit fb25215
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 55 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'realm-android'

def getProperty(String filename, String propName) {
def propsFile = rootProject.file(filename)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/esoxjem/movieguide/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import dagger.Module;
import dagger.Provides;
import io.realm.Realm;

/**
* @author arunsasidharan
Expand Down Expand Up @@ -36,4 +37,11 @@ public Resources provideResources(Context context)
{
return context.getResources();
}

@Provides
@Singleton
public Realm provideRealm()
{
return Realm.getDefaultInstance();
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/esoxjem/movieguide/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.esoxjem.movieguide.network.NetworkModule;
import com.esoxjem.movieguide.listing.sorting.SortingModule;

import io.realm.Realm;

/**
* @author arun
*/
Expand All @@ -25,6 +27,7 @@ public void onCreate()
{
super.onCreate();
StrictMode.enableDefaults();
initRealm();
appComponent = createAppComponent();
}

Expand All @@ -37,6 +40,10 @@ private AppComponent createAppComponent()
.build();
}

private void initRealm(){
Realm.init(this);
}

public DetailsComponent createDetailsComponent()
{
detailsComponent = appComponent.plus(new DetailsModule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ public boolean isFavorite(String id)
@Override
public List<Movie> getFavorites()
{
try
{
return favoritesStore.getFavorites();
} catch (IOException ignored)
{
return new ArrayList<>(0);
}
return favoritesStore.getFavorites();
}

@Override
Expand Down
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;
}
}
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(){

}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "io.realm:realm-gradle-plugin:5.3.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit fb25215

Please sign in to comment.