Skip to content

Commit

Permalink
- Refactor: Replace Toaster with Snackbar and Update Dependencies
Browse files Browse the repository at this point in the history
This commit introduces several changes:

- **Replace Toaster with Snackbar:**
  - Replaces the custom Toaster implementation with Snackbar in the Recent and Reader Compose screens, providing a more standard Android UI experience.
- **Dependency Updates:**
  - Updates Gradle to version 8.11.1.
  - Updates several library dependencies, including Landscapist, Media3, and Activity Compose.
- **Code Improvements:**
  - Improves logic for retrieving parent links in AnimeUtils, handling cases with multiple trailing slashes.
  - Simplifies logic for removing toast items in ToasterItemsSetup.
  • Loading branch information
jacobrein committed Nov 25, 2024
1 parent d19dcda commit cf2d47e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
Expand Down Expand Up @@ -70,7 +71,6 @@ import com.programmersbox.uiviews.utils.LocalSourcesRepository
import com.programmersbox.uiviews.utils.OtakuBannerBox
import com.programmersbox.uiviews.utils.PreviewTheme
import com.programmersbox.uiviews.utils.PreviewThemeColorsSizes
import com.programmersbox.uiviews.utils.ToasterItemsSetup
import com.programmersbox.uiviews.utils.components.InfiniteListHandler
import com.programmersbox.uiviews.utils.components.NoSourcesInstalled
import com.programmersbox.uiviews.utils.components.OtakuHazeScaffold
Expand Down Expand Up @@ -182,7 +182,8 @@ fun RecentView(
blurTopBar = showBlur,
topBarBlur = {
progressive = HazeProgressive.verticalGradient(startIntensity = 1f, endIntensity = 0f, preferPerformance = true)
}
},
snackbarHost = { SnackbarHost(recentVm.snackbarHostState) }
) { p ->
var showBanner by remember { mutableStateOf(false) }
OtakuBannerBox(
Expand Down Expand Up @@ -250,12 +251,6 @@ fun RecentView(
}
}
}

ToasterItemsSetup(
toastItems = recentVm,
alignment = Alignment.TopCenter,
modifier = Modifier.padding(p)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.programmersbox.uiviews.recent

import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
Expand All @@ -18,8 +19,6 @@ import com.programmersbox.models.ItemModel
import com.programmersbox.models.SourceInformation
import com.programmersbox.sharedutils.FirebaseDb
import com.programmersbox.uiviews.CurrentSourceRepository
import com.programmersbox.uiviews.utils.DefaultToastItems
import com.programmersbox.uiviews.utils.ToastItems
import com.programmersbox.uiviews.utils.combineSources
import com.programmersbox.uiviews.utils.dispatchIo
import com.programmersbox.uiviews.utils.recordFirebaseException
Expand All @@ -33,13 +32,14 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.withContext
import ru.beryukhov.reactivenetwork.ReactiveNetwork

class RecentViewModel(
dao: ItemDao,
sourceRepository: SourceRepository,
currentSourceRepository: CurrentSourceRepository,
) : ViewModel(), ToastItems by DefaultToastItems() {
) : ViewModel() {

var isRefreshing by mutableStateOf(false)
private val sourceList = mutableStateListOf<ItemModel>()
Expand All @@ -61,6 +61,8 @@ class RecentViewModel(

val sources = mutableStateListOf<SourceInformation>()

val snackbarHostState = SnackbarHostState()

init {
combineSources(sourceRepository, dao)
.onEach {
Expand Down Expand Up @@ -118,7 +120,12 @@ class RecentViewModel(
?.dispatchIo()
?.catch {
it.printStackTrace()
showError()
withContext(Dispatchers.Main) {
snackbarHostState.showSnackbar(
"Something went wrong",
withDismissAction = true
)
}
emit(emptyList())
recordFirebaseException(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ class DefaultToastItems : ToastItems {
@Composable
fun ToasterItemsSetup(
toastItems: ToastItems,
modifier: Modifier = Modifier,
scope: CoroutineScope = rememberCoroutineScope(),
toaster: ToasterState = rememberToasterState(
onToastDismissed = { scope.launch { toastItems.removeError() } }
),
modifier: Modifier = Modifier,
alignment: Alignment = Alignment.BottomEnd,
) {
LaunchedEffect(toaster, toastItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package com.programmersbox.anime_sources.utilities
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.programmersbox.anime_sources.ShowApi
import okhttp3.*
import okhttp3.CacheControl
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.Headers.Companion.toHeaders
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okio.BufferedSink
import java.net.URI
import java.util.*
import java.util.Locale
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import javax.crypto.Cipher
Expand Down Expand Up @@ -269,8 +278,9 @@ class M3u8Helper {
}

private fun getParentLink(uri: String): String {
val split = uri.split("/").toMutableList()
split.removeLast()
val split = uri.split("/")
.dropLast(1)
.toMutableList()
return split.joinToString("/")
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ okhttpVersion = "4.12.0"
ktorVersion = "3.0.1"
workVersion = "2.10.0"
ziplineVersion = "1.1.0"
landscapist = "2.4.2"
landscapist = "2.4.3"
protobufVersion = "4.26.1"
sketchVersion = "3.3.2"
activity = "1.10.0-beta01"
Expand All @@ -52,7 +52,7 @@ piasy = "1.8.1"

### AnimeWorld
autoBinding = "1.1-beta04"
media3Version = "1.4.1"
media3Version = "1.5.0"

junit = "1.2.1"
espresso-core = "3.6.1"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.LazyListState
Expand All @@ -31,6 +30,8 @@ import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -50,9 +51,6 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.dokar.sonner.ToastType
import com.dokar.sonner.ToasterDefaults
import com.dokar.sonner.rememberToasterState
import com.programmersbox.favoritesdatabase.ItemDao
import com.programmersbox.mangasettings.ImageLoaderType
import com.programmersbox.mangasettings.ReaderType
Expand All @@ -64,7 +62,6 @@ import com.programmersbox.uiviews.utils.HideSystemBarsWhileOnScreen
import com.programmersbox.uiviews.utils.LocalGenericInfo
import com.programmersbox.uiviews.utils.LocalItemDao
import com.programmersbox.uiviews.utils.LocalSettingsHandling
import com.programmersbox.uiviews.utils.ToasterSetup
import com.programmersbox.uiviews.utils.components.OtakuPullToRefreshBox
import dev.chrisbanes.haze.HazeProgressive
import dev.chrisbanes.haze.HazeState
Expand Down Expand Up @@ -104,7 +101,7 @@ fun ReadView(
) {
HideSystemBarsWhileOnScreen()

val toaster = rememberToasterState()
val snackbarHostState = remember { SnackbarHostState() }

val scope = rememberCoroutineScope()

Expand Down Expand Up @@ -146,12 +143,10 @@ fun ReadView(
var middleAction by mangaSettingsHandling.rememberPlayingMiddleAction()

fun showToast() {
toaster.show(
message = context.getString(R.string.addedChapterItem),
icon = R.mipmap.ic_launcher,
type = ToastType.Success,
duration = ToasterDefaults.DurationShort
)
scope.launch {
snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(context.getString(R.string.addedChapterItem))
}
}

val listShowItems by remember { derivedStateOf { listState.isScrolledToTheEnd() && readerType == ReaderType.List } }
Expand Down Expand Up @@ -333,6 +328,7 @@ fun ReadView(
}
}
},
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
) { p ->
Box(
modifier = if (showBlur)
Expand Down Expand Up @@ -392,10 +388,6 @@ fun ReadView(
}
}
}
ToasterSetup(
toaster = toaster,
modifier = Modifier.padding(p)
)
}
}
}
Expand Down

0 comments on commit cf2d47e

Please sign in to comment.