From b7a821b9f6c0e375cb33f3db6673887208e88ab5 Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Sat, 7 Jan 2023 01:15:04 +0100 Subject: [PATCH] Add compose to project. display history with compose. --- app/build.gradle | 25 ++++- app/src/main/AndroidManifest.xml | 10 +- .../ui/history/HistoryFragment.kt | 96 ++++++++++++------- .../ui/history/HistoryFragmentCompose.kt | 13 +++ .../ui/history/HistoryFragmentViewModel.kt | 1 + .../tichucounter/ui/history/ui/theme/Color.kt | 11 +++ .../tichucounter/ui/history/ui/theme/Theme.kt | 68 +++++++++++++ .../tichucounter/ui/history/ui/theme/Type.kt | 34 +++++++ .../main/res/drawable/ic_baseline_add_24.xml | 2 +- .../main/res/drawable/ic_baseline_undo_24.xml | 2 +- app/src/main/res/values/colors.xml | 12 ++- .../res/values/ic_launcher_background.xml | 2 +- app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 8 -- app/src/main/res/values/themes.xml | 13 ++- build.gradle | 3 + 16 files changed, 240 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentCompose.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Color.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Theme.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Type.kt diff --git a/app/build.gradle b/app/build.gradle index e35b03f..02b4465 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,13 +26,16 @@ android { defaultConfig { applicationId "me.zobrist.tichucounter" - minSdkVersion 16 + minSdkVersion 21 targetSdkVersion 33 versionCode versionProperties["versionCode"].toInteger() versionName "1.1.0Beta1" resConfigs("de", "en") testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true + vectorDrawables { + useSupportLibrary true + } } signingConfigs { create("release") { @@ -53,6 +56,11 @@ android { buildFeatures { viewBinding = true + compose = true + } + + composeOptions { + kotlinCompilerExtensionVersion = "1.3.2" } compileOptions { @@ -63,6 +71,11 @@ android { jvmTarget = '1.8' } namespace 'me.zobrist.tichucounter' + packagingOptions { + resources { + excludes += '/META-INF/{AL2.0,LGPL2.1}' + } + } } dependencies { @@ -70,7 +83,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.0-rc01' - implementation 'com.google.android.material:material:1.7.0' + implementation "androidx.compose.material3:material3:1.0.1" implementation 'com.google.android.play:core-ktx:1.8.1' implementation 'com.google.android.play:core-ktx:1.8.1' implementation 'com.google.code.gson:gson:2.8.9' @@ -83,10 +96,18 @@ dependencies { implementation 'androidx.fragment:fragment-ktx:1.5.5' implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.recyclerview:recyclerview:1.2.1' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' + implementation 'androidx.activity:activity-compose:1.3.1' + implementation "androidx.compose.ui:ui:$compose_version" + implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" + implementation "androidx.compose.runtime:runtime-livedata:$compose_version" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation "com.google.dagger:hilt-android:2.44" + androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" + debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" + debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" kapt "com.google.dagger:hilt-compiler:2.44" implementation("androidx.room:room-runtime:2.4.3") annotationProcessor("androidx.room:room-compiler:2.4.3") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 66ba258..c2cf1cd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,12 +12,16 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + + diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragment.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragment.kt index ce96490..791253a 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragment.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragment.kt @@ -4,13 +4,28 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Card +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.ViewCompositionStrategy +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels -import androidx.recyclerview.widget.GridLayoutManager -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView import dagger.hilt.android.AndroidEntryPoint -import me.zobrist.tichucounter.R +import me.zobrist.tichucounter.data.GameAndScore + /** * A fragment representing a list of Items. @@ -19,50 +34,57 @@ import me.zobrist.tichucounter.R class HistoryFragment : Fragment() { private val viewModel: HistoryFragmentViewModel by activityViewModels() - - private var columnCount = 1 - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - arguments?.let { - columnCount = it.getInt(ARG_COLUMN_COUNT) - } - } + override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, + inflater: LayoutInflater, + container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - val view = inflater.inflate(R.layout.fragment_history_list, container, false) - - // Set the adapter - if (view is RecyclerView) { - with(view) { - layoutManager = when { - columnCount <= 1 -> LinearLayoutManager(context) - else -> GridLayoutManager(context, columnCount) - } - - viewModel.gameAndHistory.observe(viewLifecycleOwner) { - adapter = MyGameRecyclerViewAdapter(it) + ): View { + return ComposeView(requireContext()).apply { + // Dispose of the Composition when the view's LifecycleOwner + // is destroyed + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) + setContent { + MaterialTheme { + // In Compose world + Text("Hello Compose!") + HistoryList(viewModel) } } } - return view } - companion object { + @Composable + fun HistoryList(viewModel: HistoryFragmentViewModel) { + val games = viewModel.gameAndHistory.observeAsState().value - const val ARG_COLUMN_COUNT = "1" - - @JvmStatic - fun newInstance(columnCount: Int) = - HistoryFragment().apply { - arguments = Bundle().apply { - putInt(ARG_COLUMN_COUNT, columnCount) + LazyColumn { + if(games != null) + { + items(games) { + HistoryListItem(it) } } + } + } + + @Composable + fun HistoryListItem(game: GameAndScore){ + Card() { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(horizontal = 15.dp, vertical = 10.dp) + ){ + Text( text = game.nameA + " - " + game.nameB) + Column( + modifier = Modifier.padding(start = 10.dp) + ){ + Text(text = game.scoreA.toString() + ":" + game.scoreB, fontSize = 22.sp) + Text(text = game.modified.toString(), fontSize = 18.sp) + } + } + } } } \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentCompose.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentCompose.kt new file mode 100644 index 0000000..c9d4674 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentCompose.kt @@ -0,0 +1,13 @@ +package me.zobrist.tichucounter.ui.history +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Card +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import me.zobrist.tichucounter.data.GameAndScore + diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentViewModel.kt index 3bb8fb9..eca6b12 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryFragmentViewModel.kt @@ -1,5 +1,6 @@ package me.zobrist.tichucounter.ui.history +import androidx.compose.runtime.State import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Color.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Color.kt new file mode 100644 index 0000000..31a01f3 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package me.zobrist.tichucounter.ui.history.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Theme.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Theme.kt new file mode 100644 index 0000000..37ccda6 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Theme.kt @@ -0,0 +1,68 @@ +package me.zobrist.tichucounter.ui.history.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.SideEffect +import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView +import androidx.core.view.ViewCompat + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun TichuCounterTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + val view = LocalView.current + if (!view.isInEditMode) { + SideEffect { + (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb() + ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme + } + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Type.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Type.kt new file mode 100644 index 0000000..5314447 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package me.zobrist.tichucounter.ui.history.ui.theme + +import androidx.compose.material3.Typography +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 Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_baseline_add_24.xml b/app/src/main/res/drawable/ic_baseline_add_24.xml index d9b76ed..7747a64 100644 --- a/app/src/main/res/drawable/ic_baseline_add_24.xml +++ b/app/src/main/res/drawable/ic_baseline_add_24.xml @@ -5,6 +5,6 @@ android:viewportWidth="24" android:viewportHeight="24"> diff --git a/app/src/main/res/drawable/ic_baseline_undo_24.xml b/app/src/main/res/drawable/ic_baseline_undo_24.xml index 6e3ff9b..f3e3911 100644 --- a/app/src/main/res/drawable/ic_baseline_undo_24.xml +++ b/app/src/main/res/drawable/ic_baseline_undo_24.xml @@ -6,6 +6,6 @@ android:viewportWidth="24" android:viewportHeight="24"> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index c2e1ee9..002e160 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,6 +1,12 @@ - @color/ic_launcher_background - #830000 - #F57F17 + #d50000 + #ff5131 + #9b0000 + #ffccbc + #ffffee + #cb9b8c + #ffffff + #000000 + \ No newline at end of file diff --git a/app/src/main/res/values/ic_launcher_background.xml b/app/src/main/res/values/ic_launcher_background.xml index 939714f..d8bfdb7 100644 --- a/app/src/main/res/values/ic_launcher_background.xml +++ b/app/src/main/res/values/ic_launcher_background.xml @@ -1,4 +1,4 @@ - #DC0E00 + @color/primaryColor \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4513ac8..16c438b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,4 +30,5 @@ Activate Delete Submit + MainActivity \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 49592f5..6b30d8d 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,11 +1,3 @@ - - \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index d907dd5..be19a8f 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,11 +1,14 @@ - -