-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/api/bodys/SchoolNotices.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.aeoliux.violet.api.bodys | ||
|
||
import com.github.aeoliux.violet.api.localDateTimeFormat | ||
import com.github.aeoliux.violet.api.types.User | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.LocalDateTime | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toInstant | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SchoolNotice( | ||
val Id: String, | ||
val StartDate: String, | ||
val EndDate: String, | ||
val Subject: String, | ||
val Content: String, | ||
val AddedBy: IdAndUrl, | ||
val CreationDate: String, | ||
val WasRead: Boolean | ||
) | ||
|
||
@Serializable | ||
data class SchoolNotices(val SchoolNotices: List<SchoolNotice>) { | ||
fun toSNList( | ||
users: LinkedHashMap<UInt, User> | ||
): List<com.github.aeoliux.violet.api.types.SchoolNotice> { | ||
return SchoolNotices.fold<SchoolNotice, List<com.github.aeoliux.violet.api.types.SchoolNotice>>(emptyList()) { acc, cur -> | ||
acc.plus( | ||
com.github.aeoliux.violet.api.types.SchoolNotice( | ||
startDate = LocalDate.parse(cur.StartDate), | ||
endDate = LocalDate.parse(cur.EndDate), | ||
subject = cur.Subject, | ||
content = cur.Content, | ||
addedBy = users[cur.AddedBy.Id]?.teacher()?: "", | ||
createdAt = LocalDateTime.parse(cur.CreationDate, localDateTimeFormat) | ||
) | ||
) | ||
}.sortedWith { a, b -> | ||
(b.createdAt.toInstant(TimeZone.currentSystemDefault()).epochSeconds - a.createdAt.toInstant( | ||
TimeZone.currentSystemDefault() | ||
).epochSeconds).toInt() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...pp/src/commonMain/kotlin/com/github/aeoliux/violet/app/schoolNotices/SchoolNoticesView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.github.aeoliux.violet.app.schoolNotices | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import com.github.aeoliux.violet.app.appState.LocalAppState | ||
import com.github.aeoliux.violet.app.components.ExpandableList | ||
import com.github.aeoliux.violet.app.storage.Database | ||
import com.github.aeoliux.violet.app.storage.selectSchoolNotice | ||
|
||
@Composable | ||
fun SchoolNoticesView(vm: SchoolNoticesViewModel = viewModel { SchoolNoticesViewModel() }) { | ||
val appState = LocalAppState.current | ||
val listOfSchoolNotices by vm.listOfSchoolNotices.collectAsState() | ||
|
||
LaunchedEffect(appState.databaseUpdated) { | ||
vm.launchedEffect() | ||
} | ||
|
||
listOfSchoolNotices.forEach { | ||
Card( | ||
Modifier | ||
.fillMaxWidth() | ||
.wrapContentHeight() | ||
.padding(2.dp) | ||
) { | ||
ExpandableList( | ||
header = { | ||
Column(Modifier.padding(10.dp)) { | ||
Text( | ||
fontWeight = FontWeight.Bold, | ||
fontSize = 20.sp, | ||
text = it.subject | ||
) | ||
Text( | ||
fontSize = 15.sp, | ||
text = "Added at ${it.createdAt} by ${it.addedBy}" | ||
) | ||
} | ||
} | ||
) { | ||
val schoolNotice = Database.selectSchoolNotice(it.id)!! | ||
|
||
Column(Modifier.padding(10.dp)) { | ||
Text("Starts at: ${schoolNotice.startDate}") | ||
Text("Ends at: ${schoolNotice.endDate}\n\n") | ||
|
||
Text( | ||
modifier = Modifier.fillMaxWidth(), | ||
textAlign = TextAlign.Justify, | ||
text = schoolNotice.content | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...c/commonMain/kotlin/com/github/aeoliux/violet/app/schoolNotices/SchoolNoticesViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.aeoliux.violet.app.schoolNotices | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.github.aeoliux.violet.api.types.SchoolNotice | ||
import com.github.aeoliux.violet.app.storage.Database | ||
import com.github.aeoliux.violet.app.storage.selectListOfSchoolNotices | ||
import com.github.aeoliux.violet.app.storage.selectSchoolNotice | ||
import comgithubaeoliuxvioletstorage.SelectListOfSchoolNotices | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
class SchoolNoticesViewModel: ViewModel() { | ||
private var _listOfSchoolNotices: MutableStateFlow<List<SelectListOfSchoolNotices>> = MutableStateFlow(emptyList()) | ||
val listOfSchoolNotices get() = _listOfSchoolNotices.asStateFlow() | ||
|
||
fun launchedEffect() { | ||
viewModelScope.launch { | ||
_listOfSchoolNotices.update { Database.selectListOfSchoolNotices()?: emptyList() } | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/app/storage/SchoolNotices.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.aeoliux.violet.app.storage | ||
|
||
import com.github.aeoliux.violet.api.types.SchoolNotice | ||
import comgithubaeoliuxvioletstorage.SelectListOfSchoolNotices | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.LocalDateTime | ||
|
||
|
||
fun Database.selectListOfSchoolNotices(): List<SelectListOfSchoolNotices>? { | ||
try { | ||
val result = dbQuery.selectListOfSchoolNotices().executeAsList() | ||
return result | ||
} catch (_: NullPointerException) { | ||
return null | ||
} | ||
} | ||
|
||
fun Database.selectSchoolNotice(id: Long): SchoolNotice? { | ||
val result = dbQuery.selectSchoolNotice(id).executeAsOneOrNull() | ||
|
||
return if (result != null) | ||
SchoolNotice( | ||
startDate = LocalDate.parse(result.startDate), | ||
endDate = LocalDate.parse(result.endDate), | ||
subject = result.subject, | ||
content = result.content, | ||
addedBy = result.addedBy, | ||
createdAt = LocalDateTime.parse(result.createdAt) | ||
) | ||
else | ||
null | ||
} | ||
|
||
fun Database.insertSchoolNotices(sn: List<SchoolNotice>) { | ||
dbQuery.transaction { | ||
dbQuery.clearSchoolNotices() | ||
|
||
sn.forEach { sn -> | ||
dbQuery.insertSchoolNotices( | ||
startDate = sn.startDate.toString(), | ||
endDate = sn.endDate.toString(), | ||
subject = sn.subject, | ||
content = sn.content, | ||
addedBy = sn.addedBy, | ||
createdAt = sn.createdAt.toString() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters