Skip to content

Commit

Permalink
completely refactor settings, implement theming.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyabsi committed Nov 30, 2024
1 parent dc66311 commit 7a6af61
Show file tree
Hide file tree
Showing 22 changed files with 1,160 additions and 711 deletions.
4 changes: 1 addition & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ android {
isDefault = true
dimension = "type"
}
create("gplay") {
dimension = "type"
}
create("quest") {
dimension = "type"
}
Expand Down Expand Up @@ -111,6 +108,7 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3-android:1.3.1")
implementation("androidx.lifecycle:lifecycle-runtime-compose-android:2.8.7")
implementation("androidx.appcompat:appcompat:1.7.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/cc/sovellus/vrcaa/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import android.content.Context
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import cc.sovellus.vrcaa.activity.CrashActivity
import cc.sovellus.vrcaa.extension.developerMode
import cc.sovellus.vrcaa.extension.networkLogging
import cc.sovellus.vrcaa.extension.minimalistMode
import cc.sovellus.vrcaa.helper.NotificationHelper

class App : Application() {
Expand All @@ -17,20 +18,27 @@ class App : Application() {
GlobalExceptionHandler.initialize(applicationContext, CrashActivity::class.java)
NotificationHelper.createNotificationChannels(this)

val preferences = getSharedPreferences("vrcaa_prefs", 0)

context = this
developerModeEnabled.value = this.getSharedPreferences("vrcaa_prefs", 0).developerMode

networkLogging.value = preferences.networkLogging
minimalistModeEnabled.value = preferences.minimalistMode
}

companion object {
@SuppressLint("StaticFieldLeak")
private lateinit var context: Context

private var developerModeEnabled: MutableState<Boolean> = mutableStateOf(false)
private var networkLogging: MutableState<Boolean> = mutableStateOf(false)
private var minimalistModeEnabled: MutableState<Boolean> = mutableStateOf(false)

private var loadingText: MutableState<String> = mutableStateOf("")

fun getContext(): Context { return context }

fun isDeveloperModeEnabled(): Boolean { return developerModeEnabled.value }
fun isNetworkLoggingEnabled(): Boolean { return networkLogging.value }
fun isMinimalistModeEnabled(): Boolean { return minimalistModeEnabled.value }

fun getLoadingText(): MutableState<String> { return loadingText }
fun setLoadingText(resourceId: Int) { loadingText.value = context.getString(resourceId) }
Expand Down
34 changes: 22 additions & 12 deletions app/src/main/java/cc/sovellus/vrcaa/activity/CrashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import cc.sovellus.vrcaa.GlobalExceptionHandler
import cc.sovellus.vrcaa.extension.currentThemeOption
import cc.sovellus.vrcaa.ui.screen.crash.CrashScreen
import cc.sovellus.vrcaa.ui.theme.LocalTheme
import cc.sovellus.vrcaa.ui.theme.Theme

class CrashActivity : ComponentActivity() {
Expand All @@ -21,19 +27,23 @@ class CrashActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
val exception = GlobalExceptionHandler.getThrowableFromIntent(intent)

val preferences = getSharedPreferences("vrcaa_prefs", MODE_PRIVATE)

setContent {
Theme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CrashScreen(
exception = exception,
onRestart = {
finishAffinity()
startActivity(Intent(this@CrashActivity, MainActivity::class.java))
}
)
CompositionLocalProvider(LocalTheme provides preferences.currentThemeOption) {
Theme(LocalTheme.current) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CrashScreen(
exception = exception,
onRestart = {
finishAffinity()
startActivity(Intent(this@CrashActivity, MainActivity::class.java))
}
)
}
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions app/src/main/java/cc/sovellus/vrcaa/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.app.ActivityCompat
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.NavigatorDisposeBehavior
import cafe.adriel.voyager.transitions.SlideTransition
import cc.sovellus.vrcaa.R
import cc.sovellus.vrcaa.extension.authToken
import cc.sovellus.vrcaa.extension.currentThemeOption
import cc.sovellus.vrcaa.manager.ApiManager.api
import cc.sovellus.vrcaa.manager.FeedManager
import cc.sovellus.vrcaa.service.PipelineService
import cc.sovellus.vrcaa.ui.screen.login.LoginScreen
import cc.sovellus.vrcaa.ui.screen.navigation.NavigationScreen
import cc.sovellus.vrcaa.ui.theme.LocalTheme
import cc.sovellus.vrcaa.ui.theme.Theme
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -82,13 +94,11 @@ class MainActivity : ComponentActivity() {
}

setContent {
Theme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Content(token.isNotBlank() && invalidSession == null && terminateSession == null)
}
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Content(token.isNotBlank() && invalidSession == null && terminateSession == null)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/cc/sovellus/vrcaa/api/BaseClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ open class BaseClient {
val response = client.newCall(request).await()
val responseBody = response.body?.string().toString()

if (App.isDeveloperModeEnabled()) {
if (App.isNetworkLoggingEnabled()) {
DebugManager.addDebugMetadata(
DebugManager.DebugMetadataData(
type = DebugManager.DebugType.DEBUG_TYPE_HTTP,
Expand All @@ -129,7 +129,7 @@ open class BaseClient {
val response = client.newCall(request).await()
val responseBody = response.body?.string().toString()

if (App.isDeveloperModeEnabled()) {
if (App.isNetworkLoggingEnabled()) {
DebugManager.addDebugMetadata(
DebugManager.DebugMetadataData(
type = DebugManager.DebugType.DEBUG_TYPE_HTTP,
Expand All @@ -154,7 +154,7 @@ open class BaseClient {
val response = client.newCall(request).await()
val responseBody = response.body?.string().toString()

if (App.isDeveloperModeEnabled()) {
if (App.isNetworkLoggingEnabled()) {
DebugManager.addDebugMetadata(
DebugManager.DebugMetadataData(
type = DebugManager.DebugType.DEBUG_TYPE_HTTP,
Expand All @@ -179,7 +179,7 @@ open class BaseClient {
val response = client.newCall(request).await()
val responseBody = response.body?.string().toString()

if (App.isDeveloperModeEnabled()) {
if (App.isNetworkLoggingEnabled()) {
DebugManager.addDebugMetadata(
DebugManager.DebugMetadataData(
type = DebugManager.DebugType.DEBUG_TYPE_HTTP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class VRChatPipeline(
}
}

if (App.isDeveloperModeEnabled()) {
if (App.isNetworkLoggingEnabled()) {
DebugManager.addDebugMetadata(
DebugManager.DebugMetadataData(
type = DebugManager.DebugType.DEBUG_TYPE_PIPELINE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ internal var SharedPreferences.avatarProvider: String
get() = getString("avatarProviderPreference", "avtrdb")!!
set(it) = edit { putString("avatarProviderPreference", it) }

internal var SharedPreferences.developerMode: Boolean
internal var SharedPreferences.networkLogging: Boolean
get() = getBoolean("isDeveloperModeEnabled", false)
set(it) = edit { putBoolean("isDeveloperModeEnabled", it) }

internal var SharedPreferences.lowDPIMode: Boolean
get() = getBoolean("isLowDPIModeEnabled", false)
set(it) = edit { putBoolean("isLowDPIModeEnabled", it) }
internal var SharedPreferences.minimalistMode: Boolean
get() = getBoolean("isMinimalistModeEnabled", false)
set(it) = edit { putBoolean("isMinimalistModeEnabled", it) }

internal var SharedPreferences.currentThemeOption: Int
get() = getInt("currentThemeOption", 2)
set(it) = edit { putInt("currentThemeOption", it) }
19 changes: 19 additions & 0 deletions app/src/main/java/cc/sovellus/vrcaa/manager/ThemeManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cc.sovellus.vrcaa.manager

object ThemeManager {
private var themeListeners: MutableList<ThemeListener> = mutableListOf()

interface ThemeListener {
fun onPreferenceUpdate(theme: Int)
}

fun addThemeListener(listener: ThemeListener) {
themeListeners.add(listener)
}

fun setTheme(theme: Int) {
themeListeners.map {
it.onPreferenceUpdate(theme)
}
}
}
43 changes: 3 additions & 40 deletions app/src/main/java/cc/sovellus/vrcaa/ui/screen/about/AboutScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Translate
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
Expand All @@ -43,6 +44,7 @@ import cafe.adriel.voyager.navigator.currentOrThrow
import cc.sovellus.vrcaa.App
import cc.sovellus.vrcaa.BuildConfig
import cc.sovellus.vrcaa.R
import cc.sovellus.vrcaa.extension.currentThemeOption
import cc.sovellus.vrcaa.ui.screen.licenses.LicensesScreen

class AboutScreen : Screen {
Expand Down Expand Up @@ -91,7 +93,7 @@ class AboutScreen : Screen {
contentAlignment = Alignment.TopCenter
) {
Image(
painter = if (isSystemInDarkTheme()) { painterResource(R.drawable.logo_dark) } else { painterResource(R.drawable.logo_white) },
painter = if (isSystemInDarkTheme() && App.getContext().getSharedPreferences("vrcaa_prefs", 0).currentThemeOption == 1) { painterResource(R.drawable.logo_dark) } else { painterResource(R.drawable.logo_white) },
contentDescription = null,
contentScale = ContentScale.FillHeight,
alignment = Alignment.Center
Expand Down Expand Up @@ -155,45 +157,6 @@ class AboutScreen : Screen {
)
)
}
if (BuildConfig.FLAVOR != "gplay") {
item {
ListItem(
headlineContent = { Text(stringResource(R.string.about_page_battery_optimizations_title)) },
modifier = Modifier.clickable(
onClick = {
val manager = getSystemService(context, PowerManager::class.java)
manager?.let { pm ->
if (!pm.isIgnoringBatteryOptimizations(context.packageName)) {
val intent = Intent(
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:${context.packageName}")
)
context.startActivity(intent)
} else {
Toast.makeText(
context,
context.getString(R.string.about_page_battery_optimizations_toast),
Toast.LENGTH_SHORT
).show()
}
}
}
)
)
}
}
if (App.isDeveloperModeEnabled()) {
item {
ListItem(
headlineContent = { Text("Artificial Exception") },
modifier = Modifier.clickable(
onClick = {
throw RuntimeException("d3ad :(")
}
)
)
}
}
}
}
)
Expand Down
Loading

0 comments on commit 7a6af61

Please sign in to comment.