Skip to content

Commit

Permalink
Improve Database error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Aug 12, 2023
1 parent eadf301 commit 6b701ed
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:name=".FeedFlowApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="FeedFlow"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:supportsRtl="true"
android:theme="@style/Theme.FeedFlow">
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ kotlin.mpp.enableCInteropCommonization=true
releaseBuild=false
android.nonFinalResIds=false

## Composite Builds
#kotlin.mpp.import.enableKgpDependencyResolution=true
kotlin.native.cacheKind.iosX64=none
kotlin.native.cacheKind.iosSimulatorArm64=none
kotlin.mpp.androidSourceSetLayoutVersion=2

## Composite Builds
kotlin.mpp.import.enableKgpDependencyResolution=true
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ androidx-compose-material3 = "1.1.1"
androidx-compose-bom = "2023.06.01"
org-robolectric = "4.9"
sqlDelight = "1.5.5"
rss-parser = "6.0.0-alpha02"
rss-parser = "6.0.0-alpha03"
kotlinx-date-time = "0.4.0"
accompanist = "0.28.0"
jsoup = "1.15.3"
Expand Down
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pluginManagement {

dependencyResolutionManagement {
repositories {
// mavenLocal()
google()
mavenCentral()
maven {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class JvmAndroidDateFormatter(
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault()),
SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()),
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault()),
SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", Locale.getDefault()),
)

@Suppress("TooGenericExceptionCaught")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.retry
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -60,14 +61,15 @@ internal class DatabaseHelper(
}
.flowOn(backgroundDispatcher)

@Suppress("MagicNumber")
fun getFeedItems(): Flow<List<SelectFeeds>> =
dbRef.feedItemQueries
.selectFeeds()
.asFlow()
.catch {
logger.e(it) { "Something wrong while getting data from Database" }
}
.mapToList()
.retry(3) { exception ->
exception is NullPointerException
}
.flowOn(backgroundDispatcher)

suspend fun insertCategories(categories: List<String>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.prof18.feedflow.domain.model.FinishedFeedUpdateStatus
import com.prof18.feedflow.domain.model.InProgressFeedUpdateStatus
import com.prof18.feedflow.domain.model.NoFeedSourcesStatus
import com.prof18.feedflow.domain.model.StartedFeedUpdateStatus
import com.prof18.feedflow.presentation.model.DatabaseError
import com.prof18.feedflow.presentation.model.ErrorState
import com.prof18.feedflow.presentation.model.FeedErrorState
import com.prof18.feedflow.utils.DispatcherProvider
Expand All @@ -30,6 +31,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -77,6 +79,11 @@ internal class FeedRetrieverRepositoryImpl(
commentsUrl = selectedFeed.comments_url,
)
}
}.catch {
logger.e(it) { "Something wrong while getting data from Database" }
errorMutableState.update {
DatabaseError
}
}.flowOn(dispatcherProvider.io)

override suspend fun updateReadStatus(itemsToUpdates: List<FeedItemId>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.prof18.feedflow.domain.feed.retriever.FeedRetrieverRepository
import com.prof18.feedflow.domain.model.FeedItem
import com.prof18.feedflow.domain.model.FeedItemId
import com.prof18.feedflow.domain.model.FeedUpdateStatus
import com.prof18.feedflow.presentation.model.DatabaseError
import com.prof18.feedflow.presentation.model.FeedErrorState
import com.prof18.feedflow.presentation.model.UIErrorState
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.ResourceFormatted
import dev.icerock.moko.resources.desc.StringDesc
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -74,6 +76,16 @@ class HomeViewModel(
)
}

is DatabaseError -> {
mutableUIErrorState.emit(
UIErrorState(
message = StringDesc.Resource(
stringRes = MR.strings.database_error,
),
),
)
}

null -> {
// Do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ sealed interface ErrorState
data class FeedErrorState(
val failingSourceName: String,
) : ErrorState

object DatabaseError : ErrorState
1 change: 1 addition & 0 deletions shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
<string name="open_source_nav_bar">Open source licenses</string>
<string name="report_issue_button">Report issue</string>
<string name="open_website_button">Open website</string>
<string name="database_error">Something went wrong during the retrieval of the feeds. Please retry or restart the app :(</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class DateFormatterTest {
"2023-07-09T18:42:00.004-07:00",
"2022/09/22",
"2023-07-25T13:55:02",
"Tue, 08 Aug 2023 12:58:00 Z",
"Tue, 5 Sep 2017 09:58:38 +0000",
"Wed, 13 Sep 2017 09:59:21 +0000",
)

@Test
Expand Down

0 comments on commit 6b701ed

Please sign in to comment.