[Android] Onboarding: add "auto rescan" option

Geoffrey Métais git at videolan.org
Tue Mar 17 15:11:01 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar 17 09:56:25 2020 +0100| [1fe13df4b089c2e70a54e585600e3c79ef71b329] | committer: Geoffrey Métais

Onboarding: add "auto rescan" option

> https://code.videolan.org/videolan/vlc-android/commit/1fe13df4b089c2e70a54e585600e3c79ef71b329
---

 .../vlc-android/res/layout/onboarding_scanning.xml | 18 ++++++++--
 .../onboarding/OnboardingFragmentPagerAdapter.kt   | 33 +++++--------------
 .../gui/onboarding/OnboardingScanningFragment.kt   | 38 +++++++---------------
 .../vlc/gui/onboarding/OnboardingThemeFragment.kt  | 14 +++-----
 4 files changed, 39 insertions(+), 64 deletions(-)

diff --git a/application/vlc-android/res/layout/onboarding_scanning.xml b/application/vlc-android/res/layout/onboarding_scanning.xml
index da1a3f910..58e6a409c 100644
--- a/application/vlc-android/res/layout/onboarding_scanning.xml
+++ b/application/vlc-android/res/layout/onboarding_scanning.xml
@@ -56,21 +56,33 @@
         android:checked="true"
         android:text="@string/onboarding_scanning_enable"
         android:textSize="18sp"
-        app:layout_constraintBottom_toTopOf="@+id/scanningFolderCheckbox"
+        app:layout_constraintBottom_toTopOf="@+id/autoScanningCheckbox"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/textView4"
         app:layout_constraintVertical_chainStyle="packed" />
 
     <CheckBox
-        android:id="@+id/scanningFolderCheckbox"
+        android:id="@+id/autoScanningCheckbox"
+        android:checked="true"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:layout_marginBottom="16dp"
         android:layout_marginTop="8dp"
+        android:text="@string/auto_rescan_summary"
+        app:layout_constraintTop_toBottomOf="@+id/scanningEnableSwitch"
+        app:layout_constraintBottom_toTopOf="@+id/scanningFolderCheckbox"
+        tools:layout_editor_absoluteX="8dp" />
+
+    <CheckBox
+        android:id="@+id/scanningFolderCheckbox"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
         android:layout_marginBottom="16dp"
         android:text="@string/onboarding_scan_customize"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/scanningEnableSwitch"
+        app:layout_constraintTop_toBottomOf="@+id/autoScanningCheckbox"
         tools:layout_editor_absoluteX="8dp" />
 
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingFragmentPagerAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingFragmentPagerAdapter.kt
index e1dcb8b83..0c0c077f3 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingFragmentPagerAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingFragmentPagerAdapter.kt
@@ -7,46 +7,29 @@ import androidx.viewpager.widget.PagerAdapter
 
 class OnboardingFragmentPagerAdapter(private val fragmentManager: FragmentManager, private var count: Int) : FragmentStatePagerAdapter(fragmentManager) {
 
-
     private var fragments = ArrayList<Fragment>()
     private var folderFragment: OnboardingFoldersFragment = OnboardingFoldersFragment.newInstance()
 
     init {
         fragments.add(OnboardingWelcomeFragment.newInstance())
-        val onboardingScanningFragment = OnboardingScanningFragment.newInstance()
-        fragments.add(onboardingScanningFragment)
+        fragments.add(OnboardingScanningFragment.newInstance())
         fragments.add(OnboardingThemeFragment.newInstance())
-
-
     }
 
     fun onCustomizedChanged(customizeEnabled: Boolean) {
-        if (customizeEnabled) {
+        count = if (customizeEnabled) {
             fragments.add(2, folderFragment)
-            count = 4
+            4
         } else {
             fragments.remove(folderFragment)
-            count = 3
+            3
         }
-
         notifyDataSetChanged()
-
-
     }
 
+    override fun getItemPosition(obj: Any)= PagerAdapter.POSITION_NONE
 
-    override fun getItemPosition(obj: Any): Int {
-        return PagerAdapter.POSITION_NONE
-    }
-
-
-    override fun getItem(position: Int): Fragment {
-        return fragments[position]
-    }
-
-    override fun getCount(): Int {
-        return count
-    }
-
+    override fun getItem(position: Int) = fragments[position]
 
-}
\ No newline at end of file
+    override fun getCount() = count
+}
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingScanningFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingScanningFragment.kt
index c776343f1..a92452c56 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingScanningFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingScanningFragment.kt
@@ -4,50 +4,39 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import android.widget.CheckBox
-import android.widget.Switch
 import androidx.fragment.app.Fragment
-import org.videolan.vlc.R
+import androidx.fragment.app.activityViewModels
+import kotlinx.android.synthetic.main.onboarding_scanning.*
 import org.videolan.tools.KEY_MEDIALIBRARY_SCAN
 import org.videolan.tools.ML_SCAN_OFF
 import org.videolan.tools.ML_SCAN_ON
 import org.videolan.tools.Settings
+import org.videolan.vlc.R
 
 class OnboardingScanningFragment : Fragment() {
-    private lateinit var scanningFolderCheckbox: CheckBox
-    private lateinit var scanningEnableSwitch: Switch
     lateinit var onScanningCustomizeChangedListener: IOnScanningCustomizeChangedListener
-    private lateinit var viewModel: OnboardingViewModel
+    private val viewModel: OnboardingViewModel by activityViewModels()
+    private val preferences by lazy { Settings.getInstance(requireActivity()) }
 
-
-    override fun onCreate(savedInstanceState: Bundle?) {
-        super.onCreate(savedInstanceState)
-        viewModel = getOnboardingModel()
-    }
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.onboarding_scanning, container, false)
     }
 
-    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
-        scanningFolderCheckbox = view.findViewById(R.id.scanningFolderCheckbox)
-        scanningEnableSwitch = view.findViewById(R.id.scanningEnableSwitch)
-        super.onViewCreated(view, savedInstanceState)
-
-    }
-
-
     override fun onActivityCreated(savedInstanceState: Bundle?) {
         super.onActivityCreated(savedInstanceState)
 
-
         scanningEnableSwitch.setOnCheckedChangeListener { _, isChecked ->
             scanningFolderCheckbox.visibility = if (isChecked) View.VISIBLE else View.GONE
-            val prefs = Settings.getInstance(requireActivity())
-            prefs.edit().putInt(KEY_MEDIALIBRARY_SCAN, if (isChecked) ML_SCAN_ON else ML_SCAN_OFF).apply()
+            autoScanningCheckbox.isChecked = isChecked
+            preferences.edit().putInt(KEY_MEDIALIBRARY_SCAN, if (isChecked) ML_SCAN_ON else ML_SCAN_OFF).apply()
             viewModel.scanStorages = isChecked
             scanningFolderCheckbox.isChecked = false
         }
 
+        autoScanningCheckbox.setOnCheckedChangeListener { _, isChecked ->
+            preferences.edit().putBoolean("auto_rescan", isChecked).apply()
+        }
+
         onScanningCustomizeChangedListener = requireActivity() as IOnScanningCustomizeChangedListener
 
         scanningFolderCheckbox.isChecked = viewModel.customizeMediaFolders
@@ -59,12 +48,9 @@ class OnboardingScanningFragment : Fragment() {
             if (::onScanningCustomizeChangedListener.isInitialized) {
                 onScanningCustomizeChangedListener.onCustomizedChanged(isChecked)
             }
-
-
         }
     }
 
-
     companion object {
         fun newInstance(): OnboardingScanningFragment {
             return OnboardingScanningFragment()
@@ -74,4 +60,4 @@ class OnboardingScanningFragment : Fragment() {
 
 interface IOnScanningCustomizeChangedListener {
     fun onCustomizedChanged(customizeEnabled: Boolean)
-}
\ No newline at end of file
+}
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
index d4d4dc9b0..17c03598c 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/onboarding/OnboardingThemeFragment.kt
@@ -1,26 +1,20 @@
 package org.videolan.vlc.gui.onboarding
 
-import android.os.Build
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import android.widget.CompoundButton
 import androidx.appcompat.app.AppCompatDelegate
 import androidx.core.content.ContextCompat
 import androidx.fragment.app.Fragment
+import androidx.fragment.app.activityViewModels
 import kotlinx.android.synthetic.main.onboarding_theme.*
-import org.videolan.vlc.R
 import org.videolan.resources.AndroidDevices
+import org.videolan.vlc.R
 
 class OnboardingThemeFragment : Fragment(), View.OnClickListener {
 
-    private lateinit var viewModel: OnboardingViewModel
-
-    override fun onCreate(savedInstanceState: Bundle?) {
-        super.onCreate(savedInstanceState)
-        viewModel = getOnboardingModel()
-    }
+    private val viewModel: OnboardingViewModel by activityViewModels()
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.onboarding_theme, container, false)
@@ -62,4 +56,4 @@ class OnboardingThemeFragment : Fragment(), View.OnClickListener {
     companion object {
         fun newInstance() = OnboardingThemeFragment()
     }
-}
\ No newline at end of file
+}



More information about the Android mailing list