Skip to content

Commit 9483563

Browse files
lukstbitaniri
andauthored
Send count of unsynced items to analytics (#244)
* Send count of unsynced items to analytics * renamed variable to keep it consistent Co-authored-by: Irina Borozan <anirib@gmail.com>
1 parent 96054bb commit 9483563

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

app/src/main/java/ro/code4/monitorizarevot/ui/forms/FormsListFragment.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ class FormsListFragment : ViewModelFragment<FormsViewModel>() {
5050
formAdapter.items = it
5151
updateSyncSuccessfulNotice()
5252
})
53-
viewModel.syncVisibility().observe(viewLifecycleOwner, Observer {
54-
syncGroup.visibility = it
53+
viewModel.unSyncedDataCount().observe(viewLifecycleOwner, Observer {
54+
syncGroup.visibility = if (it > 0) View.VISIBLE else View.GONE
5555
updateSyncSuccessfulNotice()
5656
})
5757

5858
viewModel.setTitle(getString(R.string.title_forms_list))
5959

6060
syncButton.setOnClickListener {
61-
// TODO send number of unsynced items
62-
logAnalyticsEvent(Event.MANUAL_SYNC, Param(ParamKey.NUMBER_NOT_SYNCED, 0))
61+
val unSyncedCount = viewModel.unSyncedDataCount().value ?: 0
62+
logAnalyticsEvent(Event.MANUAL_SYNC, Param(ParamKey.NUMBER_NOT_SYNCED, unSyncedCount))
6363

6464
if (!mContext.isOnline()) {
6565
Snackbar.make(
@@ -91,15 +91,13 @@ class FormsListFragment : ViewModelFragment<FormsViewModel>() {
9191
* forms will be loaded).
9292
*/
9393
private fun updateSyncSuccessfulNotice() {
94-
val visibilityOfSyncBtn = viewModel.syncVisibility().value
9594
val areFormsVisible = viewModel.forms().value?.let { true } ?: false
96-
visibilityOfSyncBtn?.let {
97-
when (it) {
95+
viewModel.unSyncedDataCount().value?.let { unSyncedCount ->
96+
when (if (unSyncedCount > 0) View.VISIBLE else View.GONE) {
9897
View.VISIBLE -> syncSuccessGroup.visibility = View.GONE
9998
View.GONE -> syncSuccessGroup.visibility =
10099
if (areFormsVisible) View.VISIBLE else View.GONE
101100
}
102-
Unit
103101
}
104102
}
105103
}

app/src/main/java/ro/code4/monitorizarevot/ui/forms/FormsViewModel.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ro.code4.monitorizarevot.ui.forms
22

33
import android.annotation.SuppressLint
4-
import android.view.View
54
import androidx.lifecycle.LiveData
65
import androidx.lifecycle.MediatorLiveData
76
import androidx.lifecycle.MutableLiveData
@@ -26,7 +25,7 @@ class FormsViewModel : BaseFormViewModel() {
2625
private val formsLiveData = MutableLiveData<ArrayList<ListItem>>()
2726
private val selectedFormLiveData = MutableLiveData<FormDetails>()
2827
private val selectedQuestionLiveData = MutableLiveData<Pair<FormDetails, Question>>()
29-
private val syncVisibilityLiveData = MediatorLiveData<Int>()
28+
private val unSyncedDataCountLiveData = MediatorLiveData<Int>()
3029
private val navigateToNotesLiveData = MutableLiveData<Question?>()
3130
private val pollingStationLiveData = MutableLiveData<PollingStationInfo>()
3231

@@ -36,31 +35,23 @@ class FormsViewModel : BaseFormViewModel() {
3635
}
3736

3837
private fun subscribe() {
39-
4038
val notSyncedQuestionsCount = repository.getNotSyncedQuestions()
4139
val notSyncedNotesCount = repository.getNotSyncedNotes()
4240
val notSyncedPollingStationsCount = repository.getNotSyncedPollingStationsCount()
4341
fun update() {
44-
syncVisibilityLiveData.value =
45-
if ((notSyncedQuestionsCount.value ?: 0) + (notSyncedNotesCount.value
46-
?: 0) + (notSyncedPollingStationsCount.value ?: 0) > 0
47-
) View.VISIBLE else View.GONE
48-
}
49-
syncVisibilityLiveData.addSource(notSyncedQuestionsCount) {
50-
update()
51-
}
52-
syncVisibilityLiveData.addSource(notSyncedNotesCount) {
53-
update()
54-
}
55-
syncVisibilityLiveData.addSource(notSyncedPollingStationsCount) {
56-
update()
42+
unSyncedDataCountLiveData.value =
43+
(notSyncedQuestionsCount.value ?: 0) + (notSyncedNotesCount.value ?: 0) +
44+
(notSyncedPollingStationsCount.value ?: 0)
5745
}
46+
unSyncedDataCountLiveData.addSource(notSyncedQuestionsCount) { update() }
47+
unSyncedDataCountLiveData.addSource(notSyncedNotesCount) { update() }
48+
unSyncedDataCountLiveData.addSource(notSyncedPollingStationsCount) { update() }
5849

5950
disposables.add(Observable.combineLatest(
6051
repository.getAnswers(countyCode, pollingStationNumber),
6152
repository.getFormsWithQuestions(),
62-
BiFunction<List<AnsweredQuestionPOJO>, List<FormWithSections>, Pair<List<AnsweredQuestionPOJO>, List<FormWithSections>>> {
63-
t1, t2 -> Pair(t1, t2)
53+
BiFunction<List<AnsweredQuestionPOJO>, List<FormWithSections>, Pair<List<AnsweredQuestionPOJO>, List<FormWithSections>>> { t1, t2 ->
54+
Pair(t1, t2)
6455
}
6556
).subscribe {
6657
processList(it.first, it.second)
@@ -136,7 +127,7 @@ class FormsViewModel : BaseFormViewModel() {
136127
selectedQuestionLiveData.postValue(Pair(selectedFormLiveData.value!!, question))
137128
}
138129

139-
fun syncVisibility(): LiveData<Int> = syncVisibilityLiveData
130+
fun unSyncedDataCount(): LiveData<Int> = unSyncedDataCountLiveData
140131

141132
fun sync() {
142133
repository.syncData()

0 commit comments

Comments
 (0)