mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-21 14:03:11 -07:00
Show Registered Hosts in Settings on Android
This commit is contained in:
parent
80bfc893af
commit
b6821609b3
8 changed files with 191 additions and 10 deletions
|
@ -65,6 +65,9 @@ interface RegisteredHostDao
|
|||
@Query("DELETE FROM registered_host WHERE ps4_mac == :mac")
|
||||
fun deleteByMac(mac: MacAddress): Completable
|
||||
|
||||
@Query("SELECT COUNT(*) FROM registered_host")
|
||||
fun count(): Flowable<Int>
|
||||
|
||||
@Insert
|
||||
fun insert(host: RegisteredHost): Single<Long>
|
||||
}
|
|
@ -17,12 +17,24 @@
|
|||
|
||||
package com.metallic.chiaki.settings
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.os.Bundle
|
||||
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
|
||||
|
||||
class SettingsActivity: AppCompatActivity()
|
||||
interface TitleFragment
|
||||
{
|
||||
fun getTitle(resources: Resources): String
|
||||
}
|
||||
|
||||
class SettingsActivity: AppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
|
||||
{
|
||||
override fun onCreate(savedInstanceState: Bundle?)
|
||||
{
|
||||
|
@ -30,5 +42,34 @@ class SettingsActivity: AppCompatActivity()
|
|||
setContentView(R.layout.activity_settings)
|
||||
title = ""
|
||||
setSupportActionBar(toolbar)
|
||||
|
||||
val rootFragment = SettingsFragment()
|
||||
replaceFragment(rootFragment, false)
|
||||
supportFragmentManager.addOnBackStackChangedListener {
|
||||
val titleFragment = supportFragmentManager.findFragmentById(R.id.settingsFragment) as? TitleFragment ?: return@addOnBackStackChangedListener
|
||||
titleTextView.text = titleFragment.getTitle(resources)
|
||||
}
|
||||
titleTextView.text = rootFragment.getTitle(resources)
|
||||
}
|
||||
|
||||
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference) = when(pref.fragment)
|
||||
{
|
||||
SettingsRegisteredHostsFragment::class.java.canonicalName -> {
|
||||
replaceFragment(SettingsRegisteredHostsFragment(), true)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
|
||||
private fun replaceFragment(fragment: Fragment, addToBackStack: Boolean)
|
||||
{
|
||||
supportFragmentManager.beginTransaction()
|
||||
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
|
||||
.replace(R.id.settingsFragment, fragment)
|
||||
.also {
|
||||
if(addToBackStack)
|
||||
it.addToBackStack(null)
|
||||
}
|
||||
.commit()
|
||||
}
|
||||
}
|
|
@ -17,14 +17,33 @@
|
|||
|
||||
package com.metallic.chiaki.settings
|
||||
|
||||
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 com.metallic.chiaki.R
|
||||
import com.metallic.chiaki.common.AppDatabase
|
||||
import com.metallic.chiaki.common.ext.viewModelFactory
|
||||
import com.metallic.chiaki.common.getDatabase
|
||||
|
||||
class SettingsFragment: PreferenceFragmentCompat()
|
||||
class SettingsFragment: PreferenceFragmentCompat(), TitleFragment
|
||||
{
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
|
||||
{
|
||||
setPreferencesFromResource(R.xml.preferences, rootKey)
|
||||
|
||||
val registeredHostsPreference = preferenceScreen.findPreference<Preference>("registered_hosts")
|
||||
|
||||
val viewModel = ViewModelProviders
|
||||
.of(this, viewModelFactory { SettingsViewModel(getDatabase(context!!)) })
|
||||
.get(SettingsViewModel::class.java)
|
||||
|
||||
viewModel.registeredHostsCount.observe(this, Observer {
|
||||
registeredHostsPreference?.summary = getString(R.string.preferences_registered_hosts_summary, it)
|
||||
})
|
||||
}
|
||||
|
||||
override fun getTitle(resources: Resources): String = resources.getString(R.string.title_settings)
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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.settings
|
||||
|
||||
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 com.metallic.chiaki.R
|
||||
import com.metallic.chiaki.common.ext.viewModelFactory
|
||||
import com.metallic.chiaki.common.getDatabase
|
||||
|
||||
class SettingsRegisteredHostsFragment: PreferenceFragmentCompat(), TitleFragment
|
||||
{
|
||||
private lateinit var viewModel: SettingsRegisteredHostsViewModel
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
|
||||
{
|
||||
val context = preferenceManager.context
|
||||
preferenceScreen = preferenceManager.createPreferenceScreen(context)
|
||||
|
||||
viewModel = ViewModelProviders
|
||||
.of(this, viewModelFactory { SettingsRegisteredHostsViewModel(getDatabase(context!!)) })
|
||||
.get(SettingsRegisteredHostsViewModel::class.java)
|
||||
|
||||
viewModel.registeredHosts.observe(this, Observer {
|
||||
preferenceScreen.removeAll()
|
||||
it.forEach { host ->
|
||||
val pref = Preference(context)
|
||||
pref.title = host.ps4Nickname
|
||||
pref.summary = host.ps4Mac.toString()
|
||||
preferenceScreen.addPreference(pref)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
override fun getTitle(resources: Resources): String = resources.getString(R.string.preferences_registered_hosts_title)
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.settings
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.metallic.chiaki.common.AppDatabase
|
||||
import com.metallic.chiaki.common.ext.toLiveData
|
||||
|
||||
class SettingsRegisteredHostsViewModel(val database: AppDatabase): ViewModel()
|
||||
{
|
||||
val registeredHosts by lazy {
|
||||
database.registeredHostDao().getAll().toLiveData()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.settings
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.metallic.chiaki.common.AppDatabase
|
||||
import com.metallic.chiaki.common.ext.toLiveData
|
||||
|
||||
class SettingsViewModel(val database: AppDatabase): ViewModel()
|
||||
{
|
||||
val registeredHostsCount by lazy {
|
||||
database.registeredHostDao().count().toLiveData()
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@
|
|||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:theme="@style/AppTheme.Toolbar">
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/titleTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
|
@ -21,11 +22,10 @@
|
|||
android:textSize="20dp"/>
|
||||
</com.google.android.material.appbar.MaterialToolbar>
|
||||
|
||||
<fragment
|
||||
android:id="@+id/settingsFragment"
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:name="com.metallic.chiaki.settings.SettingsFragment"
|
||||
android:id="@+id/settingsFragment"
|
||||
app:layout_constraintTop_toBottomOf="@id/toolbar"
|
||||
app:layout_constraintBottom_toBottomOf="parent" />
|
||||
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<PreferenceCategory
|
||||
app:key="category_general"
|
||||
app:title="@string/preferences_category_title_general">
|
||||
<Preference
|
||||
app:key="registered_hosts"
|
||||
app:summary="@string/preferences_registered_hosts_summary"
|
||||
app:title="@string/preferences_registered_hosts_title"/>
|
||||
tools:summary="@string/preferences_registered_hosts_summary"
|
||||
app:summary=" "
|
||||
app:title="@string/preferences_registered_hosts_title"
|
||||
app:fragment="com.metallic.chiaki.settings.SettingsRegisteredHostsFragment"/>
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue