From 6fd70a01947ff569cfe4b5857a14dee612d1362d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 8 Dec 2018 12:24:52 +0100 Subject: [PATCH] Add mark as read to grade menu (#194) --- .../grade/details/GradeDetailsFragment.kt | 17 +++ .../grade/details/GradeDetailsPresenter.kt | 105 +++++++++++------- .../res/menu/action_menu_grade_details.xml | 9 ++ app/src/main/res/values-pl/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 app/src/main/res/menu/action_menu_grade_details.xml diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt index 419d9e471..81aac0eba 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt @@ -2,6 +2,9 @@ package io.github.wulkanowy.ui.modules.grade.details 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.View.GONE import android.view.View.INVISIBLE @@ -45,6 +48,11 @@ class GradeDetailsFragment : BaseFragment(), GradeDetailsView, GradeView.GradeCh override val isViewEmpty get() = gradeDetailsAdapter.isEmpty + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setHasOptionsMenu(true) + } + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_grade_details, container, false) } @@ -55,6 +63,10 @@ class GradeDetailsFragment : BaseFragment(), GradeDetailsView, GradeView.GradeCh presenter.onAttachView(this) } + override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { + inflater?.inflate(R.menu.action_menu_grade_details, menu) + } + override fun initView() { gradeDetailsAdapter.run { isAutoCollapseOnExpand = true @@ -69,6 +81,11 @@ class GradeDetailsFragment : BaseFragment(), GradeDetailsView, GradeView.GradeCh gradeDetailsSwipe.setOnRefreshListener { presenter.onSwipeRefresh() } } + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + return if (item?.itemId == R.id.gradeDetailsMenuRead) presenter.onMarkAsReadSelected() + else false + } + override fun updateData(data: List) { gradeDetailsAdapter.updateDataSet(data, true) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index 241b9f686..654550e44 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -25,12 +25,74 @@ class GradeDetailsPresenter @Inject constructor( private val preferencesRepository: PreferencesRepository ) : BasePresenter(errorHandler) { + private var currentSemesterId = 0 + override fun onAttachView(view: GradeDetailsView) { super.onAttachView(view) view.initView() } fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) { + currentSemesterId = semesterId + loadData(semesterId, forceRefresh) + } + + fun onGradeItemSelected(item: AbstractFlexibleItem<*>?) { + if (item is GradeDetailsItem) { + view?.apply { + showGradeDialog(item.grade) + if (!item.grade.isRead) { + item.grade.isRead = true + updateItem(item) + getHeaderOfItem(item)?.let { header -> + if (header is GradeDetailsHeader) { + header.newGrades-- + updateItem(header) + } + } + updateGrade(item.grade) + } + } + } + } + + fun onMarkAsReadSelected(): Boolean { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getSemesters(it) } + .flatMap { gradeRepository.getNewGrades(it.first { item -> item.semesterId == currentSemesterId }) } + .map { it.map { grade -> grade.apply { isRead = true } } } + .flatMapCompletable { gradeRepository.updateGrades(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ loadData(currentSemesterId, false) }, { errorHandler.dispatch(it) })) + return true + } + + fun onSwipeRefresh() { + view?.notifyParentRefresh() + } + + fun onParentViewReselected() { + view?.run { + if (!isViewEmpty) { + if (preferencesRepository.isGradeExpandable) collapseAllItems() + scrollToStart() + } + } + } + + fun onParentViewChangeSemester() { + view?.run { + showProgress(true) + showRefresh(false) + showContent(false) + showEmpty(false) + clearView() + } + disposable.clear() + } + + private fun loadData(semesterId: Int, forceRefresh: Boolean) { disposable.add(studentRepository.getCurrentStudent() .flatMap { semesterRepository.getSemesters(it) } .flatMap { gradeRepository.getGrades(it.first { item -> item.semesterId == semesterId }, forceRefresh) } @@ -58,49 +120,6 @@ class GradeDetailsPresenter @Inject constructor( }) } - fun onGradeItemSelected(item: AbstractFlexibleItem<*>?) { - if (item is GradeDetailsItem) { - view?.apply { - showGradeDialog(item.grade) - if (!item.grade.isRead) { - item.grade.isRead = true - updateItem(item) - getHeaderOfItem(item)?.let { header -> - if (header is GradeDetailsHeader) { - header.newGrades-- - updateItem(header) - } - } - updateGrade(item.grade) - } - } - } - } - - fun onSwipeRefresh() { - view?.notifyParentRefresh() - } - - fun onParentViewReselected() { - view?.run { - if (!isViewEmpty) { - if (preferencesRepository.isGradeExpandable) collapseAllItems() - scrollToStart() - } - } - } - - fun onParentViewChangeSemester() { - view?.run { - showProgress(true) - showRefresh(false) - showContent(false) - showEmpty(false) - clearView() - } - disposable.clear() - } - private fun createGradeItems(items: Map>): List { return items.map { it.value.calcAverage().let { average -> diff --git a/app/src/main/res/menu/action_menu_grade_details.xml b/app/src/main/res/menu/action_menu_grade_details.xml new file mode 100644 index 000000000..938908291 --- /dev/null +++ b/app/src/main/res/menu/action_menu_grade_details.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 9edff8db9..0e6d7e38a 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -59,6 +59,7 @@ Obliczona średnia Końcowa średnia Podsumowanie + Oznacz jako przeczytane %d ocena %d oceny diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0bc0699cc..e7cde31c1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -59,6 +59,7 @@ Calculated average Final average Summary + Mark as read %d grade %d grades