Add swipe refresh to grade fragment (#287)

This commit is contained in:
Rafał Borcz 2019-03-20 20:45:26 +01:00 committed by Mikołaj Pich
parent 8db73e9459
commit 575e244b3a
6 changed files with 57 additions and 11 deletions

View File

@ -14,6 +14,7 @@ cache:
branches: branches:
only: only:
- master - master
- 0.7.x
android: android:
licenses: licenses:

View File

@ -77,7 +77,7 @@ play {
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation('com.github.wulkanowy:api:0aea843800') { exclude module: "threetenbp" } implementation('com.github.wulkanowy:api:bc60169383') { exclude module: "threetenbp" }
implementation "androidx.legacy:legacy-support-v4:1.0.0" implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.appcompat:appcompat:1.0.2" implementation "androidx.appcompat:appcompat:1.0.2"

View File

@ -29,6 +29,8 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView,
@Inject @Inject
lateinit var pagerAdapter: BaseFragmentPagerAdapter lateinit var pagerAdapter: BaseFragmentPagerAdapter
private var semesterSwitchMenu: MenuItem? = null
companion object { companion object {
private const val SAVED_SEMESTER_KEY = "CURRENT_SEMESTER" private const val SAVED_SEMESTER_KEY = "CURRENT_SEMESTER"
@ -57,6 +59,8 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView,
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.action_menu_grade, menu) inflater?.inflate(R.menu.action_menu_grade, menu)
semesterSwitchMenu = menu?.findItem(R.id.gradeMenuSemester)
presenter.onCreateMenu()
} }
override fun initView() { override fun initView() {
@ -75,6 +79,7 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView,
setOnSelectPageListener { presenter.onPageSelected(it) } setOnSelectPageListener { presenter.onPageSelected(it) }
} }
gradeTabLayout.setupWithViewPager(gradeViewPager) gradeTabLayout.setupWithViewPager(gradeViewPager)
gradeSwipe.setOnRefreshListener { presenter.onSwipeRefresh() }
} }
override fun onOptionsItemSelected(item: MenuItem?): Boolean { override fun onOptionsItemSelected(item: MenuItem?): Boolean {
@ -95,8 +100,20 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView,
gradeProgress.visibility = if (show) VISIBLE else INVISIBLE gradeProgress.visibility = if (show) VISIBLE else INVISIBLE
} }
override fun showEmpty() { override fun showEmpty(show: Boolean) {
gradeEmpty.visibility = VISIBLE gradeEmpty.visibility = if (show) VISIBLE else INVISIBLE
}
override fun showRefresh(show: Boolean) {
gradeSwipe.isRefreshing = show
}
override fun showSemesterSwitch(show: Boolean) {
semesterSwitchMenu?.isVisible = show
}
override fun enableSwipe(enable: Boolean) {
gradeSwipe.isEnabled = enable
} }
override fun showSemesterDialog(selectedIndex: Int) { override fun showSemesterDialog(selectedIndex: Int) {

View File

@ -33,11 +33,18 @@ class GradePresenter @Inject constructor(
disposable.add(Completable.timer(150, MILLISECONDS, schedulers.mainThread) disposable.add(Completable.timer(150, MILLISECONDS, schedulers.mainThread)
.subscribe { .subscribe {
selectedIndex = savedIndex ?: 0 selectedIndex = savedIndex ?: 0
view.initView() view.run {
initView()
enableSwipe(false)
}
loadData() loadData()
}) })
} }
fun onCreateMenu() {
if (semesters.isEmpty()) view?.showSemesterSwitch(false)
}
fun onViewReselected() { fun onViewReselected() {
Timber.i("Grade view is reselected") Timber.i("Grade view is reselected")
view?.run { notifyChildParentReselected(currentPageIndex) } view?.run { notifyChildParentReselected(currentPageIndex) }
@ -69,6 +76,7 @@ class GradePresenter @Inject constructor(
view?.apply { view?.apply {
showContent(true) showContent(true)
showProgress(false) showProgress(false)
showEmpty(false)
loadedSemesterId[currentPageIndex] = semesterId loadedSemesterId[currentPageIndex] = semesterId
} }
} }
@ -77,6 +85,10 @@ class GradePresenter @Inject constructor(
loadChild(index) loadChild(index)
} }
fun onSwipeRefresh() {
loadData()
}
private fun loadData() { private fun loadData() {
Timber.i("Loading grade data started") Timber.i("Loading grade data started")
disposable.add(studentRepository.getCurrentStudent() disposable.add(studentRepository.getCurrentStudent()
@ -89,17 +101,21 @@ class GradePresenter @Inject constructor(
} }
.subscribeOn(schedulers.backgroundThread) .subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread) .observeOn(schedulers.mainThread)
.doFinally { view?.showRefresh(false) }
.subscribe({ .subscribe({
view?.run { view?.run {
Timber.i("Loading grade result: Attempt load index $currentPageIndex") Timber.i("Loading grade result: Attempt load index $currentPageIndex")
loadChild(currentPageIndex) loadChild(currentPageIndex)
enableSwipe(false)
showSemesterSwitch(true)
} }
}) { }) {
Timber.i("Loading grade result: An exception occurred") Timber.i("Loading grade result: An exception occurred")
errorHandler.dispatch(it) errorHandler.dispatch(it)
view?.run { view?.run {
showProgress(false) showProgress(false)
showEmpty() showEmpty(true)
enableSwipe(true)
} }
}) })
} }

View File

@ -12,10 +12,16 @@ interface GradeView : BaseSessionView {
fun showProgress(show: Boolean) fun showProgress(show: Boolean)
fun showEmpty() fun showEmpty(show: Boolean)
fun showRefresh(show: Boolean)
fun showSemesterSwitch(show: Boolean)
fun showSemesterDialog(selectedIndex: Int) fun showSemesterDialog(selectedIndex: Int)
fun enableSwipe(enable: Boolean)
fun notifyChildLoadData(index: Int, semesterId: Int, forceRefresh: Boolean) fun notifyChildLoadData(index: Int, semesterId: Int, forceRefresh: Boolean)
fun notifyChildParentReselected(index: Int) fun notifyChildParentReselected(index: Int)

View File

@ -17,13 +17,19 @@
app:tabTextColor="@android:color/white" app:tabTextColor="@android:color/white"
tools:visibility="visible" /> tools:visibility="visible" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/gradeSwipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp">
<androidx.viewpager.widget.ViewPager <androidx.viewpager.widget.ViewPager
android:id="@+id/gradeViewPager" android:id="@+id/gradeViewPager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="48dp"
android:visibility="invisible" android:visibility="invisible"
tools:visibility="visible" /> tools:visibility="visible" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar <ProgressBar
android:id="@+id/gradeProgress" android:id="@+id/gradeProgress"