diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 096e2cb4..685e5e8e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -23,6 +23,7 @@ import ml.docilealligator.infinityforreddit.commentfilter.CommentFilterUsage; import ml.docilealligator.infinityforreddit.commentfilter.CommentFilterUsageDao; import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeDao; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeDaoKt; import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubreddit; import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubredditDao; import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubredditDaoKt; @@ -85,6 +86,8 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { public abstract CustomThemeDao customThemeDao(); + public abstract CustomThemeDaoKt customThemeDaoKt(); + public abstract RecentSearchQueryDao recentSearchQueryDao(); public abstract ReadPostDao readPostDao(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java index 5f4acf00..172d36e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java @@ -8,8 +8,6 @@ import androidx.room.Query; import java.util.List; -import kotlinx.coroutines.flow.Flow; - @Dao public interface CustomThemeDao { @Insert(onConflict = OnConflictStrategy.REPLACE) @@ -42,15 +40,6 @@ public interface CustomThemeDao { @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") LiveData getAmoledCustomThemeLiveData(); - @Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1") - Flow getLightCustomThemeFlow(); - - @Query("SELECT * FROM custom_themes WHERE is_dark_theme = 1 LIMIT 1") - Flow getDarkCustomThemeFlow(); - - @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") - Flow getAmoledCustomThemeFlow(); - @Query("SELECT * FROM custom_themes WHERE name = :name COLLATE NOCASE LIMIT 1") CustomTheme getCustomTheme(String name); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDaoKt.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDaoKt.kt new file mode 100644 index 00000000..d52c17e0 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDaoKt.kt @@ -0,0 +1,17 @@ +package ml.docilealligator.infinityforreddit.customtheme + +import androidx.room.Dao +import androidx.room.Query +import kotlinx.coroutines.flow.Flow + +@Dao +interface CustomThemeDaoKt { + @Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1") + fun getLightCustomThemeFlow(): Flow + + @Query("SELECT * FROM custom_themes WHERE is_dark_theme = 1 LIMIT 1") + fun getDarkCustomThemeFlow(): Flow + + @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") + fun getAmoledCustomThemeFlow(): Flow +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/LocalCustomThemeRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/LocalCustomThemeRepository.java index 81886d7e..a355382c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/LocalCustomThemeRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/LocalCustomThemeRepository.java @@ -23,9 +23,9 @@ public class LocalCustomThemeRepository { mCurrentDarkCustomTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomThemeLiveData(); mCurrentAmoledCustomTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomThemeLiveData(); - mCurrentLightCustomThemeFlow = redditDataRoomDatabase.customThemeDao().getLightCustomThemeFlow(); - mCurrentDarkCustomThemeFlow = redditDataRoomDatabase.customThemeDao().getDarkCustomThemeFlow(); - mCurrentAmoledCustomThemeFlow = redditDataRoomDatabase.customThemeDao().getAmoledCustomThemeFlow(); + mCurrentLightCustomThemeFlow = redditDataRoomDatabase.customThemeDaoKt().getLightCustomThemeFlow(); + mCurrentDarkCustomThemeFlow = redditDataRoomDatabase.customThemeDaoKt().getDarkCustomThemeFlow(); + mCurrentAmoledCustomThemeFlow = redditDataRoomDatabase.customThemeDaoKt().getAmoledCustomThemeFlow(); } LiveData> getAllCustomThemes() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/compose/AppTheme.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/compose/AppTheme.kt index ab28e038..a7a67680 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/compose/AppTheme.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/compose/AppTheme.kt @@ -6,9 +6,12 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.platform.LocalContext -import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach import ml.docilealligator.infinityforreddit.Infinity import ml.docilealligator.infinityforreddit.customtheme.CustomTheme import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper @@ -23,17 +26,21 @@ val LocalAppTheme = staticCompositionLocalOf { fun AppTheme(themeType: Int, content: @Composable () -> Unit) { val context = LocalContext.current val localCustomThemeRepository = LocalCustomThemeRepository(((context.applicationContext) as Infinity).mRedditDataRoomDatabase) + var themeLoaded by remember { mutableStateOf(false) } val currentThemeFlow = when(themeType) { CustomThemeSharedPreferencesUtils.LIGHT -> localCustomThemeRepository.currentLightCustomThemeFlow CustomThemeSharedPreferencesUtils.DARK -> localCustomThemeRepository.currentDarkCustomThemeFlow CustomThemeSharedPreferencesUtils.AMOLED -> localCustomThemeRepository.currentAmoledCustomThemeFlow else -> localCustomThemeRepository.currentLightCustomThemeFlow + }.onEach { + themeLoaded = true } val customTheme by currentThemeFlow.collectAsState(initial = null) - customTheme?.let { - CompositionLocalProvider(LocalAppTheme provides it) { + + if (themeLoaded) { + CompositionLocalProvider(LocalAppTheme provides (customTheme ?: getDefaultTheme(context, themeType))) { MaterialTheme { content() }