diff --git a/app/src/main/java/org/listenbrainz/android/model/SocialUiState.kt b/app/src/main/java/org/listenbrainz/android/model/SocialUiState.kt index f06043e4..c2d24bf6 100644 --- a/app/src/main/java/org/listenbrainz/android/model/SocialUiState.kt +++ b/app/src/main/java/org/listenbrainz/android/model/SocialUiState.kt @@ -2,5 +2,5 @@ package org.listenbrainz.android.model data class SocialUiState( val error: ResponseError? = null, - val successMsg : String? = null + val successMsgId : Int? = null ) \ No newline at end of file diff --git a/app/src/main/java/org/listenbrainz/android/ui/components/SuccessBar.kt b/app/src/main/java/org/listenbrainz/android/ui/components/SuccessBar.kt index 48a92dc2..aa6aa344 100644 --- a/app/src/main/java/org/listenbrainz/android/ui/components/SuccessBar.kt +++ b/app/src/main/java/org/listenbrainz/android/ui/components/SuccessBar.kt @@ -4,47 +4,37 @@ import android.content.res.Configuration.UI_MODE_NIGHT_YES import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.expandVertically import androidx.compose.animation.shrinkVertically -import androidx.compose.foundation.background -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.material.Text import androidx.compose.material3.SnackbarHostState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import org.listenbrainz.android.model.ResponseError import org.listenbrainz.android.ui.theme.ListenBrainzTheme - +import org.listenbrainz.android.R @Composable fun SuccessBar( - message : String?, + resId : Int?, onMessageShown: () -> Unit, snackbarState : SnackbarHostState ) { - LaunchedEffect(message) { - if (message != null) { + val context = LocalContext.current; + LaunchedEffect(resId) { + if (resId != null) { delay(4000) onMessageShown() } } AnimatedVisibility( - visible = message != null, + visible = resId != null, enter = expandVertically(), exit = shrinkVertically() ) { - LaunchedEffect(key1 = message){ - if(message != null){ - snackbarState.showSnackbar(message) + LaunchedEffect(key1 = resId){ + if(resId != null){ + snackbarState.showSnackbar(context.getString(resId)) } } } @@ -55,6 +45,6 @@ fun SuccessBar( @Composable private fun SuccessBarPreview() { ListenBrainzTheme { - ErrorBar(error = ResponseError.NETWORK_ERROR) {} + SuccessBar(resId = R.string.about_title , onMessageShown = {} , snackbarState = SnackbarHostState()) } } \ No newline at end of file diff --git a/app/src/main/java/org/listenbrainz/android/ui/screens/listens/ListensScreen.kt b/app/src/main/java/org/listenbrainz/android/ui/screens/listens/ListensScreen.kt index 995c4631..74204df4 100644 --- a/app/src/main/java/org/listenbrainz/android/ui/screens/listens/ListensScreen.kt +++ b/app/src/main/java/org/listenbrainz/android/ui/screens/listens/ListensScreen.kt @@ -28,11 +28,13 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalUriHandler import androidx.compose.ui.platform.UriHandler import androidx.compose.ui.tooling.preview.Preview import androidx.hilt.navigation.compose.hiltViewModel import kotlinx.coroutines.launch +import org.listenbrainz.android.R import org.listenbrainz.android.model.Listen import org.listenbrainz.android.model.Metadata import org.listenbrainz.android.model.SocialUiState @@ -52,7 +54,6 @@ import org.listenbrainz.android.ui.screens.feed.SocialDropdown import org.listenbrainz.android.ui.screens.profile.UserData import org.listenbrainz.android.ui.screens.settings.PreferencesUiState import org.listenbrainz.android.ui.theme.ListenBrainzTheme -import org.listenbrainz.android.util.Constants import org.listenbrainz.android.util.Utils import org.listenbrainz.android.viewmodel.FeedViewModel import org.listenbrainz.android.viewmodel.ListensViewModel @@ -171,6 +172,8 @@ fun ListensScreen( val dialogsState = rememberDialogsState() val scope = rememberCoroutineScope() + + val context = LocalContext.current // Scroll to the top when shouldScrollToTop becomes true LaunchedEffect(scrollRequestState) { @@ -266,7 +269,7 @@ fun ListensScreen( } catch(e : Error) { scope.launch { - snackbarState.showSnackbar(Constants.Strings.ERROR_MESSAGE) + snackbarState.showSnackbar(context.getString(R.string.err_generic_toast)) } } dropdownItemIndex.value = null @@ -285,7 +288,7 @@ fun ListensScreen( } ErrorBar(error = socialUiState.error, onErrorShown = onErrorShown ) - SuccessBar(message = socialUiState.successMsg, onMessageShown = onMessageShown, snackbarState = snackbarState) + SuccessBar(resId = socialUiState.successMsgId, onMessageShown = onMessageShown, snackbarState = snackbarState) Dialogs( deactivateDialog = { @@ -331,6 +334,7 @@ private fun Dialogs( snackbarState: SnackbarHostState, socialUiState: SocialUiState ) { + val context = LocalContext.current when (currentDialog) { Dialog.NONE -> Unit Dialog.PIN -> { @@ -340,7 +344,7 @@ private fun Dialogs( }) LaunchedEffect(socialUiState.error){ if(socialUiState.error == null){ - snackbarState.showSnackbar(Constants.Strings.PIN_GREETING) + snackbarState.showSnackbar(context.getString(R.string.pin_greeting)) } } } diff --git a/app/src/main/java/org/listenbrainz/android/util/Constants.kt b/app/src/main/java/org/listenbrainz/android/util/Constants.kt index 74cc0e63..16deebdd 100644 --- a/app/src/main/java/org/listenbrainz/android/util/Constants.kt +++ b/app/src/main/java/org/listenbrainz/android/util/Constants.kt @@ -35,11 +35,6 @@ object Constants { const val REFRESH_TOKEN = "refresh_token" const val STATUS_LOGGED_IN = 1 const val STATUS_LOGGED_OUT = 0 - const val RECOMMENDATION_GREETING = "Song recommended successfully!" - const val PERSONAL_RECOMMENDATION_GREETING = "Song recommendation sent successfully!" - const val REVIEW_GREETING = "Song review submitted successfully!" - const val PIN_GREETING = "Song pinned successfully!" - const val ERROR_MESSAGE = "Error occurred! Please try again later!" const val CHANNEL_NOTI_SCROBBLING = "noti_scrobbling" const val CHANNEL_NOTI_SCR_ERR = "noti_scrobble_errors" diff --git a/app/src/main/java/org/listenbrainz/android/viewmodel/BaseViewModel.kt b/app/src/main/java/org/listenbrainz/android/viewmodel/BaseViewModel.kt index 042be486..9e8952da 100644 --- a/app/src/main/java/org/listenbrainz/android/viewmodel/BaseViewModel.kt +++ b/app/src/main/java/org/listenbrainz/android/viewmodel/BaseViewModel.kt @@ -10,7 +10,7 @@ import org.listenbrainz.android.model.ResponseError abstract class BaseViewModel: ViewModel() { protected val errorFlow = MutableStateFlow(null) - protected val successMsgFlow = MutableStateFlow(null) + protected val successMsgFlow = MutableStateFlow(null) /** Visible Ui-State for UI to consume.*/ abstract val uiState: StateFlow @@ -23,9 +23,9 @@ abstract class BaseViewModel: ViewModel() { } } - protected fun emitMsg(message : String?){ + protected fun emitMsg(messageId : Int?){ viewModelScope.launch { - successMsgFlow.emit(message) + successMsgFlow.emit(messageId) } } diff --git a/app/src/main/java/org/listenbrainz/android/viewmodel/SocialViewModel.kt b/app/src/main/java/org/listenbrainz/android/viewmodel/SocialViewModel.kt index a77b6d21..99af8003 100644 --- a/app/src/main/java/org/listenbrainz/android/viewmodel/SocialViewModel.kt +++ b/app/src/main/java/org/listenbrainz/android/viewmodel/SocialViewModel.kt @@ -23,9 +23,9 @@ import org.listenbrainz.android.model.feed.ReviewEntityType import org.listenbrainz.android.repository.preferences.AppPreferences import org.listenbrainz.android.repository.remoteplayer.RemotePlaybackHandler import org.listenbrainz.android.repository.social.SocialRepository -import org.listenbrainz.android.util.Constants import org.listenbrainz.android.util.Resource import javax.inject.Inject +import org.listenbrainz.android.R @HiltViewModel class SocialViewModel @Inject constructor( @@ -108,7 +108,7 @@ class SocialViewModel @Inject constructor( emitError(result.error) } else if(result.status == Resource.Status.SUCCESS){ - emitMsg(Constants.Strings.RECOMMENDATION_GREETING) + emitMsg(R.string.recommendation_greeting) } } } @@ -135,7 +135,7 @@ class SocialViewModel @Inject constructor( emitError(result.error) } else if(result.status == Resource.Status.SUCCESS){ - emitMsg(Constants.Strings.PERSONAL_RECOMMENDATION_GREETING) + emitMsg(R.string.personal_recommendation_greeting) } } @@ -162,7 +162,7 @@ class SocialViewModel @Inject constructor( emitError(result.error) } else if(result.status == Resource.Status.SUCCESS){ - emitMsg(Constants.Strings.REVIEW_GREETING) + emitMsg(R.string.review_greeting) } } } @@ -180,7 +180,7 @@ class SocialViewModel @Inject constructor( emitError(result.error) } else if(result.status == Resource.Status.SUCCESS){ - emitMsg(Constants.Strings.PIN_GREETING) + emitMsg(R.string.pin_greeting) } } }