-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add usecases for conversation, contact retrieval
- Loading branch information
Showing
7 changed files
with
76 additions
and
49 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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/rokoblak/chatbackup/domain/usecases/ConversationUseCase.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,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() | ||
) | ||
}) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/rokoblak/chatbackup/domain/usecases/RetrieveContactUseCase.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,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 | ||
} | ||
} |
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