diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt index 4127460f..253bdb11 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt @@ -19,6 +19,6 @@ interface HomeworkDao { @Delete fun deleteAll(homework: List) - @Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date = :date") - fun loadAll(semesterId: Int, studentId: Int, date: LocalDate): Maybe> + @Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end") + fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe> } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt index 55e51fca..7af94842 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt @@ -19,7 +19,7 @@ class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) { homeworkDb.deleteAll(homework) } - fun getHomework(semester: Semester, date: LocalDate): Maybe> { - return homeworkDb.loadAll(semester.semesterId, semester.studentId, date).filter { it.isNotEmpty() } + fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe> { + return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt index 79884c9e..681b6646 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt @@ -12,9 +12,9 @@ import javax.inject.Singleton @Singleton class HomeworkRemote @Inject constructor(private val api: Api) { - fun getHomework(semester: Semester, date: LocalDate): Single> { + fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { return Single.just(api.apply { diaryId = semester.diaryId }) - .flatMap { it.getHomework(date, date) } + .flatMap { it.getHomework(startDate, endDate) } .map { homework -> homework.map { Homework( diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt index 72c09114..0447c86f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt @@ -4,6 +4,8 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork 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.Semester +import io.github.wulkanowy.utils.friday +import io.github.wulkanowy.utils.monday import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -17,18 +19,20 @@ class HomeworkRepository @Inject constructor( private val remote: HomeworkRemote ) { - fun getHomework(semester: Semester, date: LocalDate, forceRefresh: Boolean = false): Single> { - return local.getHomework(semester, date).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getHomework(semester, date) - else Single.error(UnknownHostException()) - }.flatMap { newGrades -> - local.getHomework(semester, date).toSingle(emptyList()) - .doOnSuccess { oldGrades -> - local.deleteHomework(oldGrades - newGrades) - local.saveHomework(newGrades - oldGrades) - } - }.flatMap { local.getHomework(semester, date).toSingle(emptyList()) }) + fun getHomework(semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single> { + return Single.fromCallable { start.monday to end.friday }.flatMap { (monday, friday) -> + local.getHomework(semester, monday, friday).filter { !forceRefresh } + .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) + .flatMap { + if (it) remote.getHomework(semester, monday, friday) + else Single.error(UnknownHostException()) + }.flatMap { newGrades -> + local.getHomework(semester, monday, friday).toSingle(emptyList()) + .doOnSuccess { oldGrades -> + local.deleteHomework(oldGrades - newGrades) + local.saveHomework(newGrades - oldGrades) + } + }.flatMap { local.getHomework(semester, monday, friday).toSingle(emptyList()) }) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt index 90d5f1c6..32b356c6 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt @@ -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.Student 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 org.threeten.bp.LocalDate.now import javax.inject.Inject @@ -10,6 +12,6 @@ import javax.inject.Inject class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { 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() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt index bccc07c5..743ae0e8 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt @@ -16,4 +16,3 @@ class TimetableWork @Inject constructor(private val timetableRepository: Timetab .ignoreElement() } } - diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt index ab40027d..7325e3ec 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt @@ -70,7 +70,7 @@ class HomeworkFragment : BaseSessionFragment(), HomeworkView, MainView.TitledVie homeworkAdapter.clear() } - override fun updateNavigationDay(date: String) { + override fun updateNavigationWeek(date: String) { homeworkNavDate.text = date } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkHeader.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkHeader.kt new file mode 100644 index 00000000..49023788 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkHeader.kt @@ -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() { + + override fun getLayoutRes() = R.layout.header_homework + + override fun createViewHolder(view: View?, adapter: FlexibleAdapter>?): ViewHolder { + return ViewHolder(view, adapter) + } + + override fun bindViewHolder( + adapter: FlexibleAdapter>?, holder: HomeworkHeader.ViewHolder, + position: Int, payloads: MutableList? + ) { + 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>?) : ExpandableViewHolder(view, adapter), + LayoutContainer { + + override val containerView: View + get() = contentView + } +} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt index 97ff188c..a2b8cc0a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkItem.kt @@ -1,9 +1,8 @@ package io.github.wulkanowy.ui.modules.homework -import android.annotation.SuppressLint import android.view.View 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.viewholders.FlexibleViewHolder 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.synthetic.main.item_homework.* -class HomeworkItem(val homework: Homework) : AbstractFlexibleItem() { +class HomeworkItem( + header: HomeworkHeader, val homework: Homework +) : AbstractSectionableItem(header) { - override fun getLayoutRes(): Int = R.layout.item_homework + override fun getLayoutRes() = R.layout.item_homework override fun createViewHolder(view: View, adapter: FlexibleAdapter>): ViewHolder { return ViewHolder(view, adapter) } - @SuppressLint("SetTextI18n") override fun bindViewHolder( adapter: FlexibleAdapter>, holder: ViewHolder, position: Int, payloads: MutableList? diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index 94e6f3cd..739d4494 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.modules.homework import com.google.firebase.analytics.FirebaseAnalytics.Param.START_DATE 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.semester.SemesterRepository 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.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.isHolidays +import io.github.wulkanowy.utils.monday 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 org.threeten.bp.LocalDate import timber.log.Timber @@ -40,12 +41,12 @@ class HomeworkPresenter @Inject constructor( } fun onPreviousDay() { - loadData(currentDate.previousSchoolDay) + loadData(currentDate.minusDays(7)) reloadView() } fun onNextDay() { - loadData(currentDate.nextSchoolDay) + loadData(currentDate.plusDays(7)) reloadView() } @@ -69,8 +70,9 @@ class HomeworkPresenter @Inject constructor( add(studentRepository.getCurrentStudent() .delay(200, TimeUnit.MILLISECONDS) .flatMap { semesterRepository.getCurrentSemester(it) } - .flatMap { homeworkRepository.getHomework(it, currentDate, forceRefresh) } - .map { items -> items.map { HomeworkItem(it) } } + .flatMap { homeworkRepository.getHomework(it, currentDate, currentDate, forceRefresh) } + .map { it.groupBy { homework -> homework.date }.toSortedMap() } + .map { createHomeworkItem(it) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { @@ -96,6 +98,14 @@ class HomeworkPresenter @Inject constructor( } } + private fun createHomeworkItem(items: Map>): List { + return items.flatMap { + HomeworkHeader(it.key).let { header -> + it.value.reversed().map { item -> HomeworkItem(header, item) } + } + } + } + private fun reloadView() { Timber.i("Reload homework view with the date ${currentDate.toFormattedString()}") view?.apply { @@ -104,9 +114,10 @@ class HomeworkPresenter @Inject constructor( showContent(false) showEmpty(false) clearData() - showNextButton(!currentDate.plusDays(1).isHolidays) - showPreButton(!currentDate.minusDays(1).isHolidays) - updateNavigationDay(currentDate.toFormattedString("EEEE \n dd.MM.YYYY").capitalize()) + showNextButton(!currentDate.plusDays(7).isHolidays) + showPreButton(!currentDate.minusDays(7).isHolidays) + updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " + + currentDate.friday.toFormattedString("dd.MM")) } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkView.kt index 1452b66d..546d0526 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkView.kt @@ -11,7 +11,7 @@ interface HomeworkView : BaseSessionView { fun clearData() - fun updateNavigationDay(date: String) + fun updateNavigationWeek(date: String) fun isViewEmpty(): Boolean diff --git a/app/src/main/res/layout/header_homework.xml b/app/src/main/res/layout/header_homework.xml new file mode 100644 index 00000000..526e8150 --- /dev/null +++ b/app/src/main/res/layout/header_homework.xml @@ -0,0 +1,33 @@ + + + + + + + +