/
HomeScreenViewModel.java
162 lines (139 loc) · 5.47 KB
/
HomeScreenViewModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gnd.ui.home;
import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.LiveDataReactiveStreams;
import androidx.lifecycle.MutableLiveData;
import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.feature.Feature;
import com.google.android.gnd.model.feature.Point;
import com.google.android.gnd.model.form.Form;
import com.google.android.gnd.repository.DataRepository;
import com.google.android.gnd.repository.Persistable;
import com.google.android.gnd.ui.common.AbstractViewModel;
import com.google.android.gnd.ui.common.Navigator;
import com.google.android.gnd.ui.common.SharedViewModel;
import com.google.android.gnd.ui.common.SingleLiveEvent;
import com.google.android.gnd.ui.map.MapMarker;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import javax.annotation.Nullable;
import javax.inject.Inject;
@SharedViewModel
public class HomeScreenViewModel extends AbstractViewModel {
private static final String TAG = HomeScreenViewModel.class.getSimpleName();
private final DataRepository dataRepository;
private final Navigator navigator;
/** The state and value of the currently active project (loading, loaded, etc.). */
private final LiveData<Persistable<Project>> activeProject;
private final PublishSubject<Feature> addFeatureClicks;
// TODO: Move into MapContainersViewModel
private final SingleLiveEvent<Point> addFeatureDialogRequests;
// TODO: Move into FeatureSheetViewModel.
private final SingleLiveEvent<Void> openDrawerRequests;
private final MutableLiveData<FeatureSheetState> featureSheetState;
@Nullable private Form selectedForm;
@Inject
HomeScreenViewModel(DataRepository dataRepository, Navigator navigator) {
this.dataRepository = dataRepository;
this.addFeatureDialogRequests = new SingleLiveEvent<>();
this.openDrawerRequests = new SingleLiveEvent<>();
this.featureSheetState = new MutableLiveData<>();
this.activeProject =
LiveDataReactiveStreams.fromPublisher(dataRepository.getActiveProjectOnceAndStream());
this.navigator = navigator;
this.addFeatureClicks = PublishSubject.create();
disposeOnClear(
addFeatureClicks
.switchMapSingle(
newFeature ->
dataRepository
.saveFeature(newFeature)
.toSingleDefault(newFeature)
.doOnError(this::onAddFeatureError)
.onErrorResumeNext(Single.never()) // Prevent from breaking upstream.
.subscribeOn(Schedulers.io()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::showFeatureSheet));
}
private void onAddFeatureError(Throwable throwable) {
// TODO: Show an error message to the user.
Log.e(TAG, "Couldn't add feature.", throwable);
}
public LiveData<Void> getOpenDrawerRequests() {
return openDrawerRequests;
}
public void openNavDrawer() {
openDrawerRequests.setValue(null);
}
public LiveData<Persistable<Project>> getActiveProject() {
return activeProject;
}
public LiveData<Point> getShowAddFeatureDialogRequests() {
return addFeatureDialogRequests;
}
public LiveData<FeatureSheetState> getFeatureSheetState() {
return featureSheetState;
}
// TODO: Remove extra indirection here?
public void onMarkerClick(MapMarker marker) {
marker.getFeature().ifPresent(this::showFeatureSheet);
}
private void showFeatureSheet(Feature feature) {
featureSheetState.setValue(FeatureSheetState.visible(feature));
}
public void onAddFeatureBtnClick(Point location) {
// TODO: Pause location updates while dialog is open.
addFeatureDialogRequests.setValue(location);
}
public void addFeature(Feature feature) {
addFeatureClicks.onNext(feature);
}
public void onBottomSheetHidden() {
featureSheetState.setValue(FeatureSheetState.hidden());
}
public void onFormChange(Form form) {
this.selectedForm = form;
}
public void addRecord() {
FeatureSheetState state = featureSheetState.getValue();
if (state == null) {
Log.e(TAG, "Missing featureSheetState");
return;
}
if (selectedForm == null) {
Log.e(TAG, "Missing form");
return;
}
Feature feature = state.getFeature();
navigator.addRecord(feature.getProject().getId(), feature.getId(), selectedForm.getId());
}
public void showBasemapSelector() {
navigator.showBasemapSelector();
}
/**
* Reactivates the last active project, emitting true once loaded, or false if no project was
* previously activated.
*/
public LiveData<Boolean> reactivateLastProject() {
// TODO: Handle errors activating project.
return LiveDataReactiveStreams.fromPublisher(
dataRepository.reactivateLastProject().toFlowable());
}
}