refactor BoardRepository
This commit is contained in:
parent
a645d5f64b
commit
44775d0619
@ -29,6 +29,39 @@ class BoardRepository(
|
|||||||
synchronize()
|
synchronize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun synchronize() {
|
||||||
|
service.getBoardsFromUser(reader.userId)
|
||||||
|
.enqueue(object :
|
||||||
|
Callback<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>> {
|
||||||
|
override fun onFailure(
|
||||||
|
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>,
|
||||||
|
t: Throwable
|
||||||
|
) = logNetworkError(t.message)
|
||||||
|
|
||||||
|
override fun onResponse(
|
||||||
|
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>,
|
||||||
|
response: Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>
|
||||||
|
) = synchronizeCallback(response)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun synchronizeCallback(
|
||||||
|
response: Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>
|
||||||
|
) {
|
||||||
|
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) {
|
fun insertBoard(title: String, isPrivate: Boolean, color: BoardBackgroundColor) {
|
||||||
val permission = if (isPrivate) BoardPermission.PRIVATE else BoardPermission.PUBLIC
|
val permission = if (isPrivate) BoardPermission.PRIVATE else BoardPermission.PUBLIC
|
||||||
|
|
||||||
@ -48,54 +81,29 @@ class BoardRepository(
|
|||||||
override fun onResponse(
|
override fun onResponse(
|
||||||
call: Call<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>,
|
call: Call<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>,
|
||||||
response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>
|
response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>
|
||||||
) {
|
) = insertBoardCallback(response, title)
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun synchronize() {
|
private fun insertBoardCallback(
|
||||||
service.getBoardsFromUser(reader.userId)
|
response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>,
|
||||||
.enqueue(object :
|
title: String
|
||||||
Callback<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>> {
|
) {
|
||||||
|
if (!response.isSuccessful) {
|
||||||
|
logNetworkError("${response.code()} ${response.message()}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
override fun onFailure(
|
val board = response.body()
|
||||||
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>,
|
|
||||||
t: Throwable
|
|
||||||
) = logNetworkError(t.message)
|
|
||||||
|
|
||||||
override fun onResponse(
|
if (board == null) {
|
||||||
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>,
|
logNetworkError("empty body")
|
||||||
response: Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>>
|
return
|
||||||
) {
|
}
|
||||||
if (!response.isSuccessful) {
|
|
||||||
logNetworkError("${response.code()} ${response.message()}")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// read boards from the body
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val boards = (response.body() ?: return)
|
dao.insert(Board(board.id, title))
|
||||||
.map { it.convert() }
|
}
|
||||||
|
|
||||||
addNewBoardToDb(boards)
|
|
||||||
|
|
||||||
removeOldBoardsFromDb(boards)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteBoard(id: String) {
|
fun deleteBoard(id: String) {
|
||||||
@ -105,19 +113,27 @@ class BoardRepository(
|
|||||||
logNetworkError(t.message)
|
logNetworkError(t.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResponse(call: Call<Void>, response: Response<Void>) {
|
override fun onResponse(
|
||||||
if (!response.isSuccessful) {
|
call: Call<Void>,
|
||||||
logNetworkError("${response.code()}, ${response.message()}")
|
response: Response<Void>
|
||||||
return
|
) = deleteBoardCallback(response, id)
|
||||||
}
|
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
|
||||||
dao.delete(Board(id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun deleteBoardCallback(
|
||||||
|
response: Response<Void>,
|
||||||
|
id: String
|
||||||
|
) {
|
||||||
|
if (!response.isSuccessful) {
|
||||||
|
logNetworkError("${response.code()}, ${response.message()}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
dao.delete(Board(id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun addNewBoardToDb(boards: Collection<Board>) {
|
private fun addNewBoardToDb(boards: Collection<Board>) {
|
||||||
boards.forEach {
|
boards.forEach {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
Loading…
Reference in New Issue
Block a user