Skip to content

Commit

Permalink
Remove deprecated methods in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Aug 3, 2024
1 parent f71c72b commit 395eb2a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.kizitonwose.calendar.sample.view

import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.kizitonwose.calendar.sample.R

interface HasToolbar {
Expand All @@ -18,6 +22,13 @@ abstract class BaseFragment(@LayoutRes layoutRes: Int) : Fragment(layoutRes) {
val activityToolbar: Toolbar
get() = (requireActivity() as CalendarViewActivity).binding.activityToolbar

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (this is MenuProvider) {
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.CREATED)
}
}

override fun onStart() {
super.onStart()
if (this is HasToolbar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.view.MenuProvider
import androidx.core.view.children
import androidx.core.view.updatePaddingRelative
import com.kizitonwose.calendar.core.CalendarDay
Expand All @@ -25,7 +26,7 @@ import com.kizitonwose.calendar.view.ViewContainer
import java.time.LocalDate
import java.time.Year

class Example10Fragment : BaseFragment(R.layout.example_10_fragment), HasToolbar, HasBackButton {
class Example10Fragment : BaseFragment(R.layout.example_10_fragment), HasToolbar, HasBackButton, MenuProvider {
override val toolbar: Toolbar
get() = binding.exTenToolbar

Expand All @@ -37,7 +38,6 @@ class Example10Fragment : BaseFragment(R.layout.example_10_fragment), HasToolbar

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
binding = Example10FragmentBinding.bind(view)
val config = requireContext().resources.configuration
val isTablet = config.smallestScreenWidthDp >= 600
Expand Down Expand Up @@ -152,28 +152,29 @@ class Example10Fragment : BaseFragment(R.layout.example_10_fragment), HasToolbar
}
}
}

}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.example_10_menu, menu)
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.example_10_menu, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val visibleYear = binding.exTenCalendar.findFirstVisibleYear()?.year
?: return super.onOptionsItemSelected(item)
override fun onMenuItemSelected(item: MenuItem): Boolean = with(binding.exTenCalendar) {
return when (item.itemId) {
R.id.menuItemPrevious -> {
binding.exTenCalendar.smoothScrollToYear(visibleYear.minusYears(1))
findFirstVisibleYear()?.year?.let { visibleYear ->
smoothScrollToYear(visibleYear.minusYears(1))
}
true
}

R.id.menuItemNext -> {
binding.exTenCalendar.smoothScrollToYear(visibleYear.plusYears(1))
findFirstVisibleYear()?.year?.let { visibleYear ->
smoothScrollToYear(visibleYear.plusYears(1))
}
true
}

else -> super.onOptionsItemSelected(item)
else -> false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.view.MenuProvider
import androidx.core.view.children
import com.google.android.material.snackbar.Snackbar
import com.kizitonwose.calendar.core.CalendarDay
Expand All @@ -25,7 +26,7 @@ import java.time.LocalDate
import java.time.YearMonth
import java.time.format.DateTimeFormatter

class Example2Fragment : BaseFragment(R.layout.example_2_fragment), HasToolbar, HasBackButton {
class Example2Fragment : BaseFragment(R.layout.example_2_fragment), HasToolbar, HasBackButton, MenuProvider {
override val toolbar: Toolbar
get() = binding.exTwoToolbar

Expand All @@ -38,7 +39,6 @@ class Example2Fragment : BaseFragment(R.layout.example_2_fragment), HasToolbar,

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
binding = Example2FragmentBinding.bind(view)
val daysOfWeek = daysOfWeek()
binding.legendLayout.root.children.forEachIndexed { index, child ->
Expand All @@ -57,22 +57,20 @@ class Example2Fragment : BaseFragment(R.layout.example_2_fragment), HasToolbar,
}

private lateinit var menuItem: MenuItem
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.example_2_menu, menu)
menuItem = menu.getItem(0)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.menuItemDone) {
val date = selectedDate ?: return false
menuItem = menu.findItem(R.id.menuItemDone)
menuItem.setOnMenuItemClickListener click@{
val date = selectedDate ?: return@click true
val text = "Selected: ${DateTimeFormatter.ofPattern("d MMMM yyyy").format(date)}"
Snackbar.make(requireView(), text, Snackbar.LENGTH_SHORT).show()
parentFragmentManager.popBackStack()
return true
return@click true
}
return super.onOptionsItemSelected(item)
}

override fun onMenuItemSelected(item: MenuItem): Boolean = true

private fun configureBinders() {
val calendarView = binding.exTwoCalendar

Expand Down Expand Up @@ -113,10 +111,12 @@ class Example2Fragment : BaseFragment(R.layout.example_2_fragment), HasToolbar,
textView.setTextColorRes(R.color.example_2_white)
textView.setBackgroundResource(R.drawable.example_2_selected_bg)
}

today -> {
textView.setTextColorRes(R.color.example_2_red)
textView.background = null
}

else -> {
textView.setTextColorRes(R.color.example_2_black)
textView.background = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import android.os.Bundle
import android.util.TypedValue
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat
import androidx.core.view.MenuProvider
import androidx.core.view.children
import com.google.android.material.snackbar.Snackbar
import com.kizitonwose.calendar.core.CalendarDay
Expand All @@ -35,7 +37,7 @@ import java.time.LocalDate
import java.time.YearMonth
import java.time.format.DateTimeFormatter

class Example4Fragment : BaseFragment(R.layout.example_4_fragment), HasToolbar, HasBackButton {
class Example4Fragment : BaseFragment(R.layout.example_4_fragment), HasToolbar, HasBackButton, MenuProvider {
override val toolbar: Toolbar
get() = binding.exFourToolbar

Expand All @@ -52,7 +54,6 @@ class Example4Fragment : BaseFragment(R.layout.example_4_fragment), HasToolbar,
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
addStatusBarColorUpdate(R.color.white)
setHasOptionsMenu(true)
binding = Example4FragmentBinding.bind(view)
// Set the First day of week depending on Locale
val daysOfWeek = daysOfWeek()
Expand Down Expand Up @@ -109,7 +110,7 @@ class Example4Fragment : BaseFragment(R.layout.example_4_fragment), HasToolbar,
binding.exFourSaveButton.isEnabled = selection.daysBetween != null
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
override fun onCreateMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.example_4_menu, menu)
binding.exFourToolbar.post {
// Configure menu text to match what is in the Airbnb app.
Expand All @@ -127,6 +128,8 @@ class Example4Fragment : BaseFragment(R.layout.example_4_fragment), HasToolbar,
}
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean = true

override fun onStart() {
super.onStart()
val closeIndicator = requireContext().getDrawableCompat(R.drawable.ic_close).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ public enum class MonthHeight {
* the day content.
*/
Fill,

}

0 comments on commit 395eb2a

Please sign in to comment.