diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt index 8e7675d..92eb29b 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/data/repository/BoardRepository.kt @@ -29,6 +29,39 @@ class BoardRepository( synchronize() } + fun synchronize() { + service.getBoardsFromUser(reader.userId) + .enqueue(object : + Callback> { + override fun onFailure( + call: Call>, + t: Throwable + ) = logNetworkError(t.message) + + override fun onResponse( + call: Call>, + response: Response> + ) = synchronizeCallback(response) + }) + } + + private fun synchronizeCallback( + response: Response> + ) { + if (!response.isSuccessful) { + logNetworkError("${response.code()} ${response.message()}") + return + } + + // read boards from the body + val boards = (response.body() ?: return) + .map { it.convert() } + + addNewBoardToDb(boards) + + removeOldBoardsFromDb(boards) + } + fun insertBoard(title: String, isPrivate: Boolean, color: BoardBackgroundColor) { val permission = if (isPrivate) BoardPermission.PRIVATE else BoardPermission.PUBLIC @@ -48,54 +81,29 @@ class BoardRepository( override fun onResponse( call: Call, response: Response - ) { - if (!response.isSuccessful) { - logNetworkError("${response.code()} ${response.message()}") - return - } - - val board = response.body() - - if (board == null) { - logNetworkError("empty body") - return - } - - CoroutineScope(Dispatchers.IO).launch { - dao.insert(Board(board.id, title)) - } - } + ) = insertBoardCallback(response, title) }) } - fun synchronize() { - service.getBoardsFromUser(reader.userId) - .enqueue(object : - Callback> { + private fun insertBoardCallback( + response: Response, + title: String + ) { + if (!response.isSuccessful) { + logNetworkError("${response.code()} ${response.message()}") + return + } - override fun onFailure( - call: Call>, - t: Throwable - ) = logNetworkError(t.message) + val board = response.body() - override fun onResponse( - call: Call>, - response: Response> - ) { - if (!response.isSuccessful) { - logNetworkError("${response.code()} ${response.message()}") - return - } + if (board == null) { + logNetworkError("empty body") + return + } - // read boards from the body - val boards = (response.body() ?: return) - .map { it.convert() } - - addNewBoardToDb(boards) - - removeOldBoardsFromDb(boards) - } - }) + CoroutineScope(Dispatchers.IO).launch { + dao.insert(Board(board.id, title)) + } } fun deleteBoard(id: String) { @@ -105,19 +113,27 @@ class BoardRepository( logNetworkError(t.message) } - override fun onResponse(call: Call, response: Response) { - if (!response.isSuccessful) { - logNetworkError("${response.code()}, ${response.message()}") - return - } - - CoroutineScope(Dispatchers.IO).launch { - dao.delete(Board(id)) - } - } + override fun onResponse( + call: Call, + response: Response + ) = deleteBoardCallback(response, id) }) } + private fun deleteBoardCallback( + response: Response, + id: String + ) { + if (!response.isSuccessful) { + logNetworkError("${response.code()}, ${response.message()}") + return + } + + CoroutineScope(Dispatchers.IO).launch { + dao.delete(Board(id)) + } + } + private fun addNewBoardToDb(boards: Collection) { boards.forEach { CoroutineScope(Dispatchers.IO).launch {