Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send count of unsynced items to analytics #244

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ class FormsListFragment : ViewModelFragment<FormsViewModel>() {
formAdapter.items = it
updateSyncSuccessfulNotice()
})
viewModel.syncVisibility().observe(viewLifecycleOwner, Observer {
syncGroup.visibility = it
viewModel.unSyncedDataCount().observe(viewLifecycleOwner, Observer {
syncGroup.visibility = if (it > 0) View.VISIBLE else View.GONE
updateSyncSuccessfulNotice()
})

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

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

if (!mContext.isOnline()) {
Snackbar.make(
Expand Down Expand Up @@ -91,15 +91,13 @@ class FormsListFragment : ViewModelFragment<FormsViewModel>() {
* forms will be loaded).
*/
private fun updateSyncSuccessfulNotice() {
val visibilityOfSyncBtn = viewModel.syncVisibility().value
val areFormsVisible = viewModel.forms().value?.let { true } ?: false
visibilityOfSyncBtn?.let {
when (it) {
viewModel.unSyncedDataCount().value?.let { unSyncedCount ->
when (if (unSyncedCount > 0) View.VISIBLE else View.GONE) {
View.VISIBLE -> syncSuccessGroup.visibility = View.GONE
View.GONE -> syncSuccessGroup.visibility =
if (areFormsVisible) View.VISIBLE else View.GONE
}
Unit
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ro.code4.monitorizarevot.ui.forms

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
Expand All @@ -26,7 +25,7 @@ class FormsViewModel : BaseFormViewModel() {
private val formsLiveData = MutableLiveData<ArrayList<ListItem>>()
private val selectedFormLiveData = MutableLiveData<FormDetails>()
private val selectedQuestionLiveData = MutableLiveData<Pair<FormDetails, Question>>()
private val syncVisibilityLiveData = MediatorLiveData<Int>()
private val unSyncedDataCountLiveData = MediatorLiveData<Int>()
private val navigateToNotesLiveData = MutableLiveData<Question?>()
private val pollingStationLiveData = MutableLiveData<PollingStationInfo>()

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

private fun subscribe() {

val notSyncedQuestionsCount = repository.getNotSyncedQuestions()
val notSyncedNotesCount = repository.getNotSyncedNotes()
val notSyncedPollingStationsCount = repository.getNotSyncedPollingStationsCount()
fun update() {
syncVisibilityLiveData.value =
if ((notSyncedQuestionsCount.value ?: 0) + (notSyncedNotesCount.value
?: 0) + (notSyncedPollingStationsCount.value ?: 0) > 0
) View.VISIBLE else View.GONE
}
syncVisibilityLiveData.addSource(notSyncedQuestionsCount) {
update()
}
syncVisibilityLiveData.addSource(notSyncedNotesCount) {
update()
}
syncVisibilityLiveData.addSource(notSyncedPollingStationsCount) {
update()
unSyncedDataCountLiveData.value =
(notSyncedQuestionsCount.value ?: 0) + (notSyncedNotesCount.value ?: 0) +
(notSyncedPollingStationsCount.value ?: 0)
}
unSyncedDataCountLiveData.addSource(notSyncedQuestionsCount) { update() }
unSyncedDataCountLiveData.addSource(notSyncedNotesCount) { update() }
unSyncedDataCountLiveData.addSource(notSyncedPollingStationsCount) { update() }

disposables.add(Observable.combineLatest(
repository.getAnswers(countyCode, pollingStationNumber),
repository.getFormsWithQuestions(),
BiFunction<List<AnsweredQuestionPOJO>, List<FormWithSections>, Pair<List<AnsweredQuestionPOJO>, List<FormWithSections>>> {
t1, t2 -> Pair(t1, t2)
BiFunction<List<AnsweredQuestionPOJO>, List<FormWithSections>, Pair<List<AnsweredQuestionPOJO>, List<FormWithSections>>> { t1, t2 ->
Pair(t1, t2)
}
).subscribe {
processList(it.first, it.second)
Expand Down Expand Up @@ -136,7 +127,7 @@ class FormsViewModel : BaseFormViewModel() {
selectedQuestionLiveData.postValue(Pair(selectedFormLiveData.value!!, question))
}

fun syncVisibility(): LiveData<Int> = syncVisibilityLiveData
fun unSyncedDataCount(): LiveData<Int> = unSyncedDataCountLiveData

fun sync() {
repository.syncData()
Expand Down