From 92993db5c7281b95df000b9baec6cc2bb197b909 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 1 Jul 2022 10:29:00 +0200 Subject: [PATCH] convert prefs to viewbinding --- .../com/github/libretube/SettingsActivity.kt | 22 +++++++++---------- .../github/libretube/dialogs/LoginDialog.kt | 3 ++- .../libretube/preferences/AboutFragment.kt | 8 +++++-- .../libretube/preferences/AdvancedSettings.kt | 6 ++--- .../preferences/AppearanceSettings.kt | 6 ++--- .../libretube/preferences/InstanceSettings.kt | 6 ++--- .../libretube/preferences/PlayerSettings.kt | 6 ++--- .../preferences/SponsorBlockSettings.kt | 6 ++--- .../res/drawable/ic_launcher_monochrome.xml | 21 +++++++++--------- app/src/main/res/layout/chapter_column.xml | 4 ++-- 10 files changed, 46 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SettingsActivity.kt b/app/src/main/java/com/github/libretube/SettingsActivity.kt index 8e2a3f69b..d091b6d20 100644 --- a/app/src/main/java/com/github/libretube/SettingsActivity.kt +++ b/app/src/main/java/com/github/libretube/SettingsActivity.kt @@ -3,11 +3,9 @@ package com.github.libretube import android.app.NotificationManager import android.os.Build import android.os.Bundle -import android.view.View -import android.widget.ImageButton -import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat +import com.github.libretube.databinding.ActivitySettingsBinding import com.github.libretube.preferences.MainSettings import com.github.libretube.util.ThemeHelper import com.google.android.material.color.DynamicColors @@ -17,24 +15,27 @@ var requireMainActivityRestart = false class SettingsActivity : AppCompatActivity() { val TAG = "SettingsActivity" + lateinit var binding: ActivitySettingsBinding + override fun onCreate(savedInstanceState: Bundle?) { DynamicColors.applyToActivityIfAvailable(this) ThemeHelper.updateTheme(this) + // makes the preference dialogs use material dialogs setTheme(R.style.MaterialAlertDialog) super.onCreate(savedInstanceState) + + binding = ActivitySettingsBinding.inflate(layoutInflater) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { overridePendingTransition(50, 50) } - val view = this.findViewById(android.R.id.content) - view.alpha = 0F - view.animate().alpha(1F).duration = 300 + binding.root.alpha = 0F + binding.root.animate().alpha(1F).duration = 300 - setContentView(R.layout.activity_settings) + setContentView(binding.root) - val backButton = view.findViewById(R.id.back_imageButton) - backButton.setOnClickListener { + binding.backImageButton.setOnClickListener { onBackPressed() } @@ -66,8 +67,7 @@ class SettingsActivity : AppCompatActivity() { .beginTransaction() .replace(R.id.settings, MainSettings()) .commit() - val topBarTextView = findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.settings) + binding.topBarTextView.text = getString(R.string.settings) } } } diff --git a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt index 70a6cd2ea..de9a1733a 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt @@ -29,7 +29,8 @@ class LoginDialog : DialogFragment() { binding.login.setOnClickListener { if (binding.username.text.toString() != "" && binding.password.text.toString() != "") { - val login = Login(binding.username.text.toString(), binding.password.text.toString()) + val login = + Login(binding.username.text.toString(), binding.password.text.toString()) login(login) } else { Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/github/libretube/preferences/AboutFragment.kt b/app/src/main/java/com/github/libretube/preferences/AboutFragment.kt index eb9f25da5..4e6219386 100644 --- a/app/src/main/java/com/github/libretube/preferences/AboutFragment.kt +++ b/app/src/main/java/com/github/libretube/preferences/AboutFragment.kt @@ -17,15 +17,19 @@ import com.github.libretube.DONATE_URL import com.github.libretube.PIPED_GITHUB_URL import com.github.libretube.R import com.github.libretube.WEBSITE_URL +import com.github.libretube.databinding.FragmentAboutBinding import com.google.android.material.dialog.MaterialAlertDialogBuilder class AboutFragment : Fragment() { + private lateinit var binding: FragmentAboutBinding + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.fragment_about, container, false) + ): View { + binding = FragmentAboutBinding.inflate(layoutInflater) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt index 1de88cba5..0f03b9736 100644 --- a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt @@ -1,10 +1,10 @@ package com.github.libretube.preferences import android.os.Bundle -import android.widget.TextView import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R +import com.github.libretube.SettingsActivity import com.github.libretube.requireMainActivityRestart import com.github.libretube.util.PreferenceHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -15,8 +15,8 @@ class AdvancedSettings : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.advanced_settings, rootKey) - val topBarTextView = activity?.findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.advanced) + val settingsActivity = activity as SettingsActivity + settingsActivity.binding.topBarTextView.text = getString(R.string.advanced) val clearHistory = findPreference("clear_history") clearHistory?.setOnPreferenceClickListener { diff --git a/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt index b8a4bd076..380424bd4 100644 --- a/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt @@ -1,12 +1,12 @@ package com.github.libretube.preferences import android.os.Bundle -import android.widget.TextView import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import com.github.libretube.R +import com.github.libretube.SettingsActivity import com.github.libretube.requireMainActivityRestart import com.github.libretube.util.ThemeHelper @@ -15,8 +15,8 @@ class AppearanceSettings : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.appearance_settings, rootKey) - val topBarTextView = activity?.findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.appearance) + val settingsActivity = activity as SettingsActivity + settingsActivity.binding.topBarTextView.text = getString(R.string.appearance) val themeToggle = findPreference("theme_togglee") themeToggle?.setOnPreferenceChangeListener { _, _ -> diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index e5007999d..728beed24 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -8,7 +8,6 @@ import android.os.Build import android.os.Bundle import android.text.TextUtils import android.util.Log -import android.widget.TextView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.core.app.ActivityCompat @@ -19,6 +18,7 @@ import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R +import com.github.libretube.SettingsActivity import com.github.libretube.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.LoginDialog @@ -111,8 +111,8 @@ class InstanceSettings : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.instance_settings, rootKey) - val topBarTextView = activity?.findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.instance) + val settingsActivity = activity as SettingsActivity + settingsActivity.binding.topBarTextView.text = getString(R.string.instance) val instance = findPreference("selectInstance") // fetchInstance() diff --git a/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt b/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt index 84ed41c11..16fd72aa7 100644 --- a/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt @@ -1,9 +1,9 @@ package com.github.libretube.preferences import android.os.Bundle -import android.widget.TextView import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R +import com.github.libretube.SettingsActivity class PlayerSettings : PreferenceFragmentCompat() { val TAG = "PlayerSettings" @@ -11,7 +11,7 @@ class PlayerSettings : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.player_settings, rootKey) - val topBarTextView = activity?.findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.player) + val settingsActivity = activity as SettingsActivity + settingsActivity.binding.topBarTextView.text = getString(R.string.player) } } diff --git a/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt b/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt index 4d8def0b3..02c4479c7 100644 --- a/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt @@ -1,9 +1,9 @@ package com.github.libretube.preferences import android.os.Bundle -import android.widget.TextView import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R +import com.github.libretube.SettingsActivity class SponsorBlockSettings : PreferenceFragmentCompat() { private val TAG = "SponsorBlockSettings" @@ -11,7 +11,7 @@ class SponsorBlockSettings : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.sponsorblock_settings, rootKey) - val topBarTextView = activity?.findViewById(R.id.topBar_textView) - topBarTextView?.text = getString(R.string.sponsorblock) + val settingsActivity = activity as SettingsActivity + settingsActivity.binding.topBarTextView.text = getString(R.string.sponsorblock) } } diff --git a/app/src/main/res/drawable/ic_launcher_monochrome.xml b/app/src/main/res/drawable/ic_launcher_monochrome.xml index 6cc55ad92..3573449c0 100644 --- a/app/src/main/res/drawable/ic_launcher_monochrome.xml +++ b/app/src/main/res/drawable/ic_launcher_monochrome.xml @@ -1,17 +1,16 @@ - - + + diff --git a/app/src/main/res/layout/chapter_column.xml b/app/src/main/res/layout/chapter_column.xml index b44de7e06..595130de6 100644 --- a/app/src/main/res/layout/chapter_column.xml +++ b/app/src/main/res/layout/chapter_column.xml @@ -3,9 +3,9 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="100dp" android:layout_height="wrap_content" - android:paddingHorizontal="5dp" android:background="?attr/selectableItemBackground" - android:orientation="vertical"> + android:orientation="vertical" + android:paddingHorizontal="5dp">