-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked ReadyToSendBanner to use viewmodel
- Loading branch information
1 parent
7bf40a6
commit d9a2217
Showing
6 changed files
with
131 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...app/src/main/java/org/odk/collect/android/instancemanagement/send/ReadyToSendViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.odk.collect.android.instancemanagement.send | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import org.odk.collect.async.Scheduler | ||
import org.odk.collect.forms.instances.Instance | ||
import org.odk.collect.forms.instances.InstancesRepository | ||
import java.util.function.Supplier | ||
|
||
class ReadyToSendViewModel( | ||
private val instancesRepository: InstancesRepository, | ||
private val scheduler: Scheduler, | ||
private val clock: Supplier<Long> | ||
) : ViewModel() { | ||
private val _data = MutableLiveData<Data>() | ||
val data: LiveData<Data> = _data | ||
|
||
fun init() { | ||
scheduler.immediate( | ||
background = { | ||
val sentInstances = instancesRepository.getAllByStatus(Instance.STATUS_SUBMITTED) | ||
val numberOfSentInstances = sentInstances.size | ||
val numberOfInstancesReadyToSend = instancesRepository.getCountByStatus( | ||
Instance.STATUS_COMPLETE, | ||
Instance.STATUS_SUBMISSION_FAILED | ||
) | ||
val lastInstanceSentTimeMillis = if (sentInstances.isNotEmpty()) { | ||
val lastSentInstance = sentInstances.maxBy { instance -> instance.lastStatusChangeDate } | ||
clock.get() - lastSentInstance.lastStatusChangeDate | ||
} else { | ||
0 | ||
} | ||
Data(numberOfInstancesReadyToSend, numberOfSentInstances, lastInstanceSentTimeMillis) | ||
}, | ||
foreground = { | ||
_data.value = it | ||
} | ||
) | ||
} | ||
|
||
open class Factory( | ||
private val instancesRepository: InstancesRepository, | ||
private val scheduler: Scheduler, | ||
private val clock: Supplier<Long> | ||
) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return ReadyToSendViewModel(instancesRepository, scheduler, clock) as T | ||
} | ||
} | ||
|
||
data class Data( | ||
val numberOfInstancesReadyToSend: Int, | ||
val numberOfSentInstances: Int, | ||
val lastInstanceSentTimeMillis: Long | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters