Improve logic flow of loading milestones
@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
+import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
@@ -44,14 +45,14 @@ public class MilestoneDialog extends BaseProgressDialog {
private static final String TAG = "MilestoneDialog";
- private ArrayList<Milestone> repositoryMilestones;
-
private final int requestCode;
private final BaseActivity activity;
private final Repository repository;
+ private final Single<List<Milestone>> milestoneSingle;
+
/**
* Create dialog helper to display milestones
*
@@ -65,21 +66,12 @@ public class MilestoneDialog extends BaseProgressDialog {
this.activity = activity;
this.requestCode = requestCode;
this.repository = repository;
- }
- private void load(final Milestone selectedMilestone) {
- getPageAndNext(1)
+ milestoneSingle = getPageAndNext(1)
.flatMap(page -> Observable.fromIterable(page.items()))
.toSortedList((m1, m2) -> CASE_INSENSITIVE_ORDER.compare(m1.title(), m2.title()))
.compose(RxProgress.bindToLifecycle(activity, R.string.loading_milestones))
- .subscribe(milestones -> {
- repositoryMilestones = (ArrayList) milestones;
-
- show(selectedMilestone);
- }, error -> {
- Log.e(TAG, "Exception loading milestones", error);
- ToastUtils.show(activity, error, R.string.error_milestones_load);
- });
+ .cache();
}
private Observable<Page<Milestone>> getPageAndNext(int i) {
@@ -104,15 +96,11 @@ public class MilestoneDialog extends BaseProgressDialog {
* @param selectedMilestone
*/
public void show(Milestone selectedMilestone) {
- if (repositoryMilestones == null) {
- load(selectedMilestone);
- return;
- }
-
+ milestoneSingle.subscribe(milestones -> {
int checked = -1;
if (selectedMilestone != null) {
- for (int i = 0; i < repositoryMilestones.size(); i++) {
- if (selectedMilestone.number() == repositoryMilestones.get(i).number()) {
+ for (int i = 0; i < milestones.size(); i++) {
+ if (selectedMilestone.number() == milestones.get(i).number()) {
checked = i;
break;
}
@@ -120,6 +108,10 @@ public class MilestoneDialog extends BaseProgressDialog {
}
MilestoneDialogFragment.show(activity, requestCode,
activity.getString(R.string.select_milestone), null,
- repositoryMilestones, checked);
+ new ArrayList<>(milestones), checked);
+ }, error -> {
+ Log.e(TAG, "Exception loading milestones", error);
+ ToastUtils.show(activity, error, R.string.error_milestones_load);
+ });
}
}
