diff --git a/app/src/androidTest/java/io/github/jan/kex/ExampleInstrumentedTest.kt b/app/src/androidTest/java/io/github/jan/kex/ExampleInstrumentedTest.kt deleted file mode 100644 index 27aced6..0000000 --- a/app/src/androidTest/java/io/github/jan/kex/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package io.github.jan.kex - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("io.github.jan.kex", appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/main/java/io/github/jan/kex/data/remote/ExamApi.kt b/app/src/main/java/io/github/jan/kex/data/remote/ExamApi.kt index 4b05610..fd74d6f 100644 --- a/app/src/main/java/io/github/jan/kex/data/remote/ExamApi.kt +++ b/app/src/main/java/io/github/jan/kex/data/remote/ExamApi.kt @@ -53,7 +53,7 @@ interface ExamApi { suspend fun retrieveExamData(): List - 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 @@ -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 ) diff --git a/app/src/main/java/io/github/jan/kex/ui/screen/app/AppNavigationContent.kt b/app/src/main/java/io/github/jan/kex/ui/screen/app/AppNavigationContent.kt index ec91ac8..893e87e 100644 --- a/app/src/main/java/io/github/jan/kex/ui/screen/app/AppNavigationContent.kt +++ b/app/src/main/java/io/github/jan/kex/ui/screen/app/AppNavigationContent.kt @@ -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() }) diff --git a/app/src/main/java/io/github/jan/kex/ui/screen/exam/ExamEditScreen.kt b/app/src/main/java/io/github/jan/kex/ui/screen/exam/ExamEditScreen.kt index b140afe..a271881 100644 --- a/app/src/main/java/io/github/jan/kex/ui/screen/exam/ExamEditScreen.kt +++ b/app/src/main/java/io/github/jan/kex/ui/screen/exam/ExamEditScreen.kt @@ -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 @@ -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, - onEdit: (subject: String, theme: String?, points: String?) -> Unit + onEdit: (subject: String, theme: String?, points: String?, date: String?) -> Unit ) { Column( // modifier = Modifier.padding(16.dp), @@ -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 = { @@ -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 @@ -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 + ) + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/github/jan/kex/vm/ExamViewModel.kt b/app/src/main/java/io/github/jan/kex/vm/ExamViewModel.kt index 4f4f89e..461d4ee 100644 --- a/app/src/main/java/io/github/jan/kex/vm/ExamViewModel.kt +++ b/app/src/main/java/io/github/jan/kex/vm/ExamViewModel.kt @@ -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 { diff --git a/app/src/test/java/io/github/jan/kex/ExampleUnitTest.kt b/app/src/test/java/io/github/jan/kex/ExampleUnitTest.kt deleted file mode 100644 index 14aa7f7..0000000 --- a/app/src/test/java/io/github/jan/kex/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package io.github.jan.kex - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} \ No newline at end of file