Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
DanteAndroid committed Aug 10, 2022
1 parent 97141c9 commit 633cd58
Show file tree
Hide file tree
Showing 45 changed files with 229 additions and 288 deletions.
8 changes: 4 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ComposeWall

[English version](README.md)
ComposeWall 是一款由 [Jetpack Compose](https://developer.android.com/jetpack/compose) 打造的应用.
ComposeWall 是一款由 [Jetpack Compose](https://developer.android.com/jetpack/compose) 打造的应用

### 下载与截图

Expand All @@ -11,12 +11,12 @@ ComposeWall 是一款由 [Jetpack Compose](https://developer.android.com/jetpack

### 谁适合学习此项目?

Android 初学者和资深开发都可以学习此项目
Android 初学者和资深开发都可以学习此项目

### 为什么要选择这个项目?

你还可以看看 [Compose samples](https://github.com/android/compose-samples),
但我觉得会比较难一些。如有任何疑问,请提issue,我会尽我所能地回答
你还可以看看 [Compose samples](https://github.com/android/compose-samples)
但我觉得会比较难一些。如有任何疑问,请提issue,我会尽可能地解答~

### 项目的架构?

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ android {
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha15'
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha15'
implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha15"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.5.1'
Expand Down
Binary file added app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/src/main/java/com/danteandroi/composewall/ComposeApp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.danteandroi.composewall

import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -9,6 +10,7 @@ import com.danteandroi.composewall.ui.theme.ComposeWallTheme
* @author Du Wenyu
* 2022/7/29
*/
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ComposeApp(sizeClass: WindowSizeClass? = null) {
ComposeWallTheme {
Expand Down
31 changes: 25 additions & 6 deletions app/src/main/java/com/danteandroi/composewall/ComposeNavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material.BackdropValue
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.rememberBackdropScaffoldState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
Expand All @@ -21,6 +28,7 @@ import com.danteandroi.composewall.utils.isExpandedScreen
* @author Du Wenyu
* 2022/8/2
*/
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
fun ComposeNavGraph(
modifier: Modifier = Modifier,
Expand All @@ -33,13 +41,14 @@ fun ComposeNavGraph(
startDestination = startDestination,
navController = appState.navController
) {
composable(ComposeDestinations.HOME) { stackEntry ->
composable(ComposeDestinations.HOME) {
var currentDetailImage by remember {
mutableStateOf(Image.EMPTY)
}
Box(modifier = modifier) {
BackdropScaffold(
modifier = Modifier.statusBarsPadding(),
scaffoldState = rememberBackdropScaffoldState(initialValue = BackdropValue.Concealed),
sizeClass.isExpandedScreen()
) { id, vm ->
vm.findImage(id)?.let { image ->
Expand All @@ -51,22 +60,32 @@ fun ComposeNavGraph(
enter = fadeIn(),
exit = fadeOut()
) {
DetailScreen(
image = currentDetailImage,
navigateUp = { currentDetailImage = Image.EMPTY })
Scaffold(snackbarHost = { SnackbarHost(hostState = appState.scaffoldState) }) { paddings ->
DetailScreen(
Modifier.padding(paddings),
image = currentDetailImage,
navigateUp = { currentDetailImage = Image.EMPTY })
}
}
}
}
// unused destination
composable(
"${ComposeDestinations.DETAIL}/{${ComposeDestinations.DETAIL_ID}}",
arguments = listOf(navArgument(ComposeDestinations.DETAIL_ID) {
type = NavType.StringType
})
) {
// Unused destination
val id = requireNotNull(it.arguments).getString(ComposeDestinations.DETAIL_ID)!!
appState.currentViewModel?.findImage(id)?.let { image ->
DetailScreen(image = image, navigateUp = { appState.navigateUp() })
Scaffold(snackbarHost = { SnackbarHost(hostState = appState.scaffoldState) }) { paddings ->
DetailScreen(
Modifier.padding(paddings),
image = image,
navigateUp = {
appState.navigateUp()
})
}
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/java/com/danteandroi/composewall/ComposeNavigation.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.danteandroi.composewall

import android.content.res.Resources
import androidx.compose.material.ScaffoldState
import androidx.compose.material.rememberScaffoldState
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -32,7 +31,9 @@ object ComposeDestinations {

@Composable
fun rememberComposeAppState(
scaffoldState: ScaffoldState = rememberScaffoldState(),
scaffoldState: SnackbarHostState = remember {
SnackbarHostState()
},
snackBarManager: SnackBarManager = SnackBarManager,
navController: NavHostController = rememberNavController(),
resources: Resources = LocalContext.current.resources,
Expand All @@ -42,8 +43,8 @@ fun rememberComposeAppState(
}

@Stable
class ComposeAppState(
private val scaffoldState: ScaffoldState,
class ComposeAppState constructor(
val scaffoldState: SnackbarHostState,
private val snackBarManager: SnackBarManager,
private val resources: Resources,
val navController: NavHostController,
Expand All @@ -53,16 +54,11 @@ class ComposeAppState(
init {
coroutineScope.launch {
snackBarManager.messages.collect { messages ->
try {
if (messages.isNotEmpty()) {
val text = resources.getText(messages[0].message)
scaffoldState.snackbarHostState.showSnackbar(message = text.toString())
snackBarManager.setMessageShown(messages[0].id)
}
} catch (e: Exception) {
e.printStackTrace()
if (messages.isNotEmpty()) {
val text = resources.getText(messages[0].message)
scaffoldState.showSnackbar(message = text.toString())
snackBarManager.setMessageShown(messages[0].id)
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data class Image(
val height: Int = 0,
val timestamp: Date = Date()
) {
val uniqueId = id + timestamp.time
val isValidImage get() = url.isNotEmpty() && thumbnail.isNotEmpty()

companion object {
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/danteandroi/composewall/data/ImageSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.danteandroi.composewall.data

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.danteandroi.composewall.MenuItem
import com.danteandroi.composewall.net.ImageRepository

/**
* @author Du Wenyu
* 2022/8/3
*/
class ImageSource(private val menuItem: MenuItem, private val repository: ImageRepository) :
PagingSource<Int, Image>() {

override fun getRefreshKey(state: PagingState<Int, Image>): Int? {
return state.pages[state.anchorPosition?.plus(1) ?: 0].prevKey
}

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Image> {
return try {
val nextPage = params.key ?: 1
val result = repository.fetchImages(
apiClazz = menuItem.apiClazz,
baseUrl = menuItem.baseUrl,
category = menuItem.category[nextPage - 1].first,
page = nextPage
)
LoadResult.Page(result, if (nextPage == 1) null else nextPage, nextPage + 1)
} catch (e: Exception) {
LoadResult.Error(e)
}
}


}
9 changes: 9 additions & 0 deletions app/src/main/java/com/danteandroi/composewall/data/UiEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.danteandroi.composewall.data

/**
* @author Du Wenyu
* 2022/8/9
*/
enum class UiEvent {
ScrollToTop, Refresh
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.danteandroi.composewall.net

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.danteandroi.composewall.MainActivity
import com.danteandroi.composewall.MenuItem
import com.danteandroi.composewall.data.Image
import com.danteandroi.composewall.data.LoadingUiState
Expand Down Expand Up @@ -46,7 +46,7 @@ class ImageViewModel(private val imageRepository: ImageRepository = ImageReposit
}
}

fun preloadImages(menuItem: MenuItem, index: Int, page: Int = 1) {
fun preloadImages(context: Context, menuItem: MenuItem, index: Int, page: Int = 1) {
viewModelScope.safeLaunch {
val result = imageRepository.fetchImages(
apiClazz = menuItem.apiClazz,
Expand All @@ -55,7 +55,7 @@ class ImageViewModel(private val imageRepository: ImageRepository = ImageReposit
page = page
)
result.forEach {
val drawables = MainActivity.context.preloadImage(it.url)
val drawables = context.preloadImage(it.url)
Timber.d("preload image $drawables")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.danteandroi.composewall.ui.component

import androidx.compose.material.BackdropScaffold
import androidx.compose.material.BackdropValue
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.rememberBackdropScaffoldState
import androidx.compose.material.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
Expand All @@ -21,18 +18,18 @@ import kotlinx.coroutines.launch
@Composable
fun BackdropScaffold(
modifier: Modifier = Modifier,
scaffoldState: BackdropScaffoldState,
isExpandedScreen: Boolean = false,
onViewImage: (String, ImageViewModel) -> Unit = { _, _ -> }
) {
val scaffoldState = rememberBackdropScaffoldState(BackdropValue.Concealed)
var currentMenu by remember {
mutableStateOf(0)
}
val coroutine = rememberCoroutineScope()
BackdropScaffold(
modifier = modifier,
scaffoldState = scaffoldState,
gesturesEnabled = false,
gesturesEnabled = true,
backLayerBackgroundColor = MaterialTheme.colorScheme.primary,
appBar = {
BackdropTitle(
Expand Down Expand Up @@ -69,8 +66,9 @@ fun BackdropScaffold(
)
}

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun ComposeBackdropScaffoldPreview() {
BackdropScaffold()
BackdropScaffold(scaffoldState = rememberBackdropScaffoldState(initialValue = BackdropValue.Concealed))
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -38,7 +37,7 @@ fun BackdropTitle(
text = title,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onPrimary,
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.SemiBold)
style = MaterialTheme.typography.titleMedium
)
Spacer(modifier = modifier.height(spaceHeight))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.core.graphics.drawable.toBitmap
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.imageLoader
import coil.request.ImageRequest
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.IntentUtils
import com.danteandroi.composewall.MainActivity
import com.danteandroi.composewall.R
import com.danteandroi.composewall.data.Image
import com.danteandroi.composewall.data.ImageDetailState
Expand Down Expand Up @@ -65,6 +65,8 @@ fun DetailScreen(
}
val scope = rememberCoroutineScope()
if (showOptions) {
val context = LocalContext.current
val appName = stringResource(id = R.string.app_name)
OptionsDialog(
onDismissRequest = {
showOptions = false
Expand All @@ -76,27 +78,27 @@ fun DetailScreen(
onItemClick = { index ->
when (index) {
0 -> scope.launch {
MainActivity.context?.preloadImage(image.url)?.let { drwable ->
context.preloadImage(image.url)?.let { drawable ->
ImageUtils.save2Album(
drwable.toBitmap(),
drawable.toBitmap(),
appName,
Bitmap.CompressFormat.JPEG
)
snackBarManager.showMessages(R.string.save_picture_success)
}
}
1 -> scope.launch {
MainActivity.context?.let { context ->
context.preloadImage(image.url)?.let { drawable ->
val file = ImageUtils.save2Album(
drawable.toBitmap(),
Bitmap.CompressFormat.JPEG
)
context.startActivity(
IntentUtils.getShareImageIntent(
file
)
context.preloadImage(image.url)?.let { drawable ->
val file = ImageUtils.save2Album(
drawable.toBitmap(),
appName,
Bitmap.CompressFormat.JPEG
)
context.startActivity(
IntentUtils.getShareImageIntent(
file
)
}
)
}
}
}
Expand All @@ -108,7 +110,7 @@ fun DetailScreen(
.clickable {
navigateUp.invoke()
}
.background(Color.Black.copy(alpha = 0.8f))
.background(Color.Black.copy(alpha = 0.85f))
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Expand Down
Loading

0 comments on commit 633cd58

Please sign in to comment.