refactor: handle selected contacts in a local variable

This commit is contained in:
andrekir 2024-07-06 08:31:52 -03:00
parent 56d9f03748
commit 2b4b1d7683
3 changed files with 29 additions and 43 deletions

View file

@ -11,12 +11,9 @@ import com.geeksville.mesh.database.entity.Packet
import com.geeksville.mesh.repository.datastore.ChannelSetRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.updateAndGet
import kotlinx.coroutines.launch
import java.text.DateFormat
import java.util.Date
@ -53,21 +50,6 @@ class ContactsViewModel @Inject constructor(
private val packetRepository: PacketRepository,
) : ViewModel(), Logging {
private val _selectedContacts = MutableStateFlow(emptySet<String>())
val selectedContacts get() = _selectedContacts.asStateFlow()
fun updateSelectedContacts(contact: String) = _selectedContacts.updateAndGet {
if (it.contains(contact)) {
it.minus(contact)
} else {
it.plus(contact)
}
}
fun clearSelectedContacts() {
_selectedContacts.value = emptySet()
}
val contactList = combine(
nodeDB.myNodeInfo,
packetRepository.getContacts(),
@ -124,4 +106,4 @@ class ContactsViewModel @Inject constructor(
fun deleteContacts(contacts: List<String>) = viewModelScope.launch(Dispatchers.IO) {
packetRepository.deleteContacts(contacts)
}
}
}