mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-14 18:57:07 -07:00
Refactor Android Settings
This commit is contained in:
parent
fdd992ee29
commit
4137a46af9
8 changed files with 173 additions and 46 deletions
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* This file is part of Chiaki.
|
||||
*
|
||||
* Chiaki is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Chiaki is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.metallic.chiaki.common
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.metallic.chiaki.R
|
||||
import com.metallic.chiaki.lib.VideoFPSPreset
|
||||
import com.metallic.chiaki.lib.VideoResolutionPreset
|
||||
|
||||
class Preferences(context: Context)
|
||||
{
|
||||
enum class Resolution(val value: String, @StringRes val title: Int, val preset: VideoResolutionPreset)
|
||||
{
|
||||
RES_360P("360p", R.string.preferences_resolution_title_360p, VideoResolutionPreset.RES_360P),
|
||||
RES_540P("540p", R.string.preferences_resolution_title_540p, VideoResolutionPreset.RES_540P),
|
||||
RES_720P("720p", R.string.preferences_resolution_title_720p, VideoResolutionPreset.RES_720P),
|
||||
RES_1080P("1080p", R.string.preferences_resolution_title_1080p, VideoResolutionPreset.RES_1080P),
|
||||
}
|
||||
|
||||
enum class FPS(val value: String, @StringRes val title: Int, val preset: VideoFPSPreset)
|
||||
{
|
||||
FPS_30("30", R.string.preferences_fps_title_30, VideoFPSPreset.FPS_30),
|
||||
FPS_60("60", R.string.preferences_fps_title_60, VideoFPSPreset.FPS_60)
|
||||
}
|
||||
|
||||
companion object
|
||||
{
|
||||
val resolutionDefault = Resolution.RES_720P
|
||||
val resolutionAll = Resolution.values()
|
||||
val fpsDefault = FPS.FPS_60
|
||||
val fpsAll = FPS.values()
|
||||
}
|
||||
|
||||
private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val resources = context.resources
|
||||
|
||||
val logVerboseKey get() = resources.getString(R.string.preferences_log_verbose_key)
|
||||
var logVerbose
|
||||
get() = sharedPreferences.getBoolean(logVerboseKey, false)
|
||||
set(value) { sharedPreferences.edit().putBoolean(logVerboseKey, value).apply() }
|
||||
|
||||
val resolutionKey get() = resources.getString(R.string.preferences_resolution_key)
|
||||
var resolution
|
||||
get() = sharedPreferences.getString(resolutionKey, resolutionDefault.value)?.let { value ->
|
||||
Resolution.values().firstOrNull { it.value == value }
|
||||
} ?: resolutionDefault
|
||||
set(value) { sharedPreferences.edit().putString(resolutionKey, value.value).apply() }
|
||||
|
||||
val fpsKey get() = resources.getString(R.string.preferences_fps_key)
|
||||
var fps
|
||||
get() =sharedPreferences.getString(fpsKey, fpsDefault.value)?.let { value ->
|
||||
FPS.values().firstOrNull { it.value == value }
|
||||
} ?: fpsDefault
|
||||
set(value) { sharedPreferences.edit().putString(fpsKey, value.value).apply() }
|
||||
}
|
|
@ -8,6 +8,20 @@ import java.lang.Exception
|
|||
import java.net.InetSocketAddress
|
||||
import kotlin.math.abs
|
||||
|
||||
enum class VideoResolutionPreset(val value: Int)
|
||||
{
|
||||
RES_360P(1),
|
||||
RES_540P(2),
|
||||
RES_720P(3),
|
||||
RES_1080P(4)
|
||||
}
|
||||
|
||||
enum class VideoFPSPreset(val value: Int)
|
||||
{
|
||||
FPS_30(30),
|
||||
FPS_60(60)
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
data class ConnectVideoProfile(
|
||||
val width: Int,
|
||||
|
|
|
@ -23,11 +23,8 @@ import androidx.appcompat.app.AppCompatActivity
|
|||
import androidx.fragment.app.Fragment
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.transition.TransitionManager
|
||||
import com.metallic.chiaki.R
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.activity_settings.*
|
||||
import kotlinx.android.synthetic.main.activity_settings.toolbar
|
||||
|
||||
interface TitleFragment
|
||||
{
|
||||
|
|
|
@ -21,21 +21,79 @@ import android.content.res.Resources
|
|||
import android.os.Bundle
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.*
|
||||
import com.metallic.chiaki.R
|
||||
import com.metallic.chiaki.common.AppDatabase
|
||||
import com.metallic.chiaki.common.Preferences
|
||||
import com.metallic.chiaki.common.ext.viewModelFactory
|
||||
import com.metallic.chiaki.common.getDatabase
|
||||
|
||||
class DataStore(val preferences: Preferences): PreferenceDataStore()
|
||||
{
|
||||
override fun getBoolean(key: String?, defValue: Boolean) = when(key)
|
||||
{
|
||||
preferences.logVerboseKey -> preferences.logVerbose
|
||||
else -> defValue
|
||||
}
|
||||
|
||||
override fun putBoolean(key: String?, value: Boolean)
|
||||
{
|
||||
when(key)
|
||||
{
|
||||
preferences.logVerboseKey -> preferences.logVerbose = value
|
||||
}
|
||||
}
|
||||
|
||||
override fun getString(key: String, defValue: String?) = when(key)
|
||||
{
|
||||
preferences.resolutionKey -> preferences.resolution.value
|
||||
preferences.fpsKey -> preferences.fps.value
|
||||
else -> defValue
|
||||
}
|
||||
|
||||
override fun putString(key: String, value: String?)
|
||||
{
|
||||
when(key)
|
||||
{
|
||||
preferences.resolutionKey ->
|
||||
{
|
||||
val resolution = Preferences.Resolution.values().firstOrNull { it.value == value } ?: return
|
||||
preferences.resolution = resolution
|
||||
}
|
||||
preferences.fpsKey ->
|
||||
{
|
||||
val fps = Preferences.FPS.values().firstOrNull { it.value == value } ?: return
|
||||
preferences.fps = fps
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsFragment: PreferenceFragmentCompat(), TitleFragment
|
||||
{
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
|
||||
{
|
||||
val context = context ?: return
|
||||
|
||||
val preferences = Preferences(context)
|
||||
preferenceManager.preferenceDataStore = DataStore(preferences)
|
||||
setPreferencesFromResource(R.xml.preferences, rootKey)
|
||||
|
||||
|
||||
val registeredHostsPreference = preferenceScreen.findPreference<Preference>("registered_hosts")
|
||||
|
||||
preferenceScreen.findPreference<ListPreference>(getString(R.string.preferences_resolution_key))?.let {
|
||||
it.entryValues = Preferences.resolutionAll.map { res -> res.value }.toTypedArray()
|
||||
it.entries = Preferences.resolutionAll.map { res -> getString(res.title) }.toTypedArray()
|
||||
}
|
||||
|
||||
preferenceScreen.findPreference<ListPreference>(getString(R.string.preferences_fps_key))?.let {
|
||||
it.entryValues = Preferences.fpsAll.map { fps -> fps.value }.toTypedArray()
|
||||
it.entries = Preferences.fpsAll.map { fps -> getString(fps.title) }.toTypedArray()
|
||||
}
|
||||
|
||||
val bitratePreference = preferenceScreen.findPreference<EditTextPreference>("bitrate")
|
||||
bitratePreference?.summaryProvider = Preference.SummaryProvider<EditTextPreference> { it.text }
|
||||
|
||||
val viewModel = ViewModelProviders
|
||||
.of(this, viewModelFactory { SettingsViewModel(getDatabase(context!!)) })
|
||||
.get(SettingsViewModel::class.java)
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="preferences_resolution_ids">
|
||||
<item>360p</item>
|
||||
<item>540p</item>
|
||||
<item>720p</item>
|
||||
<item>1080p</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="preferences_resolution_names">
|
||||
<item>360p</item>
|
||||
<item>540p</item>
|
||||
<item>720p</item>
|
||||
<item>1080p</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="preferences_fps_ids">
|
||||
<item>30</item>
|
||||
<item>60</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="preferences_fps_names">
|
||||
<item>30</item>
|
||||
<item>60</item>
|
||||
</string-array>
|
||||
</resources>
|
|
@ -48,4 +48,16 @@
|
|||
<string name="preferences_bitrate_title">Bitrate</string>
|
||||
<string name="preferences_log_verbose_title">Verbose Logging</string>
|
||||
<string name="preferences_log_verbose_summary">Warning: This logs a LOT! Don\'t enable for regular use.</string>
|
||||
|
||||
<!-- Don't localize these -->
|
||||
<string name="preferences_log_verbose_key">log_verbose</string>
|
||||
<string name="preferences_resolution_key">stream_resolution</string>
|
||||
<string name="preferences_resolution_title_360p">360p</string>
|
||||
<string name="preferences_resolution_title_540p">540p</string>
|
||||
<string name="preferences_resolution_title_720p">720p</string>
|
||||
<string name="preferences_resolution_title_1080p">1080p</string>
|
||||
<string name="preferences_fps_key">stream_fps</string>
|
||||
<string name="preferences_fps_title_30">30</string>
|
||||
<string name="preferences_fps_title_60">60</string>
|
||||
<string name="preferences_bitrate_key">stream_bitrate</string>
|
||||
</resources>
|
||||
|
|
|
@ -22,19 +22,17 @@
|
|||
app:key="category_stream"
|
||||
app:title="@string/preferences_category_title_stream">
|
||||
<ListPreference
|
||||
app:key="resolution"
|
||||
app:key="@string/preferences_resolution_key"
|
||||
app:title="@string/preferences_resolution_title"
|
||||
app:entries="@array/preferences_resolution_names"
|
||||
app:entryValues="@array/preferences_resolution_ids" />
|
||||
app:summary="%s" />
|
||||
|
||||
<ListPreference
|
||||
app:key="fps"
|
||||
app:key="@string/preferences_fps_key"
|
||||
app:title="@string/preferences_fps_title"
|
||||
app:entries="@array/preferences_fps_names"
|
||||
app:entryValues="@array/preferences_fps_ids" />
|
||||
app:summary="%s"/>
|
||||
|
||||
<EditTextPreference
|
||||
app:key="bitrate"
|
||||
app:title="@string/preferences_bitrate_title" />
|
||||
app:key="@string/preferences_bitrate_key"
|
||||
app:title="@string/preferences_bitrate_title"/>
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
Loading…
Add table
Add a link
Reference in a new issue