Show Registered Hosts in Settings on Android

This commit is contained in:
Florian Märkl 2019-10-16 19:21:29 +02:00
commit b6821609b3
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
8 changed files with 191 additions and 10 deletions

View file

@ -65,6 +65,9 @@ interface RegisteredHostDao
@Query("DELETE FROM registered_host WHERE ps4_mac == :mac") @Query("DELETE FROM registered_host WHERE ps4_mac == :mac")
fun deleteByMac(mac: MacAddress): Completable fun deleteByMac(mac: MacAddress): Completable
@Query("SELECT COUNT(*) FROM registered_host")
fun count(): Flowable<Int>
@Insert @Insert
fun insert(host: RegisteredHost): Single<Long> fun insert(host: RegisteredHost): Single<Long>
} }

View file

@ -17,12 +17,24 @@
package com.metallic.chiaki.settings package com.metallic.chiaki.settings
import android.content.res.Resources
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity 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 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.*
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?) override fun onCreate(savedInstanceState: Bundle?)
{ {
@ -30,5 +42,34 @@ class SettingsActivity: AppCompatActivity()
setContentView(R.layout.activity_settings) setContentView(R.layout.activity_settings)
title = "" title = ""
setSupportActionBar(toolbar) 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()
} }
} }

View file

@ -17,14 +17,33 @@
package com.metallic.chiaki.settings package com.metallic.chiaki.settings
import android.content.res.Resources
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.metallic.chiaki.R 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?) override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
{ {
setPreferencesFromResource(R.xml.preferences, rootKey) 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)
} }

View file

@ -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)
}

View file

@ -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()
}
}

View file

@ -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()
}
}

View file

@ -12,21 +12,21 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
android:theme="@style/AppTheme.Toolbar"> android:theme="@style/AppTheme.Toolbar">
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:fontFamily="sans-serif-condensed" android:fontFamily="sans-serif-condensed"
android:text="@string/title_settings" android:text="@string/title_settings"
android:textColor="?attr/colorOnPrimary" android:textColor="?attr/colorOnPrimary"
android:textSize="20dp" /> android:textSize="20dp"/>
</com.google.android.material.appbar.MaterialToolbar> </com.google.android.material.appbar.MaterialToolbar>
<fragment <FrameLayout
android:id="@+id/settingsFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:name="com.metallic.chiaki.settings.SettingsFragment" android:id="@+id/settingsFragment"
app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,12 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?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 <PreferenceCategory
app:key="category_general" app:key="category_general"
app:title="@string/preferences_category_title_general"> app:title="@string/preferences_category_title_general">
<Preference <Preference
app:key="registered_hosts" app:key="registered_hosts"
app:summary="@string/preferences_registered_hosts_summary" tools:summary="@string/preferences_registered_hosts_summary"
app:title="@string/preferences_registered_hosts_title"/> app:summary=" "
app:title="@string/preferences_registered_hosts_title"
app:fragment="com.metallic.chiaki.settings.SettingsRegisteredHostsFragment"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory