Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
refactor(desktop): refactor desktop repository
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed May 17, 2023
1 parent 27301ae commit 9fc031d
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,131 +4,103 @@ import com.intelligentbackpack.accessdomain.entities.User
import com.intelligentbackpack.desktopdomain.entities.Book
import com.intelligentbackpack.desktopdomain.entities.Desktop
import com.intelligentbackpack.desktopdomain.entities.SchoolSupply
import com.intelligentbackpack.desktopdomain.exception.BackpackNotAssociatedException
import com.intelligentbackpack.desktopdomain.repository.DesktopDomainRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext

class DesktopDomainRepositoryImpl(
private val desktopLocalDataSource: DesktopLocalDataSource,
private val desktopRemoteDataSource: DesktopRemoteDataSource,
) : DesktopDomainRepository {

override suspend fun getDesktop(user: User, success: (Desktop) -> Unit, error: (Exception) -> Unit) {
try {
private fun updateBackpackContent(rfidInsert: Set<String>) {
desktopLocalDataSource.putSchoolSuppliesInBackpack(rfidInsert)
val allSupplyRfid = desktopLocalDataSource.getAllSchoolSupplies().map { it.rfidCode }.toSet()
desktopLocalDataSource.takeSchoolSuppliesFromBackpack(allSupplyRfid - rfidInsert)
}

override suspend fun downloadDesktop(user: User): Desktop =
withContext(Dispatchers.IO) {
desktopRemoteDataSource.getDesktop(user)
.let { remoteDesktop ->
.also { remoteDesktop ->
remoteDesktop.schoolSupplies.forEach {
desktopLocalDataSource.addSchoolSupply(it)
}.let {
Desktop.create(
desktopLocalDataSource.getAllSchoolSupplies(),
desktopLocalDataSource.getSchoolSupplyInBackpack()
)
}.also { desktop ->
desktop.backpack?.let { backpack ->
desktopLocalDataSource
.putSchoolSuppliesInBackpack(
desktop.schoolSuppliesInBackpack.map { it.rfidCode }
.toSet()
)
desktopLocalDataSource
.takeSchoolSuppliesFromBackpack(
(desktop.schoolSupplies - desktop.schoolSuppliesInBackpack)
.map { it.rfidCode }
.toSet()
)
desktopLocalDataSource.associateBackpack(backpack)
}
remoteDesktop.backpack?.let { backpack ->
updateBackpackContent(remoteDesktop.schoolSuppliesInBackpack.map { it.rfidCode }.toSet())
desktopLocalDataSource.associateBackpack(backpack)
} ?: run {
updateBackpackContent(emptySet())
desktopLocalDataSource.getBackpack()?.let {
desktopLocalDataSource.disassociateBackpack()
}
success(desktop)
}
}
} catch (e: Exception) {
error(e)
}
}

override suspend fun addSchoolSupply(
user: User,
schoolSupply: SchoolSupply,
success: (Set<SchoolSupply>) -> Unit,
error: (Exception) -> Unit
) {
try {
override suspend fun getDesktop(user: User): Desktop =
withContext(Dispatchers.IO) {
Desktop.create(
desktopLocalDataSource.getAllSchoolSupplies(),
desktopLocalDataSource.getSchoolSupplyInBackpack(),
desktopLocalDataSource.getBackpack()
)
}


override suspend fun addSchoolSupply(user: User, schoolSupply: SchoolSupply) =
withContext(Dispatchers.IO) {
desktopLocalDataSource.addSchoolSupply(schoolSupply)
desktopRemoteDataSource.addSchoolSupply(user, schoolSupply)
success(desktopLocalDataSource.getAllSchoolSupplies())
} catch (e: Exception) {
error(e)
desktopLocalDataSource.getAllSchoolSupplies()
}
}

override suspend fun getBook(isbn: String, success: (Book?) -> Unit, error: (Exception) -> Unit) {
try {
desktopLocalDataSource
.getBook(isbn)
?.let { success(it) }
?: run {
success(
desktopRemoteDataSource.getBook(isbn)
?.also { remoteBook ->
desktopLocalDataSource
.addBook(remoteBook)
})
}
} catch (e: Exception) {
error(e)
override suspend fun getBook(isbn: String): Book? =
withContext(Dispatchers.IO) {
desktopLocalDataSource.getBook(isbn)
?: desktopRemoteDataSource.getBook(isbn)
?.also { remoteBook ->
desktopLocalDataSource.addBook(remoteBook)
}
}
}

override suspend fun logoutDesktop(user: User, success: () -> Unit, error: (Exception) -> Unit) {
try {
override suspend fun logoutDesktop(user: User) =
withContext(Dispatchers.IO) {
desktopLocalDataSource.deleteDesktop()
success()
} catch (e: Exception) {
error(e)
}
}

override fun subscribeToBackpack(user: User): Flow<Set<String>> {
val backpack = desktopLocalDataSource.getBackpack()
return desktopRemoteDataSource.subscribeToBackpackChanges(user, backpack).map { it.getOrDefault(setOf()) }
}

override suspend fun putSchoolSuppliesInBackpack(
rfid: Set<String>,
) {
desktopLocalDataSource.putSchoolSuppliesInBackpack(rfid)
}

override suspend fun takeSchoolSuppliesFromBackpack(
rfid: Set<String>,
) {
desktopLocalDataSource.takeSchoolSuppliesFromBackpack(rfid)
}
override suspend fun subscribeToBackpack(user: User): Flow<Set<String>> =
withContext(Dispatchers.IO) {
desktopLocalDataSource.getBackpack()?.let { backpack ->
desktopRemoteDataSource.subscribeToBackpackChanges(user, backpack)
.flowOn(Dispatchers.IO)
.map { it.getOrDefault(setOf()) }
.map { rfidCodes ->
val taken = desktopLocalDataSource
.getSchoolSupplyInBackpack().map { it.rfidCode }.toSet() - rfidCodes
desktopLocalDataSource.putSchoolSuppliesInBackpack(rfidCodes)
desktopLocalDataSource.takeSchoolSuppliesFromBackpack(taken)
rfidCodes
}
} ?: throw BackpackNotAssociatedException()
}

override suspend fun associateBackpack(user: User, hash: String, success: (String) -> Unit, error: (Exception) -> Unit) {
try {
override suspend fun associateBackpack(user: User, hash: String) =
withContext(Dispatchers.IO) {
val newHash = desktopRemoteDataSource.associateBackpack(user, hash)
desktopLocalDataSource.associateBackpack(newHash)
success(newHash)
} catch (e: Exception) {
error(e)
newHash
}
}

override suspend fun disassociateBackpack(
user: User,
hash: String,
success: (String) -> Unit,
error: (Exception) -> Unit
) {
try {
override suspend fun disassociateBackpack(user: User, hash: String) =
withContext(Dispatchers.IO) {
desktopRemoteDataSource.disassociateBackpack(user, hash)
desktopLocalDataSource.disassociateBackpack()
desktopLocalDataSource.removeAllSchoolSuppliesFromBackpack()
success(hash)
} catch (e: Exception) {
error(e)
hash
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,73 @@ import kotlinx.coroutines.flow.Flow
*/
interface DesktopDomainRepository {

/**
* Downloads the desktop.
*
* @param user the user that wants the desktop
* @return the desktop
*/

suspend fun downloadDesktop(user: User): Desktop

/**
* Gets the desktop.
*
* @param success The success callback.
* @param error The error callback.
* @param user the user that wants the desktop
* @return the desktop
*/
suspend fun getDesktop(user: User, success: (Desktop) -> Unit, error: (Exception) -> Unit)
suspend fun getDesktop(user: User): Desktop

/**
* Adds a school supply to the desktop.
*
* @param user The user that adds the school supply.
* @param schoolSupply The school supply to add.
* @param success The success callback with all the school supplies.
* @param error The error callback.
* @return The set of school supplies.
*/
suspend fun addSchoolSupply(
user: User,
schoolSupply: SchoolSupply,
success: (Set<SchoolSupply>) -> Unit,
error: (Exception) -> Unit
)
): Set<SchoolSupply>

/**
* Gets a book by its ISBN.
*
* @param isbn The ISBN of the book.
* @param success The success callback with the book if it exists.
* @param error The error callback.
* @return The book or null if it doesn't exist.
*/
suspend fun getBook(isbn: String, success: (Book?) -> Unit, error: (Exception) -> Unit)
suspend fun getBook(isbn: String): Book?

/**
* Deletes the desktop and all its school supplies.
*
* @param success The success callback.
* @param error The error callback.
* @param user The user that logout the desktop.
*/
suspend fun logoutDesktop(user: User, success: () -> Unit, error: (Exception) -> Unit)
suspend fun logoutDesktop(user: User)

/**
* Subscribes to the backpack.
*
* @param user the user that subscribe to the backpack
* @return a [Flow] of [Set] of [String] representing the rfid of the school supplies in the backpack
*/
fun subscribeToBackpack(user: User): Flow<Set<String>>

/**
* Put a set of school supplies in the backpack
*
* @param rfid the set of rfid of school supplies to put in the backpack
*/
suspend fun putSchoolSuppliesInBackpack(rfid: Set<String>)

/**
* Take a set of school supplies from the backpack
*
* @param rfid the set of rfid of school supplies to take from the backpack
*/
suspend fun takeSchoolSuppliesFromBackpack(rfid: Set<String>)
suspend fun subscribeToBackpack(user: User): Flow<Set<String>>

/**
* Associate the backpack to the desktop
*
* @param user the user that connect the backpack
* @param hash the hash of the backpack to associate
* @param success The success callback with the backpack hash.
* @param error The error callback.
* @return the hash of the backpack
*/
suspend fun associateBackpack(user: User, hash: String, success: (String) -> Unit, error: (Exception) -> Unit)
suspend fun associateBackpack(user: User, hash: String): String

/**
* Disassociate the backpack from the desktop
*
* @param user the user that disconnect the backpack
* @param hash the hash of the backpack to disassociate
* @param success The success callback with the backpack hash.
* @param error The error callback.
* @return the hash of the backpack
*/
suspend fun disassociateBackpack(user: User, hash: String, success: (String) -> Unit, error: (Exception) -> Unit)
suspend fun disassociateBackpack(user: User, hash: String): String
}
Loading

0 comments on commit 9fc031d

Please sign in to comment.