Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Fixed editors not exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
icabetong committed Apr 20, 2021
1 parent 8c65e3c commit 7d7786e
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 70 deletions.
Expand Up @@ -122,62 +122,7 @@ class MainActivity : BaseActivity() {
}
}

override fun onSupportNavigateUp(): Boolean = controller?.navigateUp() == true
//
// override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// super.onActivityResult(requestCode, resultCode, data)
// if (resultCode == Activity.RESULT_OK) {
// when (requestCode) {
// TaskEditor.REQUEST_CODE_INSERT, TaskEditor.REQUEST_CODE_UPDATE -> {
// val task: Task? =
// data?.getParcelableExtra(TaskEditor.EXTRA_TASK)
// val attachments: List<Attachment>? =
// data?.getParcelableListExtra(TaskEditor.EXTRA_ATTACHMENTS)
//
// task?.also {
// if (requestCode == TaskEditor.REQUEST_CODE_INSERT)
// tasksViewModel.insert(it, attachments ?: emptyList())
// else if (requestCode == TaskEditor.REQUEST_CODE_UPDATE)
// tasksViewModel.update(it, attachments ?: emptyList())
// }
// }
// EventEditor.REQUEST_CODE_INSERT, EventEditor.REQUEST_CODE_UPDATE -> {
// val event: Event? = data?.getParcelableExtra(EventEditor.EXTRA_EVENT)
//
// event?.also {
// if (requestCode == EventEditor.REQUEST_CODE_INSERT)
// eventsViewModel.insert(it)
// else eventsViewModel.update(it)
// }
// }
// SubjectEditor.REQUEST_CODE_INSERT, SubjectEditor.REQUEST_CODE_UPDATE -> {
// val subject: Subject?
// = data?.getParcelableExtra(SubjectEditor.EXTRA_SUBJECT)
// val schedules: List<Schedule>?
// = data?.getParcelableListExtra(SubjectEditor.EXTRA_SCHEDULE)
//
// subject?.also {
// if (requestCode == SubjectEditor.REQUEST_CODE_INSERT)
// subjectsViewModel.insert(it, schedules ?: emptyList())
// else if (requestCode == SubjectEditor.REQUEST_CODE_UPDATE)
// subjectsViewModel.update(it, schedules ?: emptyList())
// }
// }
// }
// }
// }
//
// private val tasksViewModel by lazy {
// ViewModelProvider(this).get(TaskViewModel::class.java)
// }
//
// private val eventsViewModel by lazy {
// ViewModelProvider(this).get(EventViewModel::class.java)
// }
//
// private val subjectsViewModel by lazy {
// ViewModelProvider(this).get(SubjectViewModel::class.java)
// }
override fun onSupportNavigateUp(): Boolean = controller?.navigateUp() ?: false

companion object {
const val ACTION_SHORTCUT_TASK = "action:shortcut:task"
Expand Down
@@ -1,15 +1,20 @@
package com.isaiahvonrundstedt.fokus.features.event

import android.animation.ValueAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.animation.doOnEnd
import androidx.core.animation.doOnStart
import androidx.core.os.bundleOf
import androidx.core.view.children
import androidx.core.view.doOnPreDraw
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.Navigation
Expand All @@ -30,10 +35,12 @@ import com.isaiahvonrundstedt.fokus.features.subject.Subject
import com.kizitonwose.calendarview.model.CalendarDay
import com.kizitonwose.calendarview.model.CalendarMonth
import com.kizitonwose.calendarview.model.DayOwner
import com.kizitonwose.calendarview.model.InDateStyle
import com.kizitonwose.calendarview.ui.DayBinder
import com.kizitonwose.calendarview.ui.MonthHeaderFooterBinder
import com.kizitonwose.calendarview.ui.ViewContainer
import dagger.hilt.android.AndroidEntryPoint
import me.saket.cascade.overrideOverflowMenu
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -67,6 +74,8 @@ class EventFragment : BaseFragment(), BaseAdapter.ActionListener, BaseAdapter.Ar
getParentToolbar()?.run {
title = viewModel.currentMonth.format(monthYearFormatter)
menu?.clear()
overrideOverflowMenu(::customPopupProvider)
setOnMenuItemClickListener(::onMenuItemClicked)
}

with(binding.recyclerView) {
Expand All @@ -83,10 +92,9 @@ class EventFragment : BaseFragment(), BaseAdapter.ActionListener, BaseAdapter.Ar

daysOfWeek = daysOfWeekFromLocale()
binding.calendarView.apply {
setup(viewModel.currentMonth, viewModel.currentMonth,
daysOfWeek.first())
setupAsync(viewModel.startMonth, viewModel.endMonth,
setup(viewModel.startMonth, viewModel.endMonth,
daysOfWeek.first())
scrollToMonth(viewModel.currentMonth)
}

if (savedInstanceState == null)
Expand Down Expand Up @@ -161,13 +169,13 @@ class EventFragment : BaseFragment(), BaseAdapter.ActionListener, BaseAdapter.Ar
// The user is two months away from the starting month in the CalendarView
// we'll need to add more months at the start
viewModel.startMonth = viewModel.startMonth.minusMonths(2)
binding.calendarView.updateMonthRangeAsync(startMonth = viewModel.startMonth)
binding.calendarView.updateMonthRange(startMonth = viewModel.startMonth)

} else if (it.yearMonth.plusMonths(2) == viewModel.endMonth) {
// The user is two months away from the ending month in the CalendarView
// we'll need to add more months at the end
viewModel.endMonth = viewModel.endMonth.plusMonths(2)
binding.calendarView.updateMonthRangeAsync(endMonth = viewModel.endMonth)
binding.calendarView.updateMonthRange(endMonth = viewModel.endMonth)
}
}

Expand All @@ -191,6 +199,9 @@ class EventFragment : BaseFragment(), BaseAdapter.ActionListener, BaseAdapter.Ar
super.onResume()

binding.calendarView.scrollToMonth(viewModel.currentMonth)
binding.calendarView.scrollToDate(viewModel.today)
setCurrentDate(viewModel.today)

binding.actionButton.setOnClickListener {
it.transitionName = TRANSITION_ELEMENT_ROOT

Expand Down Expand Up @@ -234,6 +245,58 @@ class EventFragment : BaseFragment(), BaseAdapter.ActionListener, BaseAdapter.Ar
}
}

private fun onMenuItemClicked(item: MenuItem): Boolean {
val firstDate = binding.calendarView.findFirstVisibleDay()?.date ?: return false
val lastDate = binding.calendarView.findLastVisibleDay()?.date ?: return false

val oneWeekHeight = binding.calendarView.daySize.height
val oneMonthHeight = oneWeekHeight * 6

when(item.itemId) {
R.id.action_view_week -> {

val animator = ValueAnimator.ofInt(oneMonthHeight, oneWeekHeight)
animator.addUpdateListener { anim ->
binding.calendarView.updateLayoutParams {
height = anim.animatedValue as Int
}
}

animator.doOnEnd {
binding.calendarView.updateMonthConfiguration(
inDateStyle = InDateStyle.FIRST_MONTH,
maxRowCount = 1,
hasBoundaries = false
)
}

animator.duration = 250
animator.start()
}
R.id.action_view_month -> {

val animator = ValueAnimator.ofInt(oneMonthHeight, oneWeekHeight)
animator.addUpdateListener { anim ->
binding.calendarView.updateLayoutParams {
height = anim.animatedValue as Int
}
}

animator.doOnStart {
binding.calendarView.updateMonthConfiguration(
inDateStyle = InDateStyle.ALL_MONTHS,
maxRowCount = 6,
hasBoundaries = true
)
}

animator.duration = 250
animator.start()
}
}
return true
}

private fun bindToCalendar(day: CalendarDay, textView: TextView, view: View,
dates: List<LocalDate> = emptyList()) {

Expand Down
Expand Up @@ -32,8 +32,8 @@ class EventViewModel @Inject constructor(
events.value = _events.value?.filter { it.event.schedule!!.toLocalDate() == selectedDate }
}

var startMonth: YearMonth = currentMonth.minusMonths(5)
var endMonth: YearMonth = currentMonth.plusMonths(5)
var startMonth: YearMonth = currentMonth.minusMonths(1)
var endMonth: YearMonth = currentMonth.plusMonths(1)

init {
events.addSource(_events) { items ->
Expand Down
Expand Up @@ -110,7 +110,12 @@ class EventEditor: BaseEditor(), FragmentResultListener {

with(binding.appBarLayout.toolbar) {
inflateMenu(R.menu.menu_editor)
setNavigationOnClickListener { controller?.navigateUp() }
setNavigationOnClickListener {
if (controller?.graph?.id == R.id.navigation_container_event)
requireActivity().finish()
else controller?.navigateUp()
}

overrideOverflowMenu(::customPopupProvider)
setOnMenuItemClickListener(::onMenuItemClicked)
}
Expand Down Expand Up @@ -324,7 +329,9 @@ class EventEditor: BaseEditor(), FragmentResultListener {
viewModel.insert()
else viewModel.update()

controller?.navigateUp()
if (controller?.graph?.id == R.id.navigation_container_event)
requireActivity().finish()
else controller?.navigateUp()
}
}

Expand Down
Expand Up @@ -101,7 +101,12 @@ class SubjectEditor : BaseEditor(), BaseAdapter.ActionListener, FragmentResultLi

with(binding.appBarLayout.toolbar) {
inflateMenu(R.menu.menu_editor)
setNavigationOnClickListener { controller?.navigateUp() }
setNavigationOnClickListener {
if (controller?.graph?.id == R.id.navigation_container_subject)
requireActivity().finish()
else controller?.navigateUp()
}

overrideOverflowMenu(::customPopupProvider)
setOnMenuItemClickListener(::onMenuItemClicked)
}
Expand Down Expand Up @@ -235,7 +240,9 @@ class SubjectEditor : BaseEditor(), BaseAdapter.ActionListener, FragmentResultLi
viewModel.insert()
else viewModel.update()

controller?.navigateUp()
if (controller?.graph?.id == R.id.navigation_container_subject)
requireActivity().finish()
else controller?.navigateUp()
}
}

Expand Down
Expand Up @@ -175,7 +175,12 @@ class TaskEditor : BaseEditor(), BaseAdapter.ActionListener, FragmentResultListe

with(binding.appBarLayout.toolbar) {
inflateMenu(R.menu.menu_editor)
setNavigationOnClickListener { controller?.navigateUp() }
setNavigationOnClickListener {
if (controller?.graph?.id == R.id.navigation_container_task)
requireActivity().finish()
else controller?.navigateUp()
}

overrideOverflowMenu(::customPopupProvider)
setOnMenuItemClickListener(::onMenuItemClicked)
}
Expand Down Expand Up @@ -417,7 +422,9 @@ class TaskEditor : BaseEditor(), BaseAdapter.ActionListener, FragmentResultListe
viewModel.insert()
else viewModel.update()

controller?.navigateUp()
if (controller?.graph?.id == R.id.navigation_container_task)
requireActivity().finish()
else controller?.navigateUp()
}
}

Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/drawable/ic_hero_eye_24.xml
@@ -0,0 +1,21 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/color_on_surface">
<path
android:pathData="M14.9998,12C14.9998,13.6569 13.6566,15 11.9998,15C10.3429,15 8.9998,13.6569 8.9998,12C8.9998,10.3431 10.3429,9 11.9998,9C13.6566,9 14.9998,10.3431 14.9998,12Z"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="#4A5568"
android:strokeLineCap="round"/>
<path
android:pathData="M2.458,12C3.7323,7.9429 7.5226,5 12.0002,5C16.4778,5 20.2681,7.9429 21.5424,12C20.2681,16.0571 16.4778,19 12.0002,19C7.5226,19 3.7323,16.0571 2.458,12Z"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="#4A5568"
android:strokeLineCap="round"/>
</vector>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_event.xml
Expand Up @@ -23,7 +23,8 @@
app:cv_dayViewResource="@layout/layout_calendar_day"
app:cv_monthHeaderResource="@layout/layout_calendar_week_days"
app:cv_orientation="horizontal"
app:cv_scrollMode="paged"/>
app:cv_scrollMode="paged"
app:cv_outDateStyle="endOfRow"/>

<TextView
android:id="@+id/currentDateTextView"
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/menu/menu_events.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:title="@string/menu_view"
android:icon="@drawable/ic_hero_eye_24"
app:showAsAction="never">

<menu>
<item
android:id="@+id/action_view_week"
android:title="@string/view_options_week"
android:icon="@drawable/ic_hero_exclamation_circle_24"/>
<item
android:id="@+id/action_view_month"
android:title="@string/view_options_month"
android:icon="@drawable/ic_hero_calendar_24" />
</menu>

</item>

</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -24,6 +24,7 @@
<string name="menu_sort">Sort</string>
<string name="menu_filter">Filter</string>
<string name="menu_share">Share</string>
<string name="menu_view">View as</string>
<string name="menu_import">Import</string>
<string name="menu_clear_items">Clear Logs</string>
<string name="menu_export_to_file">Export to file</string>
Expand Down Expand Up @@ -138,6 +139,9 @@
<string name="filter_options_today_classes">Today only</string>
<string name="filter_options_tomorrow_classes">Tomorrow only</string>

<string name="view_options_month">Month</string>
<string name="view_options_week">Week</string>

<string name="field_subject_code">Subject Code</string>
<string name="field_description">Description</string>
<string name="field_days_of_week">Days of Week</string>
Expand Down

0 comments on commit 7d7786e

Please sign in to comment.