Change homework view to weekly (#284)

This commit is contained in:
Mikołaj Pich 2019-03-17 00:17:16 +01:00 committed by Rafał Borcz
parent 38aa26a3ff
commit d27d069ce2
12 changed files with 140 additions and 37 deletions

View File

@ -19,6 +19,6 @@ interface HomeworkDao {
@Delete @Delete
fun deleteAll(homework: List<Homework>) fun deleteAll(homework: List<Homework>)
@Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date = :date") @Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(semesterId: Int, studentId: Int, date: LocalDate): Maybe<List<Homework>> fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Homework>>
} }

View File

@ -19,7 +19,7 @@ class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) {
homeworkDb.deleteAll(homework) homeworkDb.deleteAll(homework)
} }
fun getHomework(semester: Semester, date: LocalDate): Maybe<List<Homework>> { fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Homework>> {
return homeworkDb.loadAll(semester.semesterId, semester.studentId, date).filter { it.isNotEmpty() } return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() }
} }
} }

View File

@ -12,9 +12,9 @@ import javax.inject.Singleton
@Singleton @Singleton
class HomeworkRemote @Inject constructor(private val api: Api) { class HomeworkRemote @Inject constructor(private val api: Api) {
fun getHomework(semester: Semester, date: LocalDate): Single<List<Homework>> { fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<Homework>> {
return Single.just(api.apply { diaryId = semester.diaryId }) return Single.just(api.apply { diaryId = semester.diaryId })
.flatMap { it.getHomework(date, date) } .flatMap { it.getHomework(startDate, endDate) }
.map { homework -> .map { homework ->
homework.map { homework.map {
Homework( Homework(

View File

@ -4,6 +4,8 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Homework
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.utils.friday
import io.github.wulkanowy.utils.monday
import io.reactivex.Single import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException import java.net.UnknownHostException
@ -17,18 +19,20 @@ class HomeworkRepository @Inject constructor(
private val remote: HomeworkRemote private val remote: HomeworkRemote
) { ) {
fun getHomework(semester: Semester, date: LocalDate, forceRefresh: Boolean = false): Single<List<Homework>> { fun getHomework(semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single<List<Homework>> {
return local.getHomework(semester, date).filter { !forceRefresh } return Single.fromCallable { start.monday to end.friday }.flatMap { (monday, friday) ->
local.getHomework(semester, monday, friday).filter { !forceRefresh }
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
.flatMap { .flatMap {
if (it) remote.getHomework(semester, date) if (it) remote.getHomework(semester, monday, friday)
else Single.error(UnknownHostException()) else Single.error(UnknownHostException())
}.flatMap { newGrades -> }.flatMap { newGrades ->
local.getHomework(semester, date).toSingle(emptyList()) local.getHomework(semester, monday, friday).toSingle(emptyList())
.doOnSuccess { oldGrades -> .doOnSuccess { oldGrades ->
local.deleteHomework(oldGrades - newGrades) local.deleteHomework(oldGrades - newGrades)
local.saveHomework(newGrades - oldGrades) local.saveHomework(newGrades - oldGrades)
} }
}.flatMap { local.getHomework(semester, date).toSingle(emptyList()) }) }.flatMap { local.getHomework(semester, monday, friday).toSingle(emptyList()) })
}
} }
} }

View File

@ -3,6 +3,8 @@ package io.github.wulkanowy.services.sync.works
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.data.repositories.homework.HomeworkRepository
import io.github.wulkanowy.utils.friday
import io.github.wulkanowy.utils.monday
import io.reactivex.Completable import io.reactivex.Completable
import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.now
import javax.inject.Inject import javax.inject.Inject
@ -10,6 +12,6 @@ import javax.inject.Inject
class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work {
override fun create(student: Student, semester: Semester): Completable { override fun create(student: Student, semester: Semester): Completable {
return homeworkRepository.getHomework(semester, now(), true).ignoreElement() return homeworkRepository.getHomework(semester, now().monday, now().friday, true).ignoreElement()
} }
} }

View File

@ -16,4 +16,3 @@ class TimetableWork @Inject constructor(private val timetableRepository: Timetab
.ignoreElement() .ignoreElement()
} }
} }

View File

@ -70,7 +70,7 @@ class HomeworkFragment : BaseSessionFragment(), HomeworkView, MainView.TitledVie
homeworkAdapter.clear() homeworkAdapter.clear()
} }
override fun updateNavigationDay(date: String) { override fun updateNavigationWeek(date: String) {
homeworkNavDate.text = date homeworkNavDate.text = date
} }

View File

@ -0,0 +1,54 @@
package io.github.wulkanowy.ui.modules.homework
import android.view.View
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractHeaderItem
import eu.davidea.flexibleadapter.items.IFlexible
import eu.davidea.viewholders.ExpandableViewHolder
import io.github.wulkanowy.R
import io.github.wulkanowy.utils.toFormattedString
import io.github.wulkanowy.utils.weekDayName
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.header_homework.*
import org.threeten.bp.LocalDate
class HomeworkHeader(private val date: LocalDate) : AbstractHeaderItem<HomeworkHeader.ViewHolder>() {
override fun getLayoutRes() = R.layout.header_homework
override fun createViewHolder(view: View?, adapter: FlexibleAdapter<IFlexible<*>>?): ViewHolder {
return ViewHolder(view, adapter)
}
override fun bindViewHolder(
adapter: FlexibleAdapter<IFlexible<*>>?, holder: HomeworkHeader.ViewHolder,
position: Int, payloads: MutableList<Any>?
) {
holder.run {
homeworkHeaderDay.text = date.weekDayName.capitalize()
homeworkHeaderDate.text = date.toFormattedString()
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as HomeworkHeader
if (date != other.date) return false
return true
}
override fun hashCode(): Int {
return date.hashCode()
}
class ViewHolder(view: View?, adapter: FlexibleAdapter<IFlexible<*>>?) : ExpandableViewHolder(view, adapter),
LayoutContainer {
override val containerView: View
get() = contentView
}
}

View File

@ -1,9 +1,8 @@
package io.github.wulkanowy.ui.modules.homework package io.github.wulkanowy.ui.modules.homework
import android.annotation.SuppressLint
import android.view.View import android.view.View
import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.AbstractSectionableItem
import eu.davidea.flexibleadapter.items.IFlexible import eu.davidea.flexibleadapter.items.IFlexible
import eu.davidea.viewholders.FlexibleViewHolder import eu.davidea.viewholders.FlexibleViewHolder
import io.github.wulkanowy.R import io.github.wulkanowy.R
@ -11,15 +10,16 @@ import io.github.wulkanowy.data.db.entities.Homework
import kotlinx.android.extensions.LayoutContainer import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_homework.* import kotlinx.android.synthetic.main.item_homework.*
class HomeworkItem(val homework: Homework) : AbstractFlexibleItem<HomeworkItem.ViewHolder>() { class HomeworkItem(
header: HomeworkHeader, val homework: Homework
) : AbstractSectionableItem<HomeworkItem.ViewHolder, HomeworkHeader>(header) {
override fun getLayoutRes(): Int = R.layout.item_homework override fun getLayoutRes() = R.layout.item_homework
override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<*>>): ViewHolder { override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<*>>): ViewHolder {
return ViewHolder(view, adapter) return ViewHolder(view, adapter)
} }
@SuppressLint("SetTextI18n")
override fun bindViewHolder( override fun bindViewHolder(
adapter: FlexibleAdapter<IFlexible<*>>, holder: ViewHolder, adapter: FlexibleAdapter<IFlexible<*>>, holder: ViewHolder,
position: Int, payloads: MutableList<Any>? position: Int, payloads: MutableList<Any>?

View File

@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.modules.homework
import com.google.firebase.analytics.FirebaseAnalytics.Param.START_DATE import com.google.firebase.analytics.FirebaseAnalytics.Param.START_DATE
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import io.github.wulkanowy.data.db.entities.Homework
import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.data.repositories.homework.HomeworkRepository
import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository
import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.data.repositories.student.StudentRepository
@ -9,10 +10,10 @@ import io.github.wulkanowy.ui.base.session.BaseSessionPresenter
import io.github.wulkanowy.ui.base.session.SessionErrorHandler import io.github.wulkanowy.ui.base.session.SessionErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.friday
import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextOrSameSchoolDay
import io.github.wulkanowy.utils.nextSchoolDay
import io.github.wulkanowy.utils.previousSchoolDay
import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.toFormattedString
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import timber.log.Timber import timber.log.Timber
@ -40,12 +41,12 @@ class HomeworkPresenter @Inject constructor(
} }
fun onPreviousDay() { fun onPreviousDay() {
loadData(currentDate.previousSchoolDay) loadData(currentDate.minusDays(7))
reloadView() reloadView()
} }
fun onNextDay() { fun onNextDay() {
loadData(currentDate.nextSchoolDay) loadData(currentDate.plusDays(7))
reloadView() reloadView()
} }
@ -69,8 +70,9 @@ class HomeworkPresenter @Inject constructor(
add(studentRepository.getCurrentStudent() add(studentRepository.getCurrentStudent()
.delay(200, TimeUnit.MILLISECONDS) .delay(200, TimeUnit.MILLISECONDS)
.flatMap { semesterRepository.getCurrentSemester(it) } .flatMap { semesterRepository.getCurrentSemester(it) }
.flatMap { homeworkRepository.getHomework(it, currentDate, forceRefresh) } .flatMap { homeworkRepository.getHomework(it, currentDate, currentDate, forceRefresh) }
.map { items -> items.map { HomeworkItem(it) } } .map { it.groupBy { homework -> homework.date }.toSortedMap() }
.map { createHomeworkItem(it) }
.subscribeOn(schedulers.backgroundThread) .subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread) .observeOn(schedulers.mainThread)
.doFinally { .doFinally {
@ -96,6 +98,14 @@ class HomeworkPresenter @Inject constructor(
} }
} }
private fun createHomeworkItem(items: Map<LocalDate, List<Homework>>): List<HomeworkItem> {
return items.flatMap {
HomeworkHeader(it.key).let { header ->
it.value.reversed().map { item -> HomeworkItem(header, item) }
}
}
}
private fun reloadView() { private fun reloadView() {
Timber.i("Reload homework view with the date ${currentDate.toFormattedString()}") Timber.i("Reload homework view with the date ${currentDate.toFormattedString()}")
view?.apply { view?.apply {
@ -104,9 +114,10 @@ class HomeworkPresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
showNextButton(!currentDate.plusDays(1).isHolidays) showNextButton(!currentDate.plusDays(7).isHolidays)
showPreButton(!currentDate.minusDays(1).isHolidays) showPreButton(!currentDate.minusDays(7).isHolidays)
updateNavigationDay(currentDate.toFormattedString("EEEE \n dd.MM.YYYY").capitalize()) updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " +
currentDate.friday.toFormattedString("dd.MM"))
} }
} }
} }

View File

@ -11,7 +11,7 @@ interface HomeworkView : BaseSessionView {
fun clearData() fun clearData()
fun updateNavigationDay(date: String) fun updateNavigationWeek(date: String)
fun isViewEmpty(): Boolean fun isViewEmpty(): Boolean

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorControlHighlight"
android:minHeight="40dp"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
tools:context=".ui.modules.homework.HomeworkHeader">
<TextView
android:id="@+id/homeworkHeaderDay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
tools:text="@tools:sample/date/day_of_week" />
<TextView
android:id="@+id/homeworkHeaderDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:gravity="end"
android:textSize="13sp"
tools:text="@tools:sample/date/ddmmyy" />
</LinearLayout>