Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
- implemented view models
- now it uses CompositionLocalProvider to use app state
- navigation is now a modal which is bound to the left side of the screen
  • Loading branch information
aeoliux committed Oct 5, 2024
1 parent 34c2dec commit aa98086
Show file tree
Hide file tree
Showing 35 changed files with 720 additions and 435 deletions.
5 changes: 4 additions & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,24 @@ kotlin {
}

commonMain.dependencies {
// ui
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.ui)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.lifecycle.viewmodel.compose)
implementation(libs.androidx.lifecycle.runtime.compose)

// api
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.json)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.kotlinx.serialization.json)
implementation(libs.ktor.client.content.negotiation)

// both
implementation(libs.kotlinx.datetime)
}
}
Expand Down
31 changes: 12 additions & 19 deletions composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/App.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
package com.github.aeoliux.violet

import androidx.compose.material.AlertDialog
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import com.github.aeoliux.violet.app.login.LoginView
import com.github.aeoliux.violet.app.MainView
import com.github.aeoliux.violet.storage.Database
import com.github.aeoliux.violet.storage.selectAboutMe
import androidx.compose.runtime.remember
import com.github.aeoliux.violet.app.appState.AppState
import com.github.aeoliux.violet.app.appState.LocalAppState
import com.github.aeoliux.violet.app.main.AppAlertDialog
import com.github.aeoliux.violet.app.main.MainView
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
@Preview
fun App(keychain: Keychain) {
MaterialTheme {
val appState = remember { AppState().setKeychain(keychain) }

LaunchedEffect(Unit) {
AppContext.isLoggedIn.value = Database.selectAboutMe() != null
appState.appLaunchedEffect()
}

MainView(keychain)

if (AppContext.showAlert.value) {
AlertDialog(
onDismissRequest = { AppContext.showAlert.value = false },
confirmButton = { TextButton(onClick = {
AppContext.showAlert.value = false
}) { Text("Ok") } },
title = { Text(AppContext.alertTitle.value) },
text = { Text(AppContext.alertMessage.value) }
)
CompositionLocalProvider(LocalAppState provides appState) {
MainView()
AppAlertDialog()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.aeoliux.violet

import androidx.compose.ui.graphics.Color

data class Colors(val primary: Color, val secondary: Color, val background: Color)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.github.aeoliux.violet.api.bodys.attendance.AttendancesTypes
import com.github.aeoliux.violet.api.bodys.grades.Grades
import com.github.aeoliux.violet.api.bodys.grades.GradesCategories
import com.github.aeoliux.violet.api.bodys.grades.GradesComments
import com.github.aeoliux.violet.api.types.Attendance
import com.github.aeoliux.violet.api.types.AttendanceItem
import com.github.aeoliux.violet.api.types.ClassInfo
import com.github.aeoliux.violet.api.types.Grade
import com.github.aeoliux.violet.api.types.Lesson
Expand Down Expand Up @@ -96,16 +96,17 @@ class ApiClient {
suspend fun timetable(): Timetable {
val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
val weekDay = today.dayOfWeek.isoDayNumber
println(weekDay)
val weekStarts = if (weekDay > 5) {
LocalDate.fromEpochDays(today.toEpochDays() + (weekDay - 6))
LocalDate.fromEpochDays(today.toEpochDays() + 8 - weekDay)
} else {
LocalDate.fromEpochDays(today.toEpochDays() - weekDay + 1)
}

return data<Timetables>("Timetables?weekStart=${weekStarts}").toTimetableMap(classrooms)
}

suspend fun attendance(): LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>> {
suspend fun attendance(): Attendance {
return data<Attendances>("Attendances").toAttendanceMap(
colors,
data<AttendancesTypes>("Attendances/Types").Types,
Expand All @@ -115,3 +116,4 @@ class ApiClient {
}

typealias Timetable = LinkedHashMap<LocalDate, LinkedHashMap<LocalTime, List<Lesson>>>
typealias Attendance = LinkedHashMap<LocalDate, LinkedHashMap<UInt, AttendanceItem>>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class TimetableItemTeacher(

@Serializable
data class TimetableItem(
val Classroom: IdAsStringAndUrl,
val Classroom: IdAsStringAndUrl? = null,
val DateFrom: String,
val DateTo: String,
val DayNo: String,
Expand Down Expand Up @@ -56,7 +56,7 @@ data class Timetables(
lessonNo = index.toUInt(),
subject = it.Subject.Name,
teacher = "${it.Teacher.FirstName} ${it.Teacher.LastName}",
classroom = classrooms[it.Classroom.Id.toUInt()]?: return@forEach2,
classroom = classrooms[it.Classroom?.Id?.toUInt()]?: "unknown classroom",
isCanceled = it.IsCanceled,
subclassName = it.VirtualClassName
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.aeoliux.violet.api.bodys.attendance

import com.github.aeoliux.violet.api.types.Attendance
import com.github.aeoliux.violet.api.types.User
import com.github.aeoliux.violet.api.Attendance
import com.github.aeoliux.violet.api.bodys.IdAndUrl
import com.github.aeoliux.violet.api.localDateTimeFormat
import com.github.aeoliux.violet.api.types.AttendanceItem
import com.github.aeoliux.violet.api.types.User
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable
Expand All @@ -25,7 +26,7 @@ data class Attendances(val Attendances: List<AttendanceEntry>) {
colors: LinkedHashMap<UInt, String>,
types: List<AttendanceType>,
users: LinkedHashMap<UInt, User>
): LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>{
): Attendance {
return Attendances.fold(LinkedHashMap()) { acc, att ->
val date = LocalDate.parse(att.Date)
val type = types.firstOrNull { it.Id == att.Type.Id }?: return@fold acc
Expand All @@ -35,7 +36,7 @@ data class Attendances(val Attendances: List<AttendanceEntry>) {
val color = type.ColorRGB?: colors[type.Color?.Id]?: return@fold acc
val teacher = users[att.AddedBy.Id]?: return@fold acc

val attendance = Attendance(
val attendance = AttendanceItem(
addedBy = teacher.teacher(),
addDate = LocalDateTime.parse(att.AddDate, format = localDateTimeFormat),
semester = att.Semester,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.aeoliux.violet.api.types

import com.github.aeoliux.violet.api.Attendance
import kotlinx.datetime.LocalDate

fun LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>.minOrMax(accStartsWith: UInt, comparator: (acc: UInt, new: UInt) -> Boolean): UInt {
fun Attendance.minOrMax(accStartsWith: UInt, comparator: (acc: UInt, new: UInt) -> Boolean): UInt {
return this.keys.fold(accStartsWith) { acc, key ->
val attendances = this[key]!!

Expand All @@ -20,10 +21,10 @@ fun LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>.minOrMax(accStarts
}
}

fun LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>.min(): UInt {
fun Attendance.min(): UInt {
return this.minOrMax(UInt.MAX_VALUE) { acc, new -> acc > new }
}

fun LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>.max(): UInt {
fun Attendance.max(): UInt {
return this.minOrMax(UInt.MIN_VALUE) { acc, new -> new > acc }
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ data class Lesson(
val subclassName: String?
)

data class Attendance(
data class AttendanceItem(
val addedBy: String,
val addDate: LocalDateTime,
val semester: UInt,
Expand Down

This file was deleted.

Loading

0 comments on commit aa98086

Please sign in to comment.