Commit 5e34ef5f authored by Robert Hitt's avatar Robert Hitt

Fixed Realm related issues

- Maybe. I was never actually able to reproduce the crashes, but I at
  least made the Realm stuff a bit more idiomatic, which should
  hopefully fix the problems.
parent fd6f721e
Pipeline #1345 failed with stages
in 2 minutes and 57 seconds
......@@ -142,7 +142,7 @@ public class FacilityPresenter {
}
}
// Parses String with format YYYY-MM-DD to MM/DD
// Parses String with format YYYY-MM-DD to MM/DD/YY
public static String parseYMDtoMDY(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
......
......@@ -32,14 +32,17 @@ public class MainPresenter {
private MainView mMainView;
private SharedPreferences pref;
private Realm mRealm;
public void attachView(MainView view) {
this.mMainView = view;
mMainView = view;
mRealm = Realm.getDefaultInstance();
pref = PreferenceManager.getDefaultSharedPreferences(mMainView.getContext());
}
public void detachView() {
this.mMainView = null;
mMainView = null;
mRealm.close();
}
// Gets a Call from the given Retrofit service, then asynchronously executes it
......@@ -92,17 +95,14 @@ public class MainPresenter {
// Asynchronously writes the facility list to Realm
private void writeToRealm(List<Facility> facilities) {
final Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(bgRealm -> bgRealm.copyToRealmOrUpdate(facilities));
realm.close();
mRealm.executeTransactionAsync(bgRealm -> bgRealm.copyToRealmOrUpdate(facilities));
removeDeletedFacilities(facilities);
}
// Removes the facilities from Realm that are no longer in the Api
private void removeDeletedFacilities(List<Facility> facilities) {
Realm realm = Realm.getDefaultInstance();
RealmResults<Facility> results = realm.where(Facility.class).findAll();
RealmResults<Facility> results = mRealm.where(Facility.class).findAll();
// Not a pretty way to do this, but Facilities shouldn't ever have too many items anyway
for(Facility r : results) {
......@@ -117,36 +117,28 @@ public class MainPresenter {
removeFacilityFromRealm(r);
}
}
realm.close();
}
// Removes the given Facility from Realm
private void removeFacilityFromRealm(Facility facility) {
Realm realm = Realm.getDefaultInstance();
final String name = facility.getName();
realm.executeTransactionAsync((bgRealm) -> {
mRealm.executeTransactionAsync((bgRealm) -> {
RealmResults<Facility> results = bgRealm.where(Facility.class).equalTo("mName", name)
.findAll();
results.deleteAllFromRealm();
});
realm.close();
}
// Sets the open status and status duration of each facility in the Realm instance
private void updateOpenStatus() {
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(bgRealm -> {
mRealm.executeTransactionAsync(bgRealm -> {
List<Facility> facilities = bgRealm.where(Facility.class).findAll();
for(Facility f : facilities) {
f.setOpen(getOpenStatus(f, Calendar.getInstance()));
f.setStatusDuration(getStatusDuration(f, Calendar.getInstance()));
}
}, null, null);
realm.close();
}, null, null);
}
// Uses the device time to determine which facilities should be open
......@@ -156,7 +148,7 @@ public class MainPresenter {
// have to mess with the current day value, as Calender.DAY_OF_WEEK
// starts with Sunday as 1 and the Whats Open Api starts with Monday at 0
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
OpenTimes currentOpenTimes = null;
OpenTimes currentOpenTimes;
for(OpenTimes o : openTimesList) {
if (o.getStartDay() == currentDay && o.getEndDay() == currentDay) {
......
......@@ -62,6 +62,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
private FacilityPresenter mPresenter;
private Facility mFacility;
private boolean inEditMode;
private Realm mRealm;
@Override
......@@ -70,6 +71,9 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
setContentView(R.layout.activity_detail);
setUpAnimations();
// Get Realm instance
mRealm = Realm.getDefaultInstance();
getFacility(getIntent().getStringExtra("name"));
// Set up Presenter
......@@ -103,6 +107,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
protected void onDestroy() {
mPresenter.detachView();
super.onDestroy();
mRealm.close();
}
@Override
......@@ -159,9 +164,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
// Queries Realm for the facility matching the key
private void getFacility(String key) {
Realm realm = Realm.getDefaultInstance();
mFacility = realm.where(Facility.class).equalTo("mName", key).findFirst();
realm.close();
mFacility = mRealm.where(Facility.class).equalTo("mName", key).findFirst();
}
// Configures the toolbar title, actions, etc
......@@ -181,7 +184,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
public static TextView getTextViewTitle(Toolbar toolbar){
public static TextView getTextViewTitle(Toolbar toolbar) {
TextView textViewTitle = null;
for(int i = 0; i<toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
......@@ -218,10 +221,8 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
// Sets the notification button text to edit if a Notification exists
private void setNotificationStatus() {
Realm realm = Realm.getDefaultInstance();
NotificationSettings notificationSettings = realm.where(NotificationSettings.class)
NotificationSettings notificationSettings = mRealm.where(NotificationSettings.class)
.equalTo("name", mFacility.getName()).findFirst();
realm.close();
if (notificationSettings != null) {
inEditMode = true;
......
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