Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Use ThemeViewModel for theme switching
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Jul 16, 2023
1 parent 490b281 commit 3c06efd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/recorder/enums/ThemeMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ enum class ThemeMode {
SYSTEM,
LIGHT,
DARK,
AMOLED;
AMOLED,
;

companion object {
fun getCurrent() = valueOf(Preferences.getString(Preferences.themeModeKey, SYSTEM.name))
Expand Down
34 changes: 9 additions & 25 deletions app/src/main/java/com/bnyro/recorder/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,67 +1,51 @@
package com.bnyro.recorder.ui

import android.content.SharedPreferences
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
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.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.compose.rememberNavController
import com.bnyro.recorder.enums.RecorderType
import com.bnyro.recorder.enums.ThemeMode
import com.bnyro.recorder.ui.models.ThemeModel
import com.bnyro.recorder.ui.theme.RecordYouTheme
import com.bnyro.recorder.util.Preferences

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val themeModel: ThemeModel by viewModels()

val initialRecorder = when (intent?.getStringExtra("action")) {
"audio" -> RecorderType.AUDIO
"screen" -> RecorderType.VIDEO
else -> RecorderType.NONE
}

setContent {
var themeMode by remember { mutableStateOf(ThemeMode.getCurrent()) }
DisposableEffect(this, isSystemInDarkTheme()) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == Preferences.themeModeKey) {
themeMode = ThemeMode.getCurrent()
}
}
with(Preferences.prefs) {
registerOnSharedPreferenceChangeListener(listener)
onDispose {
unregisterOnSharedPreferenceChangeListener(listener)
}
}
}
RecordYouTheme(
when (themeMode) {
when (themeModel.themeMode) {
ThemeMode.SYSTEM -> isSystemInDarkTheme()
ThemeMode.DARK -> true
else -> false
}, amoledDark = themeMode == ThemeMode.AMOLED
},
amoledDark = themeModel.themeMode == ThemeMode.AMOLED,
) {
val navController = rememberNavController()
Surface(
modifier = Modifier
.fillMaxSize(),
color = MaterialTheme.colorScheme.background
color = MaterialTheme.colorScheme.background,
) {
AppNavHost(
navController = navController,
modifier = Modifier,
initialRecorder = initialRecorder
initialRecorder = initialRecorder,
)
}
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/bnyro/recorder/ui/models/ThemeModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bnyro.recorder.ui.models

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.bnyro.recorder.enums.ThemeMode

class ThemeModel : ViewModel() {
var themeMode by mutableStateOf(
ThemeMode.getCurrent(),
)
}
59 changes: 33 additions & 26 deletions app/src/main/java/com/bnyro/recorder/ui/screens/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bnyro.recorder.ui.screens

import android.net.Uri
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -24,10 +25,12 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.AudioChannels
import com.bnyro.recorder.enums.AudioDeviceSource
Expand All @@ -42,35 +45,37 @@ import com.bnyro.recorder.ui.common.CustomNumInputPref
import com.bnyro.recorder.ui.common.SelectionDialog
import com.bnyro.recorder.ui.components.NamingPatternPref
import com.bnyro.recorder.ui.dialogs.AboutDialog
import com.bnyro.recorder.ui.models.ThemeModel
import com.bnyro.recorder.util.PickFolderContract
import com.bnyro.recorder.util.Preferences

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen() {
val themeModel: ThemeModel = viewModel(LocalContext.current as ComponentActivity)
var audioFormat by remember {
mutableStateOf(AudioFormat.getCurrent())
}
var audioChannels by remember {
mutableStateOf(
AudioChannels.fromInt(
Preferences.prefs.getInt(Preferences.audioChannelsKey, AudioChannels.MONO.value)
)
Preferences.prefs.getInt(Preferences.audioChannelsKey, AudioChannels.MONO.value),
),
)
}
var audioDeviceSource by remember {
mutableStateOf(
AudioDeviceSource.fromInt(
Preferences.prefs.getInt(
Preferences.audioDeviceSourceKey,
AudioDeviceSource.DEFAULT.value
)
)
AudioDeviceSource.DEFAULT.value,
),
),
)
}
var screenAudioSource by remember {
mutableStateOf(
AudioSource.fromInt(Preferences.prefs.getInt(Preferences.audioSourceKey, 0))
AudioSource.fromInt(Preferences.prefs.getInt(Preferences.audioSourceKey, 0)),
)
}
var videoEncoder by remember {
Expand All @@ -94,35 +99,36 @@ fun SettingsScreen() {
actions = {
ClickableIcon(
imageVector = Icons.Default.DarkMode,
contentDescription = stringResource(R.string.theme)
contentDescription = stringResource(R.string.theme),
) {
showThemePref = true
}
ClickableIcon(
imageVector = Icons.Default.Info,
contentDescription = stringResource(R.string.about)
contentDescription = stringResource(R.string.about),
) {
showAbout = true
}
})
},
)
}) { paddingValues ->
Column(
modifier = Modifier
.padding(paddingValues)
.padding(horizontal = 16.dp)
.padding(horizontal = 16.dp),
) {
Text(
text = stringResource(R.string.directory),
fontWeight = FontWeight.Bold,
fontSize = 18.sp
fontSize = 18.sp,
)
Spacer(modifier = Modifier.height(5.dp))
Button(
onClick = {
val lastDir = Preferences.prefs.getString(Preferences.targetFolderKey, "")
.takeIf { !it.isNullOrBlank() }
directoryPicker.launch(lastDir?.let { Uri.parse(it) })
}
},
) {
Text(stringResource(R.string.choose_dir))
}
Expand All @@ -131,7 +137,7 @@ fun SettingsScreen() {
title = stringResource(R.string.audio_format),
entries = AudioFormat.formats.map { it.name },
values = AudioFormat.formats.map { it.format },
selections = listOf(audioFormat.format)
selections = listOf(audioFormat.format),
) { index, newValue ->
if (newValue) {
audioFormat = AudioFormat.formats[index]
Expand All @@ -142,13 +148,13 @@ fun SettingsScreen() {
CustomNumInputPref(
key = Preferences.audioSampleRateKey,
title = stringResource(R.string.sample_rate),
defValue = 44_100
defValue = 44_100,
)
Spacer(modifier = Modifier.width(10.dp))
CustomNumInputPref(
key = Preferences.audioBitrateKey,
title = stringResource(R.string.bitrate),
defValue = 192_000
defValue = 192_000,
)
}
val audioDeviceSourceValues = AudioDeviceSource.values().map { it.value }
Expand All @@ -157,16 +163,16 @@ fun SettingsScreen() {
R.string.default_audio,
R.string.microphone,
R.string.camcorder,
R.string.unprocessed
R.string.unprocessed,
).map {
stringResource(it)
},
values = audioDeviceSourceValues,
selections = listOf(audioDeviceSource.value)
selections = listOf(audioDeviceSource.value),
) { index, newValue ->
if (newValue) {
audioDeviceSource = AudioDeviceSource.fromInt(
audioDeviceSourceValues[index]
audioDeviceSourceValues[index],
)
Preferences.edit {
putInt(Preferences.audioDeviceSourceKey, audioDeviceSourceValues[index])
Expand All @@ -179,7 +185,7 @@ fun SettingsScreen() {
stringResource(it)
},
values = audioChannelsValues,
selections = listOf(audioChannels.value)
selections = listOf(audioChannels.value),
) { index, newValue ->
if (newValue) {
audioChannels = AudioChannels.fromInt(audioChannelsValues[index])
Expand All @@ -196,7 +202,7 @@ fun SettingsScreen() {
stringResource(it)
},
values = audioValues,
selections = listOf(screenAudioSource.value)
selections = listOf(screenAudioSource.value),
) { index, newValue ->
if (newValue) {
screenAudioSource = AudioSource.fromInt(audioValues[index])
Expand All @@ -206,7 +212,7 @@ fun SettingsScreen() {
ChipSelector(
entries = VideoFormat.codecs.map { it.name },
values = VideoFormat.codecs.map { it.codec },
selections = listOf(videoEncoder.codec)
selections = listOf(videoEncoder.codec),
) { index, newValue ->
if (newValue) {
videoEncoder = VideoFormat.codecs[index]
Expand All @@ -217,22 +223,22 @@ fun SettingsScreen() {
CustomNumInputPref(
key = Preferences.videoBitrateKey,
title = stringResource(R.string.bitrate),
defValue = 1_200_000
defValue = 1_200_000,
)
Spacer(modifier = Modifier.height(10.dp))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CheckboxPref(
prefKey = Preferences.losslessRecorderKey,
title = stringResource(R.string.lossless_audio),
summary = stringResource(R.string.lossless_audio_desc)
summary = stringResource(R.string.lossless_audio_desc),
)
}
Spacer(modifier = Modifier.height(10.dp))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CheckboxPref(
prefKey = Preferences.showOverlayAnnotationToolKey,
title = stringResource(R.string.screen_recorder_annotation),
summary = stringResource(R.string.screen_recorder_annotation_desc)
summary = stringResource(R.string.screen_recorder_annotation_desc),
)
}
Spacer(modifier = Modifier.height(10.dp))
Expand All @@ -248,11 +254,12 @@ fun SettingsScreen() {
R.string.system,
R.string.light,
R.string.dark,
R.string.amoled_dark
R.string.amoled_dark,
).map {
stringResource(it)
}
},
) {
themeModel.themeMode = ThemeMode.values()[it]
Preferences.edit { putString(Preferences.themeModeKey, ThemeMode.values()[it].name) }
}
}
Expand Down

0 comments on commit 3c06efd

Please sign in to comment.