From bd4c2b058631a027a33f44ee06246b8e2631c99a Mon Sep 17 00:00:00 2001 From: skydoves Date: Wed, 25 Jan 2023 21:50:22 +0900 Subject: [PATCH] Update sample codes --- README.md | 10 +-- .../com/skydoves/whatifdemo/MainActivity.kt | 42 ++++++++-- .../com/skydoves/whatifdemo/theme/Color.kt | 27 +++++++ .../com/skydoves/whatifdemo/theme/Type.kt | 80 +++++++++++++++++++ .../skydoves/whatifdemo/theme/WhatIfTheme.kt | 67 ++++++++++++++++ 5 files changed, 212 insertions(+), 14 deletions(-) create mode 100644 app/src/main/kotlin/com/skydoves/whatifdemo/theme/Color.kt create mode 100644 app/src/main/kotlin/com/skydoves/whatifdemo/theme/Type.kt create mode 100644 app/src/main/kotlin/com/skydoves/whatifdemo/theme/WhatIfTheme.kt diff --git a/README.md b/README.md index 3581da7..ab8f35f 100644 --- a/README.md +++ b/README.md @@ -23,15 +23,7 @@ ![downloads](https://user-images.githubusercontent.com/24237865/101273131-2187a980-37d6-11eb-9000-e1cd10f87b0d.png) ### Gradle -Add the codes below to your **root** `build.gradle` file: -```gradle -allprojects { - repositories { - mavenCentral() - } -} -``` -Next, add the dependency below to your module's build.gradle file: +Add the dependency below to your module's `build.gradle` file: ```gradle dependencies { implementation "com.github.skydoves:whatif:1.1.1" diff --git a/app/src/main/kotlin/com/skydoves/whatifdemo/MainActivity.kt b/app/src/main/kotlin/com/skydoves/whatifdemo/MainActivity.kt index 8fcddc3..c96d0ce 100644 --- a/app/src/main/kotlin/com/skydoves/whatifdemo/MainActivity.kt +++ b/app/src/main/kotlin/com/skydoves/whatifdemo/MainActivity.kt @@ -18,20 +18,52 @@ package com.skydoves.whatifdemo import android.os.Bundle import android.util.Log -import androidx.appcompat.app.AppCompatActivity +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.size +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +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.unit.dp import com.skydoves.whatif.whatIf import com.skydoves.whatif.whatIfMap import com.skydoves.whatif.whatIfNotNull -import com.skydoves.whatifdemo.databinding.ActivityMainBinding +import com.skydoves.whatifdemo.theme.WhatIfTheme +import com.skydoves.whatifdemo.theme.background -class MainActivity : AppCompatActivity() { +class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val binding = ActivityMainBinding.inflate(layoutInflater) - setContentView(binding.root) + whatIfExamples() + setContent { + WhatIfTheme { + var isBlueColor by remember { mutableStateOf(false) } + + Box(modifier = Modifier.fillMaxSize()) { + Box( + modifier = Modifier + .align(Alignment.Center) + .clickable { isBlueColor = !isBlueColor } + .whatIfMap(isBlueColor, { it.background(Color.Blue) }, { it.background(Color.Cyan) }) + .whatIfMap(isBlueColor, { it.size(120.dp) }, { it.size(240.dp) }) + ) + } + } + } + } + + private fun whatIfExamples() { val nullableBoolean: Boolean? = true var nullableString: String? = null diff --git a/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Color.kt b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Color.kt new file mode 100644 index 0000000..ee0f2c8 --- /dev/null +++ b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Color.kt @@ -0,0 +1,27 @@ +/* + * Designed and developed by 2019-2023 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.skydoves.whatifdemo.theme + +import androidx.compose.ui.graphics.Color + +val purple200 = Color(0xFF651FFF) +val purple500 = Color(0xFF6200EA) +val background = Color(0xFF2B292B) +val background800 = Color(0xFF424242) +val background900 = Color(0xFF212121) +val white87 = Color(0Xddffffff) +val shimmerHighLight = Color(0xA3C2C2C2) diff --git a/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Type.kt b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Type.kt new file mode 100644 index 0000000..1ccec8e --- /dev/null +++ b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/Type.kt @@ -0,0 +1,80 @@ +/* + * Designed and developed by 2019-2023 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.skydoves.whatifdemo.theme + +import androidx.compose.material.Typography +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// set of dark material typography styles to start with. +val DarkTypography = Typography( + h1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Bold, + color = Color.White, + fontSize = 28.sp + ), + h2 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Bold, + color = Color.White, + fontSize = 21.sp + ), + body1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + color = Color.White, + fontSize = 14.sp + ), + body2 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + color = white87, + fontSize = 14.sp + ) +) + +// set of light material typography styles to start with. +val LightTypography = Typography( + h1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Bold, + color = background900, + fontSize = 28.sp + ), + h2 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Bold, + color = background900, + fontSize = 21.sp + ), + body1 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + color = background800, + fontSize = 14.sp + ), + body2 = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + color = background800, + fontSize = 14.sp + ) +) diff --git a/app/src/main/kotlin/com/skydoves/whatifdemo/theme/WhatIfTheme.kt b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/WhatIfTheme.kt new file mode 100644 index 0000000..cee9c69 --- /dev/null +++ b/app/src/main/kotlin/com/skydoves/whatifdemo/theme/WhatIfTheme.kt @@ -0,0 +1,67 @@ +/* + * Designed and developed by 2019-2023 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.skydoves.whatifdemo.theme + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material.MaterialTheme +import androidx.compose.material.darkColors +import androidx.compose.material.lightColors +import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.Color + +private val DarkColorPalette = darkColors( + onBackground = background800, + primary = purple200, + primaryVariant = purple500, + secondary = purple500, + onPrimary = Color.White, + onSecondary = Color.White +) + +private val LightColorPalette = lightColors( + onBackground = Color.White, + surface = Color.White, + primary = purple200, + primaryVariant = purple500, + secondary = purple500, + onPrimary = Color.White, + onSecondary = Color.White +) + +@Composable +fun WhatIfTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + content: @Composable () -> Unit +) { + val colors = if (darkTheme) { + DarkColorPalette + } else { + LightColorPalette + } + + val typography = if (darkTheme) { + DarkTypography + } else { + LightTypography + } + + MaterialTheme( + colors = colors, + typography = typography, + content = content + ) +}