Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dalafiarisamuel committed Sep 5, 2024
1 parent 7ac67dc commit 1208d87
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ actual class PlatformDownloadImage(private val context: Context) {

(context.getSystemService(Context.DOWNLOAD_SERVICE)
as DownloadManager).enqueue(request)
ImageDownloadState.Successful
ImageDownloadState.Success
} catch (e: Exception) {
ImageDownloadState.Failure(e)
}
Expand Down
4 changes: 2 additions & 2 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import data.repository.SharedRepository
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.koin.compose.koinInject
import ui.navigation.PhotoScreen
import ui.screen.HomeScreenEntryPoint
import ui.screen.PhotoDetailScreenEntryPoint
import ui.theme.LocalNavController
import ui.theme.UnsplashKMPTheme

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -28,7 +28,7 @@ fun App() {

UnsplashKMPTheme(darkTheme = sharedRepository.isDarkThemeEnabled) {

val navController = rememberNavController()
val navController = LocalNavController.current

NavHost(navController = navController, startDestination = PhotoScreen.HomeScreen) {

Expand Down
22 changes: 22 additions & 0 deletions composeApp/src/commonMain/kotlin/ui/navigation/Util.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ui.navigation

import com.mohamedrejeb.calf.permissions.ExperimentalPermissionsApi
import com.mohamedrejeb.calf.permissions.PermissionState
import com.mohamedrejeb.calf.permissions.PermissionStatus

@OptIn(ExperimentalPermissionsApi::class, ExperimentalPermissionsApi::class)
fun runWithPermission(permissionState: PermissionState, ifGranted: () -> Unit) {
when (val status = permissionState.status) {
is PermissionStatus.Denied -> {
if (status.shouldShowRationale) {
permissionState.launchPermissionRequest()
} else {
permissionState.openAppSettings()
}
}

is PermissionStatus.Granted -> {
ifGranted()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
Expand All @@ -18,11 +19,11 @@ import androidx.compose.ui.window.DialogProperties
import androidx.navigation.NavController
import com.mohamedrejeb.calf.permissions.ExperimentalPermissionsApi
import com.mohamedrejeb.calf.permissions.Permission
import com.mohamedrejeb.calf.permissions.PermissionStatus
import com.mohamedrejeb.calf.permissions.rememberPermissionState
import getPlatform
import org.koin.compose.viewmodel.koinViewModel
import org.koin.core.annotation.KoinExperimentalAPI
import ui.navigation.runWithPermission
import ui.theme.appDark
import ui.viewmodel.PhotoDetailViewModel

Expand All @@ -45,47 +46,28 @@ fun PhotoDetailScreenEntryPoint(
Permission.Gallery
)

val currentPlatform = remember { getPlatform() }

var showDialog = viewModel.isDownloading

PhotoDetail(
modifier = Modifier.fillMaxSize(),
state = viewModel.uiState,
onBackPressed = { navController.popBackStack() }
) {
when (getPlatform()) {
when (currentPlatform) {
Platform.Android -> {

when (val status = writeStorage.status) {
is PermissionStatus.Denied -> {
if (status.shouldShowRationale) {
writeStorage.launchPermissionRequest()
} else {
writeStorage.openAppSettings()
}
}

is PermissionStatus.Granted -> {
if (it != null) {
viewModel.startDownload(it)
}
runWithPermission(writeStorage) {
if (it != null) {
viewModel.startDownload(it)
}
}
}

Platform.Apple -> {
when (val status = gallery.status) {
is PermissionStatus.Denied -> {
if (status.shouldShowRationale) {
gallery.launchPermissionRequest()
} else {
gallery.openAppSettings()
}
}

is PermissionStatus.Granted -> {
if (it != null) {
viewModel.startDownload(it)
}
runWithPermission(gallery) {
if (it != null) {
viewModel.startDownload(it)
}
}
}
Expand All @@ -104,7 +86,7 @@ fun PhotoDetailScreenEntryPoint(
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)
) {
Column(
modifier = Modifier.size(100.dp).background(appDark, RoundedCornerShape(8.dp)),
modifier = Modifier.size(100.dp).background(appDark, RoundedCornerShape(10.dp)),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package ui.state
sealed class ImageDownloadState {
data object Idle : ImageDownloadState()
data object Loading : ImageDownloadState()
data object Successful : ImageDownloadState()
data object Success : ImageDownloadState()
data class Failure(val exception: Throwable) : ImageDownloadState()
}
15 changes: 12 additions & 3 deletions composeApp/src/commonMain/kotlin/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import ng.devtamuno.unsplash.compose.ui.theme.Shapes

private val DarkColorPalette = darkColors(
Expand All @@ -27,6 +31,10 @@ private val LightColorPalette = lightColors(
secondaryVariant = Color(0xFFD4D8EB)
)

val LocalNavController = compositionLocalOf<NavHostController> {
error("NavController not provided")
}

@ExperimentalFoundationApi
@Composable
fun UnsplashKMPTheme(
Expand All @@ -42,7 +50,8 @@ fun UnsplashKMPTheme(
MaterialTheme(
colors = colors,
typography = getTypography(),
shapes = Shapes,
content = content
)
shapes = Shapes
) {
CompositionLocalProvider(LocalNavController provides rememberNavController(), content = content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ actual class PlatformDownloadImage(private val client: HttpClient) {
val body = response.bodyAsChannel()
val result = body.toByteArray()
saveImageToFile(result, fileName)
ImageDownloadState.Successful
ImageDownloadState.Success
} catch (e: Exception) {
ImageDownloadState.Failure(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ actual class PlatformDownloadImage {

saveImageToPhotos(image)
println("Image saved successfully")
ImageDownloadState.Successful
ImageDownloadState.Success
} catch (e: Exception) {
ImageDownloadState.Failure(e)
}
Expand Down

0 comments on commit 1208d87

Please sign in to comment.