[UI] Refactor dropdown inputs code.

This commit is contained in:
Kuba Szczodrzyński 2021-04-14 11:59:58 +02:00
parent 297867cbf3
commit 07fb1e0e12
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
8 changed files with 87 additions and 116 deletions

View File

@ -23,9 +23,7 @@ import pl.szczodrzynski.edziennik.data.api.events.ApiTaskAllFinishedEvent
import pl.szczodrzynski.edziennik.data.api.events.ApiTaskErrorEvent import pl.szczodrzynski.edziennik.data.api.events.ApiTaskErrorEvent
import pl.szczodrzynski.edziennik.data.api.events.ApiTaskFinishedEvent import pl.szczodrzynski.edziennik.data.api.events.ApiTaskFinishedEvent
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.db.entity.Event import pl.szczodrzynski.edziennik.data.db.entity.*
import pl.szczodrzynski.edziennik.data.db.entity.Metadata
import pl.szczodrzynski.edziennik.data.db.entity.Profile
import pl.szczodrzynski.edziennik.data.db.full.EventFull import pl.szczodrzynski.edziennik.data.db.full.EventFull
import pl.szczodrzynski.edziennik.data.db.full.LessonFull import pl.szczodrzynski.edziennik.data.db.full.LessonFull
import pl.szczodrzynski.edziennik.databinding.DialogEventManualV2Binding import pl.szczodrzynski.edziennik.databinding.DialogEventManualV2Binding
@ -396,11 +394,11 @@ class EventManualDialog(
private fun saveEvent() { private fun saveEvent() {
val date = b.dateDropdown.getSelected() as? Date val date = b.dateDropdown.getSelected() as? Date
val timeSelected = b.timeDropdown.getSelected() val timeSelected = b.timeDropdown.getSelected()
val teamId = b.teamDropdown.getSelected() as? Long val team = b.teamDropdown.getSelected()
val type = b.typeDropdown.selected?.id val type = b.typeDropdown.getSelected()
val topic = b.topic.text?.toString() val topic = b.topic.text?.toString()
val subjectId = b.subjectDropdown.getSelected() as? Long val subject = b.subjectDropdown.getSelected() as? Subject
val teacherId = b.teacherDropdown.getSelected() val teacher = b.teacherDropdown.getSelected()
val share = b.shareSwitch.isChecked val share = b.shareSwitch.isChecked
@ -431,7 +429,7 @@ class EventManualDialog(
isError = true isError = true
} }
if (share && teamId == null) { if (share && team == null) {
b.teamDropdown.error = app.getString(R.string.dialog_event_manual_team_choose) b.teamDropdown.error = app.getString(R.string.dialog_event_manual_team_choose)
if (!isError) b.teamDropdown.parent.requestChildFocus(b.teamDropdown, b.teamDropdown) if (!isError) b.teamDropdown.parent.requestChildFocus(b.teamDropdown, b.teamDropdown)
isError = true isError = true
@ -467,10 +465,10 @@ class EventManualDialog(
time = startTime, time = startTime,
topic = topic, topic = topic,
color = customColor, color = customColor,
type = type ?: Event.TYPE_DEFAULT, type = type?.id ?: Event.TYPE_DEFAULT,
teacherId = teacherId ?: -1, teacherId = teacher?.id ?: -1,
subjectId = subjectId ?: -1, subjectId = subject?.id ?: -1,
teamId = teamId ?: -1, teamId = team?.id ?: -1,
addedDate = editingEvent?.addedDate ?: System.currentTimeMillis() addedDate = editingEvent?.addedDate ?: System.currentTimeMillis()
).also { ).also {
it.addedManually = true it.addedManually = true
@ -478,7 +476,7 @@ class EventManualDialog(
val metadataObject = Metadata( val metadataObject = Metadata(
profileId, profileId,
when (type) { when (type?.id) {
Event.TYPE_HOMEWORK -> Metadata.TYPE_HOMEWORK Event.TYPE_HOMEWORK -> Metadata.TYPE_HOMEWORK
else -> Metadata.TYPE_EVENT else -> Metadata.TYPE_EVENT
}, },
@ -578,10 +576,10 @@ class EventManualDialog(
} }
onSaveListener?.invoke(eventObject.withMetadata(metadataObject).also { onSaveListener?.invoke(eventObject.withMetadata(metadataObject).also {
it.subjectLongName = b.subjectDropdown.selected?.text?.toString() it.subjectLongName = (b.subjectDropdown.getSelected() as? Subject)?.longName
it.teacherName = b.teacherDropdown.selected?.text?.toString() it.teacherName = b.teacherDropdown.getSelected()?.fullName
it.teamName = b.teamDropdown.selected?.text?.toString() it.teamName = b.teamDropdown.getSelected()?.name
it.typeName = b.typeDropdown.selected?.text?.toString() it.typeName = b.typeDropdown.getSelected()?.name
}) })
dialog.dismiss() dialog.dismiss()
Toast.makeText(activity, R.string.saved, Toast.LENGTH_SHORT).show() Toast.makeText(activity, R.string.saved, Toast.LENGTH_SHORT).show()

View File

@ -175,7 +175,7 @@ class DateDropdown : TextInputDropDown {
} }
} }
fun pickerDialog() { private fun pickerDialog() {
val date = getSelected() as? Date ?: Date.getToday() val date = getSelected() as? Date ?: Date.getToday()
MaterialDatePicker.Builder.datePicker() MaterialDatePicker.Builder.datePicker()

View File

@ -5,9 +5,7 @@
package pl.szczodrzynski.edziennik.ui.modules.views package pl.szczodrzynski.edziennik.ui.modules.views
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.mikepenz.iconics.utils.colorInt import com.mikepenz.iconics.utils.colorInt
@ -23,18 +21,6 @@ class EventTypeDropdown : TextInputDropDown {
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val activity: AppCompatActivity?
get() {
var context: Context? = context ?: return null
if (context is AppCompatActivity) return context
while (context is ContextWrapper) {
if (context is AppCompatActivity)
return context
context = context.baseContext
}
return null
}
lateinit var db: AppDb lateinit var db: AppDb
var profileId: Int = 0 var profileId: Int = 0
var onTypeSelected: ((eventType: EventType) -> Unit)? = null var onTypeSelected: ((eventType: EventType) -> Unit)? = null
@ -83,9 +69,7 @@ class EventTypeDropdown : TextInputDropDown {
/** /**
* Select an event type by the [typeId]. * Select an event type by the [typeId].
*/ */
fun selectType(typeId: Long) { fun selectType(typeId: Long) = select(typeId)
select(typeId)
}
/** /**
* Select an event type by the [typeId] **if it's not selected yet**. * Select an event type by the [typeId] **if it's not selected yet**.

View File

@ -15,6 +15,7 @@ import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.crc16 import pl.szczodrzynski.edziennik.crc16
import pl.szczodrzynski.edziennik.data.db.AppDb import pl.szczodrzynski.edziennik.data.db.AppDb
import pl.szczodrzynski.edziennik.data.db.entity.Subject
import pl.szczodrzynski.edziennik.ui.dialogs.input import pl.szczodrzynski.edziennik.ui.dialogs.input
import pl.szczodrzynski.edziennik.utils.TextInputDropDown import pl.szczodrzynski.edziennik.utils.TextInputDropDown
@ -40,7 +41,7 @@ class SubjectDropdown : TextInputDropDown {
var showNoSubject = true var showNoSubject = true
var showCustomSubject = false var showCustomSubject = false
var customSubjectName = "" var customSubjectName = ""
var onSubjectSelected: ((subjectId: Long?) -> Unit)? = null var onSubjectSelected: ((subject: Subject?) -> Unit)? = null
var onCustomSubjectSelected: ((subjectName: String) -> Unit)? = null var onCustomSubjectSelected: ((subjectName: String) -> Unit)? = null
override fun create(context: Context) { override fun create(context: Context) {
@ -73,7 +74,7 @@ class SubjectDropdown : TextInputDropDown {
list += subjects.map { Item( list += subjects.map { Item(
it.id, it.id,
it.longName, it.longName,
tag = it.id tag = it
) } ) }
list list
@ -91,10 +92,11 @@ class SubjectDropdown : TextInputDropDown {
} }
-1L -> { -1L -> {
// no subject // no subject
deselect()
onSubjectSelected?.invoke(null) onSubjectSelected?.invoke(null)
true false
} }
is Long -> { is Subject -> {
// selected a subject // selected a subject
onSubjectSelected?.invoke(it.tag) onSubjectSelected?.invoke(it.tag)
true true
@ -104,7 +106,7 @@ class SubjectDropdown : TextInputDropDown {
} }
} }
fun customNameDialog() { private fun customNameDialog() {
activity ?: return activity ?: return
MaterialAlertDialogBuilder(activity!!) MaterialAlertDialogBuilder(activity!!)
.setTitle("Własny przedmiot") .setTitle("Własny przedmiot")
@ -127,32 +129,37 @@ class SubjectDropdown : TextInputDropDown {
.show() .show()
} }
fun selectSubject(subjectId: Long) { /**
if (select(subjectId) == null) * Select a subject by the [subjectId].
select(Item( */
subjectId, fun selectSubject(subjectId: Long): Item? {
"nieznany przedmiot ($subjectId)", if (subjectId == -1L) {
tag = subjectId deselect()
)) return null
}
return select(subjectId)
} }
fun selectDefault(subjectId: Long?) { /**
* Select a subject by the [subjectId] **if it's not selected yet**.
*/
fun selectDefault(subjectId: Long?): Item? {
if (subjectId == null || selected != null) if (subjectId == null || selected != null)
return return null
selectSubject(subjectId) return selectSubject(subjectId)
} }
/** /**
* Get the currently selected subject. * Get the currently selected subject.
* ### Returns: * ### Returns:
* - null if no valid subject is selected * - null if no valid subject is selected
* - [Long] - the selected subject's ID * - [Subject] - the selected subject
* - [String] - a custom subject name entered, if [showCustomSubject] == true * - [String] - a custom subject name entered, if [showCustomSubject] == true
*/ */
fun getSelected(): Any? { fun getSelected(): Any? {
return when (selected?.tag) { return when (selected?.tag) {
-1L -> null -1L -> null
is Long -> selected?.tag as Long is Subject -> selected?.tag as Subject
is String -> selected?.tag as String is String -> selected?.tag as String
else -> null else -> null
} }

View File

@ -5,13 +5,12 @@
package pl.szczodrzynski.edziennik.ui.modules.views package pl.szczodrzynski.edziennik.ui.modules.views
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.AppDb import pl.szczodrzynski.edziennik.data.db.AppDb
import pl.szczodrzynski.edziennik.data.db.entity.Teacher
import pl.szczodrzynski.edziennik.utils.TextInputDropDown import pl.szczodrzynski.edziennik.utils.TextInputDropDown
class TeacherDropdown : TextInputDropDown { class TeacherDropdown : TextInputDropDown {
@ -19,22 +18,10 @@ class TeacherDropdown : TextInputDropDown {
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val activity: AppCompatActivity?
get() {
var context: Context? = context ?: return null
if (context is AppCompatActivity) return context
while (context is ContextWrapper) {
if (context is AppCompatActivity)
return context
context = context.baseContext
}
return null
}
lateinit var db: AppDb lateinit var db: AppDb
var profileId: Int = 0 var profileId: Int = 0
var showNoTeacher = true var showNoTeacher = true
var onTeacherSelected: ((teacherId: Long?) -> Unit)? = null var onTeacherSelected: ((teacher: Teacher?) -> Unit)? = null
override fun create(context: Context) { override fun create(context: Context) {
super.create(context) super.create(context)
@ -58,7 +45,7 @@ class TeacherDropdown : TextInputDropDown {
list += teachers.map { Item( list += teachers.map { Item(
it.id, it.id,
it.fullName, it.fullName,
tag = it.id tag = it
) } ) }
list list
@ -71,10 +58,11 @@ class TeacherDropdown : TextInputDropDown {
when (it.tag) { when (it.tag) {
-1L -> { -1L -> {
// no teacher // no teacher
deselect()
onTeacherSelected?.invoke(null) onTeacherSelected?.invoke(null)
true false
} }
is Long -> { is Teacher -> {
// selected a teacher // selected a teacher
onTeacherSelected?.invoke(it.tag) onTeacherSelected?.invoke(it.tag)
true true
@ -87,34 +75,33 @@ class TeacherDropdown : TextInputDropDown {
/** /**
* Select a teacher by the [teacherId]. * Select a teacher by the [teacherId].
*/ */
fun selectTeacher(teacherId: Long) { fun selectTeacher(teacherId: Long): Item? {
if (select(teacherId) == null) if (teacherId == -1L) {
select(Item( deselect()
teacherId, return null
"nieznany nauczyciel ($teacherId)", }
tag = teacherId return select(teacherId)
))
} }
/** /**
* Select a teacher by the [teacherId] **if it's not selected yet**. * Select a teacher by the [teacherId] **if it's not selected yet**.
*/ */
fun selectDefault(teacherId: Long?) { fun selectDefault(teacherId: Long?): Item? {
if (teacherId == null || selected != null) if (teacherId == null || selected != null)
return return null
selectTeacher(teacherId) return selectTeacher(teacherId)
} }
/** /**
* Get the currently selected teacher. * Get the currently selected teacher.
* ### Returns: * ### Returns:
* - null if no valid teacher is selected * - null if no valid teacher is selected
* - [Long] - the selected teacher's ID * - [Teacher] - the selected teacher
*/ */
fun getSelected(): Long? { fun getSelected(): Teacher? {
return when (selected?.tag) { return when (selected?.tag) {
-1L -> null -1L -> null
is Long -> selected?.tag as Long is Teacher -> selected?.tag as Teacher
else -> null else -> null
} }
} }

View File

@ -5,9 +5,7 @@
package pl.szczodrzynski.edziennik.ui.modules.views package pl.szczodrzynski.edziennik.ui.modules.views
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
@ -20,22 +18,10 @@ class TeamDropdown : TextInputDropDown {
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val activity: AppCompatActivity?
get() {
var context: Context? = context ?: return null
if (context is AppCompatActivity) return context
while (context is ContextWrapper) {
if (context is AppCompatActivity)
return context
context = context.baseContext
}
return null
}
lateinit var db: AppDb lateinit var db: AppDb
var profileId: Int = 0 var profileId: Int = 0
var showNoTeam = true var showNoTeam = true
var onTeamSelected: ((teamId: Long?) -> Unit)? = null var onTeamSelected: ((team: Team?) -> Unit)? = null
override fun create(context: Context) { override fun create(context: Context) {
super.create(context) super.create(context)
@ -59,7 +45,7 @@ class TeamDropdown : TextInputDropDown {
list += teams.map { Item( list += teams.map { Item(
it.id, it.id,
it.name, it.name,
tag = it.id tag = it
) } ) }
list list
@ -72,10 +58,11 @@ class TeamDropdown : TextInputDropDown {
when (it.tag) { when (it.tag) {
-1L -> { -1L -> {
// no team // no team
deselect()
onTeamSelected?.invoke(null) onTeamSelected?.invoke(null)
true false
} }
is Long -> { is Team -> {
// selected a team // selected a team
onTeamSelected?.invoke(it.tag) onTeamSelected?.invoke(it.tag)
true true
@ -85,21 +72,29 @@ class TeamDropdown : TextInputDropDown {
} }
} }
fun selectTeam(teamId: Long) { /**
if (select(teamId) == null) * Select a teacher by the [teamId].
select(Item( */
teamId, fun selectTeam(teamId: Long): Item? {
"nieznana grupa ($teamId)", if (teamId == -1L) {
tag = teamId deselect()
)) return null
}
return select(teamId)
} }
fun selectDefault(teamId: Long?) { /**
* Select a team by the [teamId] **if it's not selected yet**.
*/
fun selectDefault(teamId: Long?): Item? {
if (teamId == null || selected != null) if (teamId == null || selected != null)
return return null
selectTeam(teamId) return selectTeam(teamId)
} }
/**
* Select a team of the [Team.TYPE_CLASS] type.
*/
fun selectTeamClass() { fun selectTeamClass() {
select(items.singleOrNull { select(items.singleOrNull {
it.tag is Team && it.tag.type == Team.TYPE_CLASS it.tag is Team && it.tag.type == Team.TYPE_CLASS
@ -110,12 +105,12 @@ class TeamDropdown : TextInputDropDown {
* Get the currently selected team. * Get the currently selected team.
* ### Returns: * ### Returns:
* - null if no valid team is selected * - null if no valid team is selected
* - [Long] - the team's ID * - [Team] - the selected team
*/ */
fun getSelected(): Any? { fun getSelected(): Team? {
return when (selected?.tag) { return when (selected?.tag) {
-1L -> null -1L -> null
is Long -> selected?.tag as Long is Team -> selected?.tag as Team
else -> null else -> null
} }
} }

View File

@ -175,7 +175,7 @@ class TimeDropdown : TextInputDropDown {
return !noTimetable return !noTimetable
} }
fun pickerDialog() { private fun pickerDialog() {
val time = (getSelected() as? Pair<*, *>)?.first as? Time ?: Time.getNow() val time = (getSelected() as? Pair<*, *>)?.first as? Time ?: Time.getNow()
MaterialTimePicker.Builder() MaterialTimePicker.Builder()

View File

@ -33,7 +33,7 @@ open class TextInputDropDown : TextInputEditText {
val selectedId val selectedId
get() = selected?.id get() = selected?.id
fun updateText() { private fun updateText() {
setText(selected?.displayText ?: selected?.text) setText(selected?.displayText ?: selected?.text)
} }