Skip to content

Commit

Permalink
allow editing date
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Aug 30, 2024
1 parent 881f69b commit e74059a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 56 deletions.

This file was deleted.

7 changes: 4 additions & 3 deletions app/src/main/java/io/github/jan/kex/data/remote/ExamApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface ExamApi {

suspend fun retrieveExamData(): List<Exam>

suspend fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?)
suspend fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?, date: String?)

suspend fun createExam(subject: String, date: String, theme: String, type: Exam.Type): Exam

Expand Down Expand Up @@ -92,14 +92,15 @@ internal class ExamApiImpl(
}

@OptIn(SupabaseInternal::class)
override suspend fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?) {
override suspend fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?, date: String?) {
exams.upsert(buildJsonObject {
putJsonObject(
Json.encodeToJsonElement(
exam.copy(
theme = theme,
subject = subject,
points = points
points = points,
date = date?.toCustomLocalDate() ?: exam.date
)
).jsonObject
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ fun AppNavigationContent(
ExamEditScreen(
exam = exam,
suggestions = subjectSuggestions,
onEdit = { subject, theme, points ->
onEdit = { subject, theme, points, date ->
examVm.updateExam(
selectedExam,
subject,
theme,
points?.toLongOrNull()
points?.toLongOrNull(),
date
)
navController.popBackStack()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -25,18 +21,22 @@ import androidx.compose.ui.unit.sp
import com.mohamedrejeb.richeditor.model.RichTextState
import io.github.jan.kex.R
import io.github.jan.kex.data.remote.Exam
import io.github.jan.kex.ui.components.DatePickerField
import io.github.jan.kex.ui.components.SubjectField
import io.github.jan.kex.ui.components.ThemeEditor
import io.github.jan.kex.ui.icons.rememberNumbersIcon
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ExamEditScreen(
exam: Exam,
suggestions: List<String>,
onEdit: (subject: String, theme: String?, points: String?) -> Unit
onEdit: (subject: String, theme: String?, points: String?, date: String?) -> Unit
) {
Column(
// modifier = Modifier.padding(16.dp),
Expand All @@ -59,7 +59,22 @@ fun ExamEditScreen(
exam.theme?.let { setHtml(it) }
}) }
val markdownTheme = remember { mutableStateOf(TextFieldValue(richTheme.toMarkdown())) }
val datePickerState = rememberDatePickerState()
var showDatePicker by remember { mutableStateOf(false) }
val selectedDate = remember(datePickerState, showDatePicker) {
datePickerState.selectedDateMillis?.let {
val date = Instant.fromEpochMilliseconds(it).toLocalDateTime(
TimeZone.currentSystemDefault()
)
"${if(date.dayOfMonth < 10) "0" + date.dayOfMonth else date.dayOfMonth}.${if (date.monthNumber < 10) "0" + date.monthNumber else date.monthNumber}.${date.year}"
}
}
SubjectField(subject = subject, onSubjectChange = { subject = it }, isError = isError, suggestions = filteredSuggestions)
DatePickerField(
selectedDate = selectedDate,
onClick = { showDatePicker = true },
displayError = isError && selectedDate == null
)
OutlinedTextField(
value = points,
onValueChange = {
Expand All @@ -80,7 +95,7 @@ fun ExamEditScreen(
Button(
onClick = {
if (subject.text.isNotBlank()) {
onEdit(subject.text, richTheme.toHtml().ifBlank { null }, points.ifBlank { null })
onEdit(subject.text, richTheme.toHtml().ifBlank { null }, points.ifBlank { null }, selectedDate)
} else {
errorScope.launch {
isError = true
Expand All @@ -94,5 +109,19 @@ fun ExamEditScreen(
) {
Text(stringResource(id = R.string.save))
}
if (showDatePicker) {
DatePickerDialog(
onDismissRequest = { showDatePicker = false },
confirmButton = {
TextButton(onClick = { showDatePicker = false }) {
Text("Ok")
}
}
) {
DatePicker(
state = datePickerState
)
}
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/io/github/jan/kex/vm/ExamViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ class ExamViewModel(
}
}

fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?) {
fun updateExam(exam: Exam, subject: String, theme: String?, points: Long?, date: String?) {
viewModelScope.launch(Dispatchers.IO) {
subjectSuggestionDataSource.insert(exam.subject)
kotlin.runCatching {
if(!exam.offlineCreated) {
examApi.updateExam(exam, subject, theme, points)
examApi.updateExam(exam, subject, theme, points, date)
}
}.onSuccess {
val newExam = exam.copy(subject = subject, theme = theme, points = points)
val newExam = exam.copy(subject = subject, theme = theme, points = points, date = date?.toCustomLocalDate() ?: exam.date)
examDataSource.insertExams(listOf(newExam))
examNotificationManager.scheduleNotifications(newExam)
}.onFailure {
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/io/github/jan/kex/ExampleUnitTest.kt

This file was deleted.

0 comments on commit e74059a

Please sign in to comment.