Skip to content

Commit

Permalink
Added listing attendance, refresh operation now reports what it is do…
Browse files Browse the repository at this point in the history
…ing at specific moment, updated login page
  • Loading branch information
aeoliux committed Sep 15, 2024
1 parent c3a28b9 commit 34c2dec
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ fun App(keychain: Keychain) {
AppContext.isLoggedIn.value = Database.selectAboutMe() != null
}

if (AppContext.isLoggedIn.value) {
MainView(keychain)
} else {
LoginView(keychain)
}
MainView(keychain)

if (AppContext.showAlert.value) {
AlertDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ object AppContext {
var alertMessage: MutableState<String> = mutableStateOf("")

var databaseUpdated = mutableStateOf(false)
var statusMessage = mutableStateOf<String?>(null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ import io.ktor.client.request.forms.submitForm
import io.ktor.client.request.get
import io.ktor.http.parameters
import io.ktor.serialization.kotlinx.json.json
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.toLocalDateTime
import kotlinx.serialization.json.Json

class ApiClient {
Expand Down Expand Up @@ -90,7 +94,15 @@ class ApiClient {
}

suspend fun timetable(): Timetable {
return data<Timetables>("Timetables").toTimetableMap(classrooms)
val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
val weekDay = today.dayOfWeek.isoDayNumber
val weekStarts = if (weekDay > 5) {
LocalDate.fromEpochDays(today.toEpochDays() + (weekDay - 6))
} else {
LocalDate.fromEpochDays(today.toEpochDays() - weekDay + 1)
}

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

suspend fun attendance(): LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,39 @@ suspend fun logIn(keychain: Keychain, login: String? = null, password: String? =

suspend fun fetchData(keychain: Keychain, login: String? = null, password: String? = null): Boolean {
try {
AppContext.statusMessage.value = "Logging in to Synergia..."
logIn(keychain, login, password)

AppContext.statusMessage.value = "Getting your personal data..."
val me = AppContext.client.value.me()
Database.setAboutMe(me)

AppContext.statusMessage.value = "Checking if you got 1..."
val grades = AppContext.client.value.grades()
Database.insertGrades(grades)

AppContext.statusMessage.value = "What class are you in?"
val classInfo = AppContext.client.value.classInfo()
Database.setClassInfo(classInfo)

AppContext.statusMessage.value = "What is today's lucky number?"
val luckyNumber = AppContext.client.value.luckyNumber()
Database.setLuckyNumber(luckyNumber)

AppContext.statusMessage.value = "Let's check the timetable!"
val timetable = AppContext.client.value.timetable()
Database.insertLessons(timetable)

AppContext.statusMessage.value = "Have you been at school?"
val attendances = AppContext.client.value.attendance()
Database.insertAttendances(attendances)

AppContext.statusMessage.value = "Saving some data and refreshing the view..."
AppContext.semester.value = classInfo.semester
AppContext.databaseUpdated.value = !AppContext.databaseUpdated.value

AppContext.statusMessage.value = null

return true
} catch (e: Exception) {
showAlert("Fetching data error", e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.aeoliux.violet.app

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -31,10 +35,13 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.github.aeoliux.violet.AppContext
import com.github.aeoliux.violet.Keychain
import com.github.aeoliux.violet.app.attendance.AttendanceView
import com.github.aeoliux.violet.app.grades.GradesView
import com.github.aeoliux.violet.app.home.HomeView
import com.github.aeoliux.violet.app.login.LoginView
import com.github.aeoliux.violet.app.timetable.TimetableView
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -64,33 +71,54 @@ fun MainView(keychain: Keychain) {

Scaffold(
topBar = { TopAppBar({
Text(text = tabs[selectedTabItem].text, fontWeight = FontWeight.Bold)
Column {
Text(
text = if (AppContext.isLoggedIn.value)
tabs[selectedTabItem].text
else
"Log in to S***rgia",
fontWeight = FontWeight.Bold
)

AnimatedVisibility(
AppContext.statusMessage.value != null
) {
Text(
modifier = Modifier.padding(top = 2.dp),
text = AppContext.statusMessage.value?: "",
fontSize = 13.sp
)
}
}
}) },
bottomBar = {
BottomNavigation(
Modifier
.fillMaxWidth()
.wrapContentHeight()
) {
tabs.forEachIndexed { i, it ->
BottomNavigationItem(
modifier = Modifier
.height(50.dp)
.fillMaxWidth(),
selected = selectedTabItem == i,
onClick = { selectedTabItem = i },
icon = {
Text(
text = it.text,
fontWeight = FontWeight.Bold
)
}
)
if (AppContext.isLoggedIn.value) {
tabs.forEachIndexed { i, it ->
BottomNavigationItem(
modifier = Modifier
.height(50.dp)
.fillMaxWidth(),
selected = selectedTabItem == i,
onClick = { selectedTabItem = i },
icon = {
Text(
text = it.text,
fontWeight = FontWeight.Bold
)
}
)
}
}
}
}
) {
Box(Modifier.fillMaxSize().pullRefresh(refreshState)) {
if (AppContext.isLoggedIn.value) {
Box(Modifier.fillMaxSize().pullRefresh(refreshState)) {
// LazyColumn(
// modifier = Modifier
// .fillMaxWidth()
Expand All @@ -103,22 +131,31 @@ fun MainView(keychain: Keychain) {
// }
// }

Column(
modifier = Modifier
.fillMaxSize()
.padding(10.dp)
.padding(bottom = 55.dp)
.verticalScroll(scrollState),
Column(
modifier = Modifier
.fillMaxSize()
.padding(10.dp)
.padding(bottom = 55.dp)
.verticalScroll(scrollState),

) {
Column(Modifier.padding(2.dp).fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally) {
tabs[selectedTabItem].destination()
) {
Column(
Modifier.padding(2.dp).fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
tabs[selectedTabItem].destination()
}
}
}

PullRefreshIndicator(isRefreshing, refreshState, Modifier.align(Alignment.TopCenter))
PullRefreshIndicator(
isRefreshing,
refreshState,
Modifier.align(Alignment.TopCenter)
)
}
} else {
LoginView(keychain)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.aeoliux.violet.app.attendance

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.github.aeoliux.violet.api.toColorLong
import com.github.aeoliux.violet.api.types.Attendance
import com.github.aeoliux.violet.app.components.ExpandableList
import kotlinx.datetime.LocalDate

@Composable
fun AttendanceListView(
attendance: LinkedHashMap<LocalDate, LinkedHashMap<UInt, Attendance>>
) {
attendance.forEach { (date, attendances) ->
Card(Modifier.fillMaxWidth().wrapContentHeight().padding(10.dp)) {
ExpandableList(
header = {
Text(date.toString())
}
) {
attendances.forEach { (lessonNo, attendanceInfo) ->
Row(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(10.dp),
verticalAlignment = Alignment.CenterVertically
) {
Column(
modifier = Modifier
.padding(end = 10.dp)
.background(attendanceInfo.color.toColorLong())
.size(50.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = attendanceInfo.typeShort)
}
Column(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
) {
Text("At ${attendanceInfo.addDate} ${attendanceInfo.addedBy} added ${attendanceInfo.type} on lesson no. ${lessonNo}")
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.github.aeoliux.violet.app.attendance

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.ScrollableTabRow
import androidx.compose.material.Tab
import androidx.compose.material.TabRow
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -20,15 +18,10 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.github.aeoliux.violet.AppContext
import com.github.aeoliux.violet.Keychain
import com.github.aeoliux.violet.api.types.Attendance
import com.github.aeoliux.violet.api.types.max
import com.github.aeoliux.violet.api.types.min
import com.github.aeoliux.violet.app.TabItem
import com.github.aeoliux.violet.app.logIn
import com.github.aeoliux.violet.storage.Database
import com.github.aeoliux.violet.storage.selectAttendances
import kotlinx.datetime.LocalDate
Expand All @@ -42,6 +35,7 @@ fun AttendanceView() {
}
var selectedView by remember { mutableStateOf(-1) }
val views = listOf(
TabItem("List") { AttendanceListView(attendance) },
TabItem("Table") { AttendanceTableView(attendance) }
)

Expand All @@ -51,17 +45,31 @@ fun AttendanceView() {
}

if (selectedView != -1) {
ScrollableTabRow(
TabRow(
selectedTabIndex = selectedView,
backgroundColor = Color.White,
contentColor = Color.Black
contentColor = Color.Black,
// indicator = { positions ->
// TabRowDefaults.Indicator(
// modifier = Modifier
// .tabIndicatorOffset(positions[selectedView])
// .fillMaxWidth()
// )
// }
) {
views.forEachIndexed { index, it ->
Tab(
modifier = Modifier.fillMaxWidth(),
selected = selectedView == index,
onClick = { selectedView = index }
) {
Text(modifier = Modifier.padding(16.dp), text = it.text)
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Text(modifier = Modifier.padding(16.dp), text = it.text)
}
}
}
}
Expand Down
Loading

0 comments on commit 34c2dec

Please sign in to comment.