From a96bf18a1856aa3e55a1410db348ada78ecc3bec Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 12 Nov 2024 18:40:50 +0100 Subject: [PATCH] Use AccountError in FeedTab --- .../main/java/com/readrops/app/AppModule.kt | 6 ++-- .../app/account/AccountScreenModel.kt | 3 +- .../com/readrops/app/feeds/FeedScreenModel.kt | 29 ++++++++++++------- .../java/com/readrops/app/feeds/FeedState.kt | 4 +-- .../java/com/readrops/app/feeds/FeedTab.kt | 9 ++---- .../app/feeds/dialogs/UpdateFeedDialog.kt | 6 ++-- .../com/readrops/app/home/TabScreenModel.kt | 6 ++++ .../app/timelime/TimelineScreenModel.kt | 3 +- .../util/components/dialog/TextFieldDialog.kt | 8 ++--- 9 files changed, 41 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/readrops/app/AppModule.kt b/app/src/main/java/com/readrops/app/AppModule.kt index 99bbd65e..ec96c509 100644 --- a/app/src/main/java/com/readrops/app/AppModule.kt +++ b/app/src/main/java/com/readrops/app/AppModule.kt @@ -40,15 +40,15 @@ import org.koin.dsl.module val appModule = module { - factory { TimelineScreenModel(get(), get(), get()) } + factory { TimelineScreenModel(get(), get(), get(), androidContext()) } - factory { FeedScreenModel(get(), get(), get(), androidContext()) } + factory { FeedScreenModel(get(), get(), androidContext()) } factory { (url: String?) -> NewFeedScreenModel(get(), get(), androidContext(), url) } factory { AccountSelectionScreenModel(get()) } - factory { AccountScreenModel(get()) } + factory { AccountScreenModel(get(), androidContext()) } factory { (itemId: Int) -> ItemScreenModel(get(), itemId, get()) } diff --git a/app/src/main/java/com/readrops/app/account/AccountScreenModel.kt b/app/src/main/java/com/readrops/app/account/AccountScreenModel.kt index f49db771..0c1f027d 100644 --- a/app/src/main/java/com/readrops/app/account/AccountScreenModel.kt +++ b/app/src/main/java/com/readrops/app/account/AccountScreenModel.kt @@ -29,8 +29,9 @@ import org.koin.core.component.get class AccountScreenModel( private val database: Database, + context: Context, private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : TabScreenModel(database) { +) : TabScreenModel(database, context) { private val _closeHome = MutableStateFlow(false) val closeHome = _closeHome.asStateFlow() diff --git a/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt b/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt index 46db91ad..bec9b179 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedScreenModel.kt @@ -3,7 +3,6 @@ package com.readrops.app.feeds import android.content.Context import android.util.Patterns import cafe.adriel.voyager.core.model.screenModelScope -import com.readrops.api.localfeed.LocalRSSDataSource import com.readrops.app.R import com.readrops.app.home.TabScreenModel import com.readrops.app.repositories.GetFoldersWithFeeds @@ -28,10 +27,9 @@ import org.koin.core.component.KoinComponent class FeedScreenModel( database: Database, private val getFoldersWithFeeds: GetFoldersWithFeeds, - private val localRSSDataSource: LocalRSSDataSource, private val context: Context, private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : TabScreenModel(database), KoinComponent { +) : TabScreenModel(database, context), KoinComponent { private val _feedState = MutableStateFlow(FeedState()) val feedsState = _feedState.asStateFlow() @@ -112,14 +110,14 @@ class FeedScreenModel( it.copy( value = "", textFieldError = null, - exception = null, + error = null, isLoading = false ) } } is DialogState.UpdateFeed -> { - _updateFeedDialogState.update { it.copy(exception = null, isLoading = false) } + _updateFeedDialogState.update { it.copy(error = null, isLoading = false) } } else -> {} @@ -162,7 +160,7 @@ class FeedScreenModel( try { repository?.deleteFeed(feed) } catch (e: Exception) { - _feedState.update { it.copy(exception = e) } + _feedState.update { it.copy(error = accountError?.deleteFeedMessage(e)) } } } } @@ -172,7 +170,7 @@ class FeedScreenModel( try { repository?.deleteFolder(folder) } catch (e: Exception) { - _feedState.update { it.copy(exception = e) } + _feedState.update { it.copy(error = accountError?.deleteFolderMessage(e)) } } } } @@ -236,7 +234,7 @@ class FeedScreenModel( } else -> { - _updateFeedDialogState.update { it.copy(exception = null, isLoading = true) } + _updateFeedDialogState.update { it.copy(error = null, isLoading = true) } screenModelScope.launch(dispatcher) { with(_updateFeedDialogState.value) { @@ -256,7 +254,7 @@ class FeedScreenModel( } catch (e: Exception) { _updateFeedDialogState.update { it.copy( - exception = e, + error = accountError?.updateFeedMessage(e), isLoading = false ) } @@ -304,7 +302,16 @@ class FeedScreenModel( repository?.addFolder(Folder(name = name, accountId = currentAccount!!.id)) } } catch (e: Exception) { - _folderState.update { it.copy(exception = e, isLoading = false) } + _folderState.update { + it.copy( + error = if (updateFolder) { + accountError?.updateFolderMessage(e) + } else { + accountError?.newFolderMessage(e) + }, + isLoading = false + ) + } return@launch } @@ -312,7 +319,7 @@ class FeedScreenModel( } } - fun resetException() = _feedState.update { it.copy(exception = null) } + fun resetException() = _feedState.update { it.copy(error = null) } //endregion } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/feeds/FeedState.kt b/app/src/main/java/com/readrops/app/feeds/FeedState.kt index e4ecf4ae..5a91e266 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedState.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedState.kt @@ -9,7 +9,7 @@ data class FeedState( val foldersAndFeeds: FolderAndFeedsState = FolderAndFeedsState.InitialState, val dialog: DialogState? = null, val areFoldersExpanded: Boolean = false, - val exception: Exception? = null, + val error: String? = null, val config: AccountConfig? = null ) { @@ -48,7 +48,7 @@ data class UpdateFeedDialogState( val folders: List = listOf(), val isFolderDropDownExpanded: Boolean = false, val isFeedUrlReadOnly: Boolean = true, - val exception: Exception? = null, + val error: String? = null, val isLoading: Boolean = false ) { val isFeedNameError diff --git a/app/src/main/java/com/readrops/app/feeds/FeedTab.kt b/app/src/main/java/com/readrops/app/feeds/FeedTab.kt index 840c8351..63259cd9 100644 --- a/app/src/main/java/com/readrops/app/feeds/FeedTab.kt +++ b/app/src/main/java/com/readrops/app/feeds/FeedTab.kt @@ -32,7 +32,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.input.nestedscroll.nestedScroll -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.platform.LocalUriHandler import androidx.compose.ui.res.painterResource @@ -48,7 +47,6 @@ import com.readrops.app.R import com.readrops.app.feeds.dialogs.FeedModalBottomSheet import com.readrops.app.feeds.dialogs.UpdateFeedDialog import com.readrops.app.feeds.newfeed.NewFeedScreen -import com.readrops.app.util.ErrorMessage import com.readrops.app.util.components.CenteredProgressIndicator import com.readrops.app.util.components.ErrorMessage import com.readrops.app.util.components.Placeholder @@ -75,7 +73,6 @@ object FeedTab : Tab { override fun Content() { val haptic = LocalHapticFeedback.current val uriHandler = LocalUriHandler.current - val context = LocalContext.current val navigator = LocalNavigator.currentOrThrow val screenModel = koinScreenModel() @@ -85,9 +82,9 @@ object FeedTab : Tab { val topAppBarScrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState()) - LaunchedEffect(state.exception) { - if (state.exception != null) { - snackbarHostState.showSnackbar(ErrorMessage.get(state.exception!!, context)) + LaunchedEffect(state.error) { + if (state.error != null) { + snackbarHostState.showSnackbar((state.error!!)) screenModel.resetException() } } diff --git a/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt b/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt index 13779f3a..09673bb0 100644 --- a/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt +++ b/app/src/main/java/com/readrops/app/feeds/dialogs/UpdateFeedDialog.kt @@ -12,13 +12,11 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.readrops.app.R import com.readrops.app.feeds.FeedScreenModel -import com.readrops.app.util.ErrorMessage import com.readrops.app.util.components.LoadingTextButton import com.readrops.app.util.components.dialog.BaseDialog import com.readrops.app.util.theme.LargeSpacer @@ -123,11 +121,11 @@ fun UpdateFeedDialog( ) } - if (state.exception != null) { + if (state.error != null) { MediumSpacer() Text( - text = ErrorMessage.get(state.exception!!, LocalContext.current), + text = state.error!!, color = MaterialTheme.colorScheme.error ) } diff --git a/app/src/main/java/com/readrops/app/home/TabScreenModel.kt b/app/src/main/java/com/readrops/app/home/TabScreenModel.kt index 882eceff..5a677ed7 100644 --- a/app/src/main/java/com/readrops/app/home/TabScreenModel.kt +++ b/app/src/main/java/com/readrops/app/home/TabScreenModel.kt @@ -1,11 +1,13 @@ package com.readrops.app.home +import android.content.Context import android.content.SharedPreferences import cafe.adriel.voyager.core.model.ScreenModel import cafe.adriel.voyager.core.model.screenModelScope import com.readrops.api.services.Credentials import com.readrops.api.utils.AuthInterceptor import com.readrops.app.repositories.BaseRepository +import com.readrops.app.util.accounterror.AccountError import com.readrops.db.Database import com.readrops.db.entities.account.Account import kotlinx.coroutines.CoroutineDispatcher @@ -24,6 +26,7 @@ import org.koin.core.parameter.parametersOf */ abstract class TabScreenModel( private val database: Database, + private val context: Context, dispatcher: CoroutineDispatcher = Dispatchers.IO ) : ScreenModel, KoinComponent { @@ -34,6 +37,8 @@ abstract class TabScreenModel( protected var currentAccount: Account? = null + protected var accountError: AccountError? = null + private val _accountEvent = MutableSharedFlow() protected val accountEvent = _accountEvent.shareIn(scope = screenModelScope, started = SharingStarted.Eagerly) @@ -61,6 +66,7 @@ abstract class TabScreenModel( currentAccount = account repository = get(parameters = { parametersOf(account) }) + accountError = AccountError.from(account, context) _accountEvent.emit(account) } diff --git a/app/src/main/java/com/readrops/app/timelime/TimelineScreenModel.kt b/app/src/main/java/com/readrops/app/timelime/TimelineScreenModel.kt index d5ac1ac7..7290e271 100644 --- a/app/src/main/java/com/readrops/app/timelime/TimelineScreenModel.kt +++ b/app/src/main/java/com/readrops/app/timelime/TimelineScreenModel.kt @@ -45,8 +45,9 @@ class TimelineScreenModel( private val database: Database, private val getFoldersWithFeeds: GetFoldersWithFeeds, private val preferences: Preferences, + context: Context, private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : TabScreenModel(database) { +) : TabScreenModel(database, context) { private val _timelineState = MutableStateFlow(TimelineState()) val timelineState = _timelineState.asStateFlow() diff --git a/app/src/main/java/com/readrops/app/util/components/dialog/TextFieldDialog.kt b/app/src/main/java/com/readrops/app/util/components/dialog/TextFieldDialog.kt index c79af6de..6af5e5ec 100644 --- a/app/src/main/java/com/readrops/app/util/components/dialog/TextFieldDialog.kt +++ b/app/src/main/java/com/readrops/app/util/components/dialog/TextFieldDialog.kt @@ -10,10 +10,8 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.painter.Painter -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import com.readrops.app.R -import com.readrops.app.util.ErrorMessage import com.readrops.app.util.components.LoadingTextButton import com.readrops.app.util.components.TextFieldError import com.readrops.app.util.theme.LargeSpacer @@ -21,7 +19,7 @@ import com.readrops.app.util.theme.LargeSpacer data class TextFieldDialogState( val value: String = "", val textFieldError: TextFieldError? = null, - val exception: Exception? = null, + val error: String? = null, val isLoading: Boolean = false ) { val isTextFieldError @@ -66,9 +64,9 @@ fun TextFieldDialog( supportingText = { Text(text = state.textFieldError?.errorText().orEmpty()) } ) - if (state.exception != null) { + if (state.error != null) { Text( - text = ErrorMessage.get(state.exception, LocalContext.current), + text = state.error, color = MaterialTheme.colorScheme.error ) }