Skip to content

Commit

Permalink
[feature] Introducing Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Apr 17, 2024
1 parent 5dd78f3 commit d6e2853
Show file tree
Hide file tree
Showing 22 changed files with 918 additions and 185 deletions.
19 changes: 17 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
applicationId = "com.skyd.anivu"
minSdk = 24
targetSdk = 34
versionCode = 10
versionName = "1.1-beta07"
versionCode = 11
versionName = "1.1-beta08"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -100,9 +100,13 @@ android {
jvmTarget = "17"
}
buildFeatures {
compose = true
viewBinding = true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.11"
}
packaging {
resources.excludes += mutableSetOf(
"DebugProbesKt.bin",
Expand All @@ -118,7 +122,14 @@ android {
tasks.withType(KotlinCompile::class.java).configureEach {
kotlinOptions {
freeCompilerArgs += listOf(
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
"-opt-in=androidx.compose.material.ExperimentalMaterialApi",
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-opt-in=coil.annotation.ExperimentalCoilApi",
"-opt-in=androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi",
"-opt-in=androidx.compose.foundation.layout.ExperimentalLayoutApi",
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=kotlinx.coroutines.FlowPreview",
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
Expand All @@ -133,6 +144,10 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("androidx.compose.ui:ui:1.6.5")
implementation("androidx.compose.material3:material3:1.2.1")
implementation("androidx.compose.material3:material3-window-size-class:1.2.1")
implementation("com.materialkolor:material-kolor:1.4.4")
implementation("androidx.room:room-runtime:2.6.1")
implementation("androidx.room:room-ktx:2.6.1")
implementation("androidx.room:room-paging:2.6.1")
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/skyd/anivu/base/BaseComposeFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.skyd.anivu.base

import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.navigation.NavHostController
import com.skyd.anivu.ext.findMainNavController
import com.skyd.anivu.model.preference.SettingsProvider
import com.skyd.anivu.ui.local.LocalDarkMode
import com.skyd.anivu.ui.local.LocalNavController
import com.skyd.anivu.ui.local.LocalWindowSizeClass
import com.skyd.anivu.ui.theme.AniVuTheme


abstract class BaseComposeFragment : Fragment() {
private lateinit var navController: NavHostController
fun setContentBase(content: @Composable () -> Unit): ComposeView {
navController = findMainNavController() as NavHostController
return ComposeView(requireContext()).apply {
setContent {
CompositionLocalProvider(
LocalNavController provides navController,
LocalWindowSizeClass provides calculateWindowSizeClass(requireActivity())
) {
SettingsProvider { AniVuTheme(darkTheme = LocalDarkMode.current, content) }
}
}
}
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/PaddingValuesExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.skyd.anivu.ext

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalLayoutDirection

@Composable
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues = PaddingValues(
top = calculateTopPadding() + other.calculateTopPadding(),
bottom = calculateBottomPadding() + other.calculateBottomPadding(),
start = calculateStartPadding(LocalLayoutDirection.current) +
other.calculateStartPadding(LocalLayoutDirection.current),
end = calculateEndPadding(LocalLayoutDirection.current) +
other.calculateEndPadding(LocalLayoutDirection.current)
)
14 changes: 14 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/PreferenceExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.skyd.anivu.ext

import androidx.datastore.preferences.core.Preferences
import com.skyd.anivu.model.preference.Settings
import com.skyd.anivu.model.preference.appearance.DarkModePreference
import com.skyd.anivu.model.preference.appearance.ThemePreference

fun Preferences.toSettings(): Settings {
return Settings(
// Theme
theme = ThemePreference.fromPreferences(this),
darkMode = DarkModePreference.fromPreferences(this),
)
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/skyd/anivu/model/preference/Settings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.skyd.anivu.model.preference

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.toSettings
import com.skyd.anivu.model.preference.appearance.DarkModePreference
import com.skyd.anivu.model.preference.appearance.ThemePreference
import com.skyd.anivu.ui.local.LocalDarkMode
import com.skyd.anivu.ui.local.LocalTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.map

data class Settings(
// Theme
val theme: String = ThemePreference.default,
val darkMode: Int = DarkModePreference.default,
)

@Composable
fun SettingsProvider(
content: @Composable () -> Unit,
) {
val context = LocalContext.current
val settings by remember { context.dataStore.data.map { it.toSettings() } }
.collectAsState(initial = Settings(), context = Dispatchers.Default)

CompositionLocalProvider(
// Theme
LocalTheme provides settings.theme,
LocalDarkMode provides settings.darkMode,
) {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.skyd.anivu.model.preference.appearance
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.intPreferencesKey
import com.skyd.anivu.R
Expand Down Expand Up @@ -66,4 +69,12 @@ object DarkModePreference : BasePreference<Int> {
}
return value
}

@Composable
@ReadOnlyComposable
fun inDark(value: Int) = when (value) {
AppCompatDelegate.MODE_NIGHT_YES -> true
AppCompatDelegate.MODE_NIGHT_NO -> false
else -> isSystemInDarkTheme()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.skyd.anivu.model.preference.appearance

import android.content.Context
import androidx.compose.ui.graphics.Color
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import com.google.android.material.color.DynamicColors
Expand Down Expand Up @@ -77,4 +78,16 @@ object ThemePreference : BasePreference<String> {
PURPLE -> R.style.Theme_AniVu_Purple
else -> R.style.Theme_AniVu_Pink
}

fun toSeedColor(
context: Context,
value: String = context.dataStore.getOrDefault(this),
): Color = when (value) {
PINK -> Color(0xFF884A69)
GREEN -> Color(0xFF406836)
BLUE -> Color(0xFF3A608F)
YELLOW -> Color(0xFF6C5E10)
PURPLE -> Color(0xFF65558F)
else -> Color(0xFF884A69)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.skyd.anivu.databinding.ItemColorPalette1Binding
import com.skyd.anivu.databinding.ItemDownload1Binding
import com.skyd.anivu.databinding.ItemEnclosure1Binding
import com.skyd.anivu.databinding.ItemFeed1Binding
import com.skyd.anivu.databinding.ItemLicense1Binding
import com.skyd.anivu.databinding.ItemLinkEnclosure1Binding
import com.skyd.anivu.databinding.ItemMedia1Binding
import com.skyd.anivu.databinding.ItemMore1Binding
Expand Down Expand Up @@ -48,6 +47,3 @@ class ColorPalette1ViewHolder(binding: ItemColorPalette1Binding) :

class OtherWorks1ViewHolder(binding: ItemOtherWorks1Binding) :
BaseViewHolder<ItemOtherWorks1Binding>(binding)

class License1ViewHolder(binding: ItemLicense1Binding) :
BaseViewHolder<ItemLicense1Binding>(binding)

This file was deleted.

Loading

0 comments on commit d6e2853

Please sign in to comment.