refactor BoardRepository

This commit is contained in:
Raffaele Mignone 2019-12-03 19:48:29 +01:00
parent a645d5f64b
commit 44775d0619
Signed by: norangebit
GPG Key ID: F5255658CB220573

View File

@ -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,6 +81,13 @@ 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)
})
}
private fun insertBoardCallback(
response: Response<it.unisannio.ding.ids.wedroid.wrapper.entity.Board>,
title: String
) { ) {
if (!response.isSuccessful) { if (!response.isSuccessful) {
logNetworkError("${response.code()} ${response.message()}") logNetworkError("${response.code()} ${response.message()}")
@ -65,38 +105,6 @@ class BoardRepository(
dao.insert(Board(board.id, title)) dao.insert(Board(board.id, title))
} }
} }
})
}
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>>
) {
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 deleteBoard(id: String) { fun deleteBoard(id: String) {
service.deleteBoard(id).enqueue( service.deleteBoard(id).enqueue(
@ -105,7 +113,17 @@ class BoardRepository(
logNetworkError(t.message) logNetworkError(t.message)
} }
override fun onResponse(call: Call<Void>, response: Response<Void>) { override fun onResponse(
call: Call<Void>,
response: Response<Void>
) = deleteBoardCallback(response, id)
})
}
private fun deleteBoardCallback(
response: Response<Void>,
id: String
) {
if (!response.isSuccessful) { if (!response.isSuccessful) {
logNetworkError("${response.code()}, ${response.message()}") logNetworkError("${response.code()}, ${response.message()}")
return return
@ -115,8 +133,6 @@ class BoardRepository(
dao.delete(Board(id)) dao.delete(Board(id))
} }
} }
})
}
private fun addNewBoardToDb(boards: Collection<Board>) { private fun addNewBoardToDb(boards: Collection<Board>) {
boards.forEach { boards.forEach {