Commit 85a83210 authored by Robert Hitt's avatar Robert Hitt
Browse files

Favorite status is now saved.

- yay SharedPreferences
parent bb486366
...@@ -2,9 +2,10 @@ package srct.whatsopen.ui; ...@@ -2,9 +2,10 @@ package srct.whatsopen.ui;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -22,9 +23,7 @@ import butterknife.ButterKnife; ...@@ -22,9 +23,7 @@ import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import io.realm.OrderedRealmCollection; import io.realm.OrderedRealmCollection;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmAsyncTask;
import io.realm.RealmList; import io.realm.RealmList;
import io.realm.RealmQuery;
import io.realm.RealmRecyclerViewAdapter; import io.realm.RealmRecyclerViewAdapter;
import io.realm.RealmResults; import io.realm.RealmResults;
import srct.whatsopen.R; import srct.whatsopen.R;
...@@ -39,8 +38,6 @@ import srct.whatsopen.model.OpenTimes; ...@@ -39,8 +38,6 @@ import srct.whatsopen.model.OpenTimes;
public class FacilityListAdapter extends public class FacilityListAdapter extends
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> { RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> {
Context mContext;
public FacilityListAdapter(Context context, public FacilityListAdapter(Context context,
OrderedRealmCollection<Facility> data) { OrderedRealmCollection<Facility> data) {
...@@ -49,7 +46,7 @@ public class FacilityListAdapter extends ...@@ -49,7 +46,7 @@ public class FacilityListAdapter extends
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext(); Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
View facilityView = inflater.inflate(R.layout.item_facility, parent, false); View facilityView = inflater.inflate(R.layout.item_facility, parent, false);
...@@ -68,10 +65,10 @@ public class FacilityListAdapter extends ...@@ -68,10 +65,10 @@ public class FacilityListAdapter extends
if(isOpen) { if(isOpen) {
// set the RV cell to be highlighted // set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat holder.itemView.setBackgroundColor(ContextCompat
.getColor(mContext, R.color.facilityOpen)); .getColor(context, R.color.facilityOpen));
} else { } else {
holder.itemView.setBackgroundColor(ContextCompat holder.itemView.setBackgroundColor(ContextCompat
.getColor(mContext, R.color.facilityClosed)); .getColor(context, R.color.facilityClosed));
} }
if(facility.isFavorited()) { if(facility.isFavorited()) {
...@@ -150,11 +147,15 @@ public class FacilityListAdapter extends ...@@ -150,11 +147,15 @@ public class FacilityListAdapter extends
} }
} }
// Asynchronously updates the favorite status // Asynchronously updates the Realm object's favorite status
// Would block the favorite button redrawing otherwise // and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
void toggleFavoriteAsync(final boolean status) { void toggleFavoriteAsync(final boolean status) {
Realm realm = Realm.getDefaultInstance(); Realm realm = Realm.getDefaultInstance();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = data.getName(); final String facilityName = data.getName();
realm.executeTransactionAsync(new Realm.Transaction() { realm.executeTransactionAsync(new Realm.Transaction() {
@Override @Override
...@@ -164,6 +165,8 @@ public class FacilityListAdapter extends ...@@ -164,6 +165,8 @@ public class FacilityListAdapter extends
.equalTo("mName", facilityName).findFirst(); .equalTo("mName", facilityName).findFirst();
facility.setFavorited(status); facility.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
} }
}, null, null); }, null, null);
......
package srct.whatsopen.ui; package srct.whatsopen.ui;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
...@@ -67,11 +69,19 @@ public class MainActivity extends AppCompatActivity { ...@@ -67,11 +69,19 @@ public class MainActivity extends AppCompatActivity {
// Gets a Call from the given Retrofit service, then asynchronously executes it // Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB // On success, copies the resulting facility list to the Realm DB
private void callWhatsOpenAPI(WhatsOpenService service) { private void callWhatsOpenAPI(WhatsOpenService service) {
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Call<List<Facility>> call = service.facilityList(); Call<List<Facility>> call = service.facilityList();
call.enqueue(new Callback<List<Facility>>() { call.enqueue(new Callback<List<Facility>>() {
@Override @Override
public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) { public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) {
List<Facility> facilities = response.body(); List<Facility> facilities = response.body();
// Query SharedReferences for each Facility's favorite status. defaults to false
for(Facility facility : facilities) {
facility.setFavorited(pref.getBoolean(facility.getName(), false));
}
mRealm.beginTransaction(); mRealm.beginTransaction();
mRealm.copyToRealmOrUpdate(facilities); mRealm.copyToRealmOrUpdate(facilities);
mRealm.commitTransaction(); mRealm.commitTransaction();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment