Add Jetpack Compose ColorScheme

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-01 15:00:25 +01:00 committed by Alper Öztürk
parent 1f31127df3
commit b4c5de4e7e
8 changed files with 87 additions and 27 deletions

View file

@ -48,7 +48,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@ -143,8 +142,6 @@ fun AssistantScreen(viewModel: AssistantViewModel) {
if (showDeleteTaskAlertDialog) {
taskIdToDeleted?.let { id ->
SimpleAlertDialog(
backgroundColor = Color.White,
textColor = Color.Black,
title = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_title),
description = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_description),
dismiss = { showDeleteTaskAlertDialog = false },

View file

@ -29,7 +29,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import com.nextcloud.client.assistant.AssistantViewModel
@ -44,8 +43,6 @@ fun AddTaskAlertDialog(viewModel: AssistantViewModel, taskType: TaskType, dismis
}
SimpleAlertDialog(
backgroundColor = Color.White,
textColor = Color.Black,
title = taskType.name ?: "",
description = taskType.description,
dismiss = { dismiss() },

View file

@ -27,7 +27,6 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
@ -37,7 +36,6 @@ fun CenterText(text: String) {
Text(
text = text,
fontSize = 18.sp,
color = Color.Black,
textAlign = TextAlign.Center
)
}

View file

@ -36,6 +36,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@ -46,6 +47,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.nextcloud.ui.composeActivity.ComposeActivity.Companion.schemeFlow
import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet
import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.model.Task
@ -58,6 +60,7 @@ fun TaskView(
task: Task,
showDeleteTaskAlertDialog: (Long) -> Unit
) {
val scheme = schemeFlow.collectAsState().value
var expanded by remember { mutableStateOf(false) }
var showMoreActionsBottomSheet by remember { mutableStateOf(false) }
@ -66,7 +69,7 @@ fun TaskView(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(Color(R.color.primary))
.background(scheme.primary)
.combinedClickable(onClick = {
expanded = !expanded
}, onLongClick = {

View file

@ -24,6 +24,8 @@ package com.nextcloud.ui.composeActivity
import android.content.Context
import android.os.Bundle
import android.view.MenuItem
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@ -35,6 +37,7 @@ import com.nextcloud.client.assistant.AssistantViewModel
import com.nextcloud.common.NextcloudClient
import com.nextcloud.common.User
import com.nextcloud.utils.extensions.getSerializableArgument
import com.nextcloud.utils.extensions.toColorScheme
import com.owncloud.android.R
import com.owncloud.android.databinding.ActivityComposeBinding
import com.owncloud.android.lib.common.OwnCloudClientFactory
@ -42,6 +45,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.ui.activity.DrawerActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.withContext
class ComposeActivity : DrawerActivity() {
@ -52,6 +56,8 @@ class ComposeActivity : DrawerActivity() {
const val destinationKey = "destinationKey"
const val titleKey = "titleKey"
const val menuItemKey = "menuItemKey"
lateinit var schemeFlow: MutableStateFlow<ColorScheme>
}
override fun onCreate(savedInstanceState: Bundle?) {
@ -68,8 +74,15 @@ class ComposeActivity : DrawerActivity() {
setupDrawer(menuItemId)
schemeFlow = MutableStateFlow(viewThemeUtils.material.getScheme(this).toColorScheme())
binding.composeView.setContent {
Content(destination, storageManager.user, this)
MaterialTheme(
colorScheme = schemeFlow.value,
content = {
Content(destination, storageManager.user, this)
}
)
}
}
@ -79,7 +92,7 @@ class ComposeActivity : DrawerActivity() {
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
var retval = true
var result = true
if (item.itemId == android.R.id.home) {
if (isDrawerOpen) {
closeDrawer()
@ -87,9 +100,9 @@ class ComposeActivity : DrawerActivity() {
openDrawer()
}
} else {
retval = super.onOptionsItemSelected(item)
result = super.onOptionsItemSelected(item)
}
return retval
return result
}
@Composable

View file

@ -31,7 +31,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.owncloud.android.R
@ -39,8 +38,6 @@ import com.owncloud.android.R
@Suppress("LongParameterList")
@Composable
fun SimpleAlertDialog(
backgroundColor: Color,
textColor: Color,
title: String,
description: String?,
heightFraction: Float? = null,
@ -57,15 +54,14 @@ fun SimpleAlertDialog(
}
AlertDialog(
containerColor = backgroundColor,
onDismissRequest = { dismiss() },
title = {
Text(text = title, color = textColor)
Text(text = title)
},
text = {
Column(modifier = modifier) {
if (description != null) {
Text(text = description, color = textColor)
description?.let {
Text(text = description)
}
content?.let {
@ -81,16 +77,14 @@ fun SimpleAlertDialog(
dismiss()
}) {
Text(
stringResource(id = R.string.common_ok),
color = textColor
stringResource(id = R.string.common_ok)
)
}
},
dismissButton = {
TextButton(onClick = { dismiss() }) {
Text(
stringResource(id = R.string.common_cancel),
color = textColor
stringResource(id = R.string.common_cancel)
)
}
}

View file

@ -21,12 +21,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.owncloud.android.R
import kotlinx.coroutines.launch
@SuppressLint("ResourceAsColor")
@ -82,7 +80,6 @@ fun MoreActionsBottomSheet(
Icon(
painter = painterResource(id = action.first),
contentDescription = "action icon",
tint = Color(R.color.secondary_button_background_color),
modifier = Modifier.size(20.dp)
)

View file

@ -0,0 +1,61 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2024 Alper Ozturk
* Copyright (C) 2024 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.utils.extensions
import androidx.compose.material3.ColorScheme
import androidx.compose.ui.graphics.Color
import com.vanniktech.ui.color
import scheme.Scheme
fun Scheme.toColorScheme(): ColorScheme {
return ColorScheme(
primary = Color(primary.color.argb),
onPrimary = Color(onPrimary.color.argb),
primaryContainer = Color(primaryContainer.color.argb),
onPrimaryContainer = Color(onPrimaryContainer.color.argb),
inversePrimary = Color(inversePrimary.color.argb),
secondary = Color(secondary.color.argb),
onSecondary = Color(onSecondary.color.argb),
secondaryContainer = Color(secondaryContainer.color.argb),
onSecondaryContainer = Color(onSecondaryContainer.color.argb),
tertiary = Color(tertiary.color.argb),
onTertiary = Color(onTertiary.color.argb),
tertiaryContainer = Color(tertiaryContainer.color.argb),
onTertiaryContainer = Color(onTertiaryContainer.color.argb),
background = Color(background.color.argb),
onBackground = Color(onBackground.color.argb),
surface = Color(surface.color.argb),
onSurface = Color(onSurface.color.argb),
surfaceVariant = Color(surfaceVariant.color.argb),
onSurfaceVariant = Color(onSurfaceVariant.color.argb),
surfaceTint = Color(surfaceVariant.color.argb),
inverseSurface = Color(inverseSurface.color.argb),
inverseOnSurface = Color(inverseOnSurface.color.argb),
error = Color(error.color.argb),
onError = Color(onError.color.argb),
errorContainer = Color(errorContainer.color.argb),
onErrorContainer = Color(onErrorContainer.color.argb),
outline = Color(outline.color.argb),
outlineVariant = Color(outlineVariant.color.argb),
scrim = Color(scrim.color.argb)
)
}