Commit bac38b9e authored by Robert Hitt's avatar Robert Hitt
Browse files

Fixed errors with showing incorrect Open status

- Caused by the current method only updating the open status
open successful network calls.
parent 79ba481d
...@@ -54,7 +54,7 @@ public class MainPresenter { ...@@ -54,7 +54,7 @@ public class MainPresenter {
Observable<List<Facility>> call = service.facilityList(); Observable<List<Facility>> call = service.facilityList();
call.subscribeOn(Schedulers.io()) call.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.map(facilities1 -> setStatus(facilities1)) .map(facilities1 -> setFavoriteStatus(facilities1))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe(new Subscriber<List<Facility>>() { .subscribe(new Subscriber<List<Facility>>() {
@Override @Override
...@@ -66,8 +66,8 @@ public class MainPresenter { ...@@ -66,8 +66,8 @@ public class MainPresenter {
public void onError(Throwable e) { public void onError(Throwable e) {
if(mMainView != null) if(mMainView != null)
mMainView.dismissProgressBar(); mMainView.dismissProgressBar();
Toast.makeText(mMainView.getContext(), "Error establishing connection, " + Toast.makeText(mMainView.getContext(), "Error getting data; " +
"schedules may be inaccurate.", Toast.LENGTH_LONG).show(); "schedules may be out of date.", Toast.LENGTH_LONG).show();
} }
@Override @Override
public void onNext(List<Facility> facilities) { public void onNext(List<Facility> facilities) {
...@@ -77,12 +77,11 @@ public class MainPresenter { ...@@ -77,12 +77,11 @@ public class MainPresenter {
} }
// Sets the favorite and open status of each Facility // Sets the favorite and open status of each Facility
private List<Facility> setStatus(List<Facility> facilities) { private List<Facility> setFavoriteStatus(List<Facility> facilities) {
for(Facility facility : facilities) { for(Facility facility : facilities) {
// Query SharedReferences for each Facility's favorite status. defaults to false // Query SharedReferences for each Facility's favorite status. defaults to false
facility.setFavorited(pref.getBoolean(facility.getName()+"FavoriteStatus", false)); facility.setFavorited(pref.getBoolean(facility.getName()+"FavoriteStatus", false));
facility.setOpen(getOpenStatus(facility, Calendar.getInstance()));
} }
return facilities; return facilities;
...@@ -95,6 +94,18 @@ public class MainPresenter { ...@@ -95,6 +94,18 @@ public class MainPresenter {
realm.close(); realm.close();
} }
// Sets the open status of each facility in the Realm instance
public void updateOpenStatus() {
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(bgRealm -> {
List<Facility> facilities = bgRealm.where(Facility.class).findAll();
for(Facility f : facilities) {
f.setOpen(getOpenStatus(f, Calendar.getInstance()));
}
}, null, null);
realm.close();
}
// Uses the device time to determine which facilities should be open // Uses the device time to determine which facilities should be open
public boolean getOpenStatus(Facility facility, Calendar now) { public boolean getOpenStatus(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now); RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now);
......
...@@ -45,6 +45,7 @@ public class MainActivity extends AppCompatActivity implements MainView { ...@@ -45,6 +45,7 @@ public class MainActivity extends AppCompatActivity implements MainView {
// Reload facility data // Reload facility data
mPresenter.loadFacilities(); mPresenter.loadFacilities();
mPresenter.updateOpenStatus();
// Configure toolbar // Configure toolbar
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar); Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
......
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