[go: nahoru, domu]

Skip to content

Commit

Permalink
Add usecases for conversation, contact retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
oblakr24 committed Jun 28, 2023
1 parent 4fbdaea commit a7b2e53
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.cash.molecule.RecompositionClock
import app.cash.molecule.launchMolecule
import com.rokoblak.chatbackup.data.Conversation
import com.rokoblak.chatbackup.di.AppScope
import com.rokoblak.chatbackup.domain.usecases.ConversationUseCase
import com.rokoblak.chatbackup.navigation.RouteNavigator
import com.rokoblak.chatbackup.services.ConversationUIMapper
import com.rokoblak.chatbackup.services.ConversationsRepo
Expand All @@ -26,8 +27,8 @@ import javax.inject.Inject
@HiltViewModel
class ConversationViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val routeNavigator: RouteNavigator,
conversationsRepo: ConversationsRepo,
routeNavigator: RouteNavigator,
conversationUseCase: ConversationUseCase,
private val uiMapper: ConversationUIMapper,
private val smsSender: SMSSender,
) :
Expand All @@ -37,7 +38,7 @@ class ConversationViewModel @Inject constructor(

private val routeInput = ConversationRoute.getIdFrom(savedStateHandle)

private val convsFlow = conversationsRepo.conversationFor(
private val convsFlow = conversationUseCase.conversationFor(
contactId = routeInput.resolvedContactId,
number = routeInput.address,
isImport = routeInput.isImport
Expand All @@ -56,7 +57,7 @@ class ConversationViewModel @Inject constructor(
@SuppressLint("ComposableNaming")
@Composable
private fun ConversationPresenter(
convFlow: Flow<Conversation?>,
convFlow: Flow<Conversation>,
): ConversationScreenUIState {
val conv = convFlow.collectAsState(initial = null).value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel
import com.rokoblak.chatbackup.navigation.NavRoute

private const val KEY_INPUT_ID = "key-chat-id"

object CreateChatRoute : NavRoute<CreateChatViewModel> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.rokoblak.chatbackup.domain.usecases

import com.rokoblak.chatbackup.data.Contact
import com.rokoblak.chatbackup.data.Conversation
import com.rokoblak.chatbackup.services.ConversationsRepo
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.mapNotNull
import javax.inject.Inject

class ConversationUseCase @Inject constructor(
private val repo: ConversationsRepo,
private val contactsUseCase: RetrieveContactUseCase,
) {

fun conversationFor(contactId: String?, number: String, isImport: Boolean) = flow {
val matching = contactsUseCase.resolveContact(number)
val contact = matching ?: Contact(
name = null,
orgNumber = number,
)
if (isImport) {
val conv =
repo.importedConversations?.resolveConv(contactId = contactId, number = number)
emit(conv ?: Conversation(contact, emptyList()))
} else {
emitAll(repo.deviceConvsFlow.mapNotNull {
it?.resolveConv(contactId = contactId, number = number) ?: Conversation(
contact,
emptyList()
)
})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.rokoblak.chatbackup.domain.usecases

import com.rokoblak.chatbackup.AppConstants
import com.rokoblak.chatbackup.commonui.PreviewDataUtils.obfuscate
import com.rokoblak.chatbackup.data.Contact
import com.rokoblak.chatbackup.services.ContactsRepository
import javax.inject.Inject

class RetrieveContactUseCase @Inject constructor(
private val repo: ContactsRepository,
) {

suspend fun resolveContact(number: String): Contact? {
val contacts = repo.retrieveContacts()
val matching = contacts.firstOrNull { it.number == number }
if (matching != null) {
return matching
}
val resolvedName = repo.resolveContactName(number)
if (resolvedName != null) {
val contact = Contact(
name = resolvedName,
orgNumber = number,
).let { if (AppConstants.OBFUSCATE) it.obfuscate() else it }
return contact
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,15 @@ class ContactsRepository @Inject constructor(
contacts.filter { it.matches(query) }
}

suspend fun resolveContact(number: String): Contact? {
val contacts = retrieveContacts()
val matching = contacts.firstOrNull { it.number == number }
if (matching != null) {
return matching
}
val resolvedName = resolveContactName(number)
if (resolvedName != null) {
val contact = Contact(
name = resolvedName,
orgNumber = number,
).let { if (AppConstants.OBFUSCATE) it.obfuscate() else it }
return contact
}
return null
}

private suspend fun retrieveContacts(): List<Contact> {
suspend fun retrieveContacts(): List<Contact> {
return withTimeoutOrNull(6000L) {
contactsFlow.mapNotNull { op ->
(op as? OperationResult.Done)?.data.takeIf { it?.isNotEmpty() == true }
}.firstOrNull()
} ?: emptyList()
}

private fun resolveContactName(number: String): String? {
fun resolveContactName(number: String): String? {
val lookupUri =
Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.rokoblak.chatbackup.services

import com.rokoblak.chatbackup.data.Contact
import com.rokoblak.chatbackup.data.Conversation
import com.rokoblak.chatbackup.data.Conversations
import com.rokoblak.chatbackup.data.PhoneType
import com.rokoblak.chatbackup.data.model.OperationResult
import com.rokoblak.chatbackup.di.AppScope
import com.rokoblak.chatbackup.di.SMSEvent
Expand All @@ -22,11 +20,11 @@ import javax.inject.Singleton
class ConversationsRepo @Inject constructor(
private val appScope: AppScope,
private val smsRetriever: MessagesRetriever,
private val contactsRepo: ContactsRepository,
) {
private val scope = CoroutineScope(Dispatchers.Main + Job())

private var importedConversations: Conversations? = null
var importedConversations: Conversations? = null
private set

fun setImportedConversations(conversations: Conversations) {
importedConversations = conversations
Expand All @@ -44,25 +42,6 @@ class ConversationsRepo @Inject constructor(
}
}

fun conversationFor(contactId: String?, number: String, isImport: Boolean) = flow {
val matching = contactsRepo.resolveContact(number)
val contact = matching ?: Contact(
name = null,
orgNumber = number,
)
if (isImport) {
val conv = importedConversations?.resolveConv(contactId = contactId, number = number)
emit(conv ?: Conversation(contact, emptyList()))
} else {
emitAll(deviceConvsFlow.mapNotNull {
it?.resolveConv(contactId = contactId, number = number) ?: Conversation(
contact,
emptyList()
)
})
}
}

private val loadTypeFlow = MutableStateFlow(ConvLoadType(emitInitial = true))

private val deletionsFlow = MutableStateFlow<Set<String>?>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.rokoblak.chatbackup.data.Message
import com.rokoblak.chatbackup.data.MinimalContact
import com.rokoblak.chatbackup.data.model.OperationResult
import com.rokoblak.chatbackup.di.AppScope
import com.rokoblak.chatbackup.domain.usecases.RetrieveContactUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
Expand All @@ -27,15 +28,15 @@ import javax.inject.Inject
class MessagesRetriever @Inject constructor(
private val appScope: AppScope,
private val builder: ConversationBuilder,
private val contactsRepo: ContactsRepository,
private val retrieveContactUseCase: RetrieveContactUseCase,
) {

suspend fun retrieveMessages(): Conversations = withContext(Dispatchers.IO) {
val messages = retrieveSMSMessages() + retrieveMMSMessages()

if (messages.isNotEmpty()) {
builder.groupMessages(messages, contactRetriever = { num ->
contactsRepo.resolveContact(num)
retrieveContactUseCase.resolveContact(num)
})
} else {
Conversations(emptyMap(), emptyList())
Expand Down

0 comments on commit a7b2e53

Please sign in to comment.