From 7c37b183d70f87eee75e0432525b2b19d72ff5f9 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sat, 20 Aug 2022 21:11:04 +0200 Subject: [PATCH] Refactor functions --- .../repository/RepositoryImpl.kt | 43 +++++-------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index a29b924..a8ba4c3 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -178,13 +178,9 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun markAsReadById(id: Int): Boolean { - var success = false - if (isNetworkAvailable()) { - success = api.markAsRead(id.toString())?.isSuccess == true - } - return success - } + suspend fun markAsReadById(id: Int): Boolean = + isNetworkAvailable() && api.markAsRead(id.toString())?.isSuccess == true + suspend fun unmarkAsRead(item: SelfossModel.Item): Boolean { val success = unmarkAsReadById(item.id) @@ -195,13 +191,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun unmarkAsReadById(id: Int): Boolean { - var success = false - if (isNetworkAvailable()) { - success = api.unmarkAsRead(id.toString())?.isSuccess == true - } - return success - } + suspend fun unmarkAsReadById(id: Int): Boolean = + isNetworkAvailable() && api.unmarkAsRead(id.toString())?.isSuccess == true suspend fun starr(item: SelfossModel.Item): Boolean { val success = starrById(item.id) @@ -212,13 +203,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun starrById(id: Int): Boolean { - var success = false - if (isNetworkAvailable()) { - success = api.starr(id.toString())?.isSuccess == true - } - return success - } + suspend fun starrById(id: Int): Boolean = + isNetworkAvailable() && api.starr(id.toString())?.isSuccess == true suspend fun unstarr(item: SelfossModel.Item): Boolean { val success = unstarrById(item.id) @@ -229,22 +215,15 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails return success } - suspend fun unstarrById(id: Int): Boolean { - var success = false - if (isNetworkAvailable()) { - success = api.unstarr(id.toString())?.isSuccess == true - } - return success - } + suspend fun unstarrById(id: Int): Boolean = + isNetworkAvailable() && api.unstarr(id.toString())?.isSuccess == true suspend fun markAllAsRead(items: ArrayList): Boolean { var success = false - if (isNetworkAvailable()) { - success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true - } - if (success) { + if (isNetworkAvailable() && api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true) { + success = true for (item in items) { markAsReadLocally(item) }