From b6821609b3a54d8eafbc18551005d0062c7809d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Wed, 16 Oct 2019 19:21:29 +0200 Subject: [PATCH] Show Registered Hosts in Settings on Android --- .../metallic/chiaki/common/RegisteredHost.kt | 3 + .../chiaki/settings/SettingsActivity.kt | 43 +++++++++++++- .../chiaki/settings/SettingsFragment.kt | 21 ++++++- .../SettingsRegisteredHostsFragment.kt | 56 +++++++++++++++++++ .../SettingsRegisteredHostsViewModel.kt | 29 ++++++++++ .../chiaki/settings/SettingsViewModel.kt | 29 ++++++++++ .../src/main/res/layout/activity_settings.xml | 10 ++-- android/app/src/main/res/xml/preferences.xml | 10 +++- 8 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt create mode 100644 android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt create mode 100644 android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt diff --git a/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt index 0acd10f..e15dd07 100644 --- a/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt +++ b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt @@ -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 + @Insert fun insert(host: RegisteredHost): Single } \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt index 17df044..1d0a466 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt @@ -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() } } \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt index 9dcee63..8adb263 100644 --- a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt @@ -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("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) } \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt new file mode 100644 index 0000000..955b359 --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt @@ -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 . + */ + +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) +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt new file mode 100644 index 0000000..489a31e --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt @@ -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 . + */ + +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() + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt new file mode 100644 index 0000000..f3578ee --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt @@ -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 . + */ + +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() + } +} \ No newline at end of file diff --git a/android/app/src/main/res/layout/activity_settings.xml b/android/app/src/main/res/layout/activity_settings.xml index 9522232..fffe0ce 100644 --- a/android/app/src/main/res/layout/activity_settings.xml +++ b/android/app/src/main/res/layout/activity_settings.xml @@ -12,21 +12,21 @@ app:layout_constraintTop_toTopOf="parent" android:theme="@style/AppTheme.Toolbar"> + android:textSize="20dp"/> - + app:layout_constraintBottom_toBottomOf="parent" /> \ No newline at end of file diff --git a/android/app/src/main/res/xml/preferences.xml b/android/app/src/main/res/xml/preferences.xml index 9929ea5..172fc4f 100644 --- a/android/app/src/main/res/xml/preferences.xml +++ b/android/app/src/main/res/xml/preferences.xml @@ -1,12 +1,16 @@ - + + tools:summary="@string/preferences_registered_hosts_summary" + app:summary=" " + app:title="@string/preferences_registered_hosts_title" + app:fragment="com.metallic.chiaki.settings.SettingsRegisteredHostsFragment"/>