Add option to show only student plan lessons (#654)

This commit is contained in:
Mikołaj Pich
2020-01-20 22:18:26 +01:00
committed by Rafał Borcz
parent e9fa95f113
commit ad3bb3a522
21 changed files with 1835 additions and 15 deletions

View File

@ -60,6 +60,7 @@ import io.github.wulkanowy.data.db.migrations.Migration17
import io.github.wulkanowy.data.db.migrations.Migration18
import io.github.wulkanowy.data.db.migrations.Migration19
import io.github.wulkanowy.data.db.migrations.Migration2
import io.github.wulkanowy.data.db.migrations.Migration20
import io.github.wulkanowy.data.db.migrations.Migration3
import io.github.wulkanowy.data.db.migrations.Migration4
import io.github.wulkanowy.data.db.migrations.Migration5
@ -101,7 +102,7 @@ import javax.inject.Singleton
abstract class AppDatabase : RoomDatabase() {
companion object {
const val VERSION_SCHEMA = 19
const val VERSION_SCHEMA = 20
fun getMigrations(sharedPrefProvider: SharedPrefProvider): Array<Migration> {
return arrayOf(
@ -122,7 +123,8 @@ abstract class AppDatabase : RoomDatabase() {
Migration16(),
Migration17(),
Migration18(),
Migration19(sharedPrefProvider)
Migration19(sharedPrefProvider),
Migration20()
)
}

View File

@ -40,6 +40,9 @@ data class Timetable(
val info: String,
@ColumnInfo(name = "student_plan")
val studentPlan: Boolean,
val changes: Boolean,
val canceled: Boolean

View File

@ -0,0 +1,33 @@
package io.github.wulkanowy.data.db.migrations
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
class Migration20 : Migration(19, 20) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE Timetable")
database.execSQL("""
CREATE TABLE IF NOT EXISTS `Timetable` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`student_id` INTEGER NOT NULL,
`diary_id` INTEGER NOT NULL,
`number` INTEGER NOT NULL,
`start` INTEGER NOT NULL,
`end` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
`subject` TEXT NOT NULL,
`subjectOld` TEXT NOT NULL,
`group` TEXT NOT NULL,
`room` TEXT NOT NULL,
`roomOld` TEXT NOT NULL,
`teacher` TEXT NOT NULL,
`teacherOld` TEXT NOT NULL,
`info` TEXT NOT NULL,
`student_plan` INTEGER NOT NULL,
`changes` INTEGER NOT NULL,
`canceled` INTEGER NOT NULL
)
""")
}
}

View File

@ -65,6 +65,9 @@ class PreferencesRepository @Inject constructor(
val fillMessageContent: Boolean
get() = getBoolean(R.string.pref_key_fill_message_content, R.bool.pref_default_fill_message_content)
val showWholeClassPlan: String
get() = getString(R.string.pref_key_timetable_show_whole_class, R.string.pref_default_timetable_show_whole_class)
private fun getString(id: Int, default: Int) = getString(context.getString(id), default)
private fun getString(id: String, default: Int) = sharedPref.getString(id, context.getString(default)) ?: context.getString(default)

View File

@ -36,7 +36,7 @@ class SemesterRepository @Inject constructor(
local.saveSemesters(new.uniqueSubtract(old))
}
} else {
Timber.i("Current semesters list:\n${currentSemesters.joinToString(separator = "\n")}")
Timber.i("Current semesters list:\n${new.joinToString(separator = "\n")}")
throw IllegalArgumentException("Current semester can be only one.")
}
}.flatMap { local.getSemesters(student).toSingle(emptyList()) })

View File

@ -30,6 +30,7 @@ class TimetableRemote @Inject constructor(private val sdk: Sdk) {
teacher = it.teacher,
teacherOld = it.teacherOld,
info = it.info,
studentPlan = it.studentPlan,
changes = it.changes,
canceled = it.canceled
)

View File

@ -15,11 +15,15 @@ import io.github.wulkanowy.utils.getThemeAttrColor
import io.github.wulkanowy.utils.toFormattedString
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_timetable.*
import kotlinx.android.synthetic.main.item_timetable_small.*
class TimetableItem(val lesson: Timetable) :
class TimetableItem(val lesson: Timetable, private val showWholeClassPlan: String) :
AbstractFlexibleItem<TimetableItem.ViewHolder>() {
override fun getLayoutRes() = R.layout.item_timetable
override fun getLayoutRes() = when {
showWholeClassPlan == "small" && !lesson.studentPlan -> R.layout.item_timetable_small
else -> R.layout.item_timetable
}
override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<*>>): ViewHolder {
return ViewHolder(view, adapter)
@ -27,16 +31,29 @@ class TimetableItem(val lesson: Timetable) :
@SuppressLint("SetTextI18n")
override fun bindViewHolder(adapter: FlexibleAdapter<IFlexible<*>>, holder: ViewHolder, position: Int, payloads: MutableList<Any>?) {
updateFields(holder)
when (itemViewType) {
R.layout.item_timetable_small -> {
with(holder) {
timetableSmallItemNumber.text = lesson.number.toString()
timetableSmallItemSubject.text = lesson.subject
timetableSmallItemTimeStart.text = lesson.start.toFormattedString("HH:mm")
timetableSmallItemRoom.text = lesson.room
timetableSmallItemTeacher.text = lesson.teacher
}
}
R.layout.item_timetable -> {
updateFields(holder)
with(holder) {
timetableItemSubject.paintFlags =
if (lesson.canceled) timetableItemSubject.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else timetableItemSubject.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
with(holder) {
timetableItemSubject.paintFlags =
if (lesson.canceled) timetableItemSubject.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else timetableItemSubject.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
updateDescription(holder)
updateColors(holder)
}
}
updateDescription(holder)
updateColors(holder)
}
private fun updateFields(holder: ViewHolder) {

View File

@ -2,6 +2,8 @@ package io.github.wulkanowy.ui.modules.timetable
import android.annotation.SuppressLint
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import io.github.wulkanowy.data.db.entities.Timetable
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
import io.github.wulkanowy.data.repositories.semester.SemesterRepository
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.data.repositories.timetable.TimetableRepository
@ -29,6 +31,7 @@ class TimetablePresenter @Inject constructor(
studentRepository: StudentRepository,
private val timetableRepository: TimetableRepository,
private val semesterRepository: SemesterRepository,
private val prefRepository: PreferencesRepository,
private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<TimetableView>(errorHandler, studentRepository, schedulers) {
@ -134,7 +137,7 @@ class TimetablePresenter @Inject constructor(
.flatMap { semesterRepository.getCurrentSemester(it) }
.delay(200, MILLISECONDS)
.flatMap { timetableRepository.getTimetable(it, currentDate, currentDate, forceRefresh) }
.map { items -> items.map { TimetableItem(it) } }
.map { createTimetableItems(it) }
.map { items -> items.sortedBy { it.lesson.number } }
.subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread)
@ -172,6 +175,12 @@ class TimetablePresenter @Inject constructor(
}
}
private fun createTimetableItems(items: List<Timetable>): List<TimetableItem> {
return items
.filter { if (prefRepository.showWholeClassPlan == "no") it.studentPlan else true }
.map { TimetableItem(it, prefRepository.showWholeClassPlan) }
}
private fun reloadView() {
Timber.i("Reload timetable view with the date ${currentDate.toFormattedString()}")
view?.apply {