storage: demo: Switch to toolbar APIs for working with menu

Activity methods have been deprecated

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta 2024-09-19 13:25:18 +05:30
parent f5c2f0bd3a
commit a5fe52a01f
9 changed files with 98 additions and 58 deletions

View file

@ -11,8 +11,6 @@ import android.os.Bundle
import android.os.Environment
import android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View.INVISIBLE
@ -22,6 +20,7 @@ import android.widget.Button
import android.widget.ProgressBar
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView
@ -48,7 +47,6 @@ open class LogFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
setHasOptionsMenu(true)
val v = inflater.inflate(R.layout.fragment_log, container, false)
list = v.findViewById(R.id.listView)
list.adapter = adapter
@ -75,16 +73,16 @@ open class LogFragment : Fragment() {
return v
}
override fun onStart() {
super.onStart()
requireActivity().setTitle(R.string.app_name)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.app_name)
setOnMenuItemClickListener(::onMenuItemSelected)
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.fragment_main, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.settings -> {
if (!checkPermission()) return false
@ -108,7 +106,7 @@ open class LogFragment : Fragment() {
startActivity(shareIntent)
true
}
else -> super.onOptionsItemSelected(item)
else -> false
}
}

View file

@ -31,7 +31,7 @@ class DemoSnapshotFragment : SnapshotFragment() {
bottomStub.layoutResource = R.layout.footer_snapshot
val footer = bottomStub.inflate()
footer.findViewById<Button>(R.id.button).setOnClickListener {
requireActivity().onBackPressed()
requireActivity().onBackPressedDispatcher.onBackPressed()
}
return v
}

View file

@ -10,8 +10,6 @@ import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View.FOCUS_DOWN
@ -21,6 +19,7 @@ import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.UiThread
import androidx.appcompat.widget.Toolbar
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
@ -53,8 +52,6 @@ open class MediaScanFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
setHasOptionsMenu(true)
requireActivity().title = arguments?.getString("name")
val v = inflater.inflate(R.layout.fragment_scan, container, false)
scrollView = v.findViewById(R.id.scrollView)
logView = v.findViewById(R.id.logView)
@ -63,11 +60,19 @@ open class MediaScanFragment : Fragment() {
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.fragment_scan, menu)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = arguments?.getString("name")
setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.refresh -> {
loadText()
@ -85,7 +90,7 @@ open class MediaScanFragment : Fragment() {
startActivity(shareIntent)
true
}
else -> super.onOptionsItemSelected(item)
else -> false
}
}

View file

@ -9,14 +9,12 @@ import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.activityViewModels
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.grobox.storagebackuptester.MainViewModel
@ -44,18 +42,21 @@ class SettingsFragment : BackupContentFragment() {
onBackupUriReceived(uri)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
setHasOptionsMenu(true)
requireActivity().title = "Settings"
return super.onCreateView(inflater, container, savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = "Settings"
inflateMenu(R.menu.fragment_settings)
onCreateMenu(menu)
setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.fragment_settings, menu)
private fun onCreateMenu(menu: Menu) {
backupLocationItem = menu.findItem(R.id.backup_location)
backupLocationItem.isChecked = viewModel.hasBackupLocation()
jobItem = menu.findItem(R.id.backup_job)
@ -65,7 +66,7 @@ class SettingsFragment : BackupContentFragment() {
restoreItem.isEnabled = backupLocationItem.isChecked
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.info -> {
parentFragmentManager.beginTransaction()
@ -102,7 +103,7 @@ class SettingsFragment : BackupContentFragment() {
Toast.makeText(requireContext(), "Cache cleared", LENGTH_SHORT).show()
true
}
else -> super.onOptionsItemSelected(item)
else -> false
}
}

View file

@ -9,6 +9,17 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/fragment_main" />
<ProgressBar
android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal"
@ -16,7 +27,7 @@
android:layout_height="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:max="10"
tools:progress="7" />

View file

@ -9,13 +9,25 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/fragment_scan"
app:navigationIcon="?attr/homeAsUpIndicator" />
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:fadeScrollbars="false"
android:scrollbars="vertical"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toBottomOf="@id/toolbar">
<TextView
android:id="@+id/logView"

View file

@ -4,7 +4,7 @@
-->
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/matrix</item>
<item name="colorPrimaryVariant">@color/purple_700</item>

View file

@ -4,7 +4,7 @@
-->
<resources>
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/matrix</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>

View file

@ -3,9 +3,22 @@
SPDX-FileCopyrightText: 2021 The Calyx Institute
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:navigationIcon="?attr/homeAsUpIndicator" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -26,5 +39,5 @@
android:contentDescription="@string/content_add"
android:src="@drawable/ic_add"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>