-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
302 additions
and
24 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
79 changes: 79 additions & 0 deletions
79
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreAveragesComponent.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,79 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.router.stack.ChildStack | ||
import com.arkivanov.decompose.router.stack.StackNavigation | ||
import com.arkivanov.decompose.router.stack.childStack | ||
import com.arkivanov.decompose.router.stack.pop | ||
import com.arkivanov.decompose.value.MutableValue | ||
import com.arkivanov.decompose.value.Value | ||
import com.arkivanov.essenty.backhandler.BackHandlerOwner | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import nl.tiebe.otarium.Data | ||
import nl.tiebe.otarium.logic.default.componentCoroutineScope | ||
import nl.tiebe.otarium.logic.root.home.children.averages.AveragesComponent | ||
import nl.tiebe.otarium.magister.GradeWithGradeInfo | ||
import nl.tiebe.otarium.magister.ManualGrade | ||
|
||
class StoreAveragesComponent(componentContext: ComponentContext): AveragesComponent, ComponentContext by componentContext, | ||
BackHandlerOwner { | ||
override val refreshState: MutableValue<Boolean> = MutableValue(false) | ||
|
||
override fun refreshGrades() { | ||
scope.launch { | ||
refreshState.value = true | ||
delay(500L) | ||
refreshState.value = false | ||
} | ||
} | ||
|
||
override val navigation = StackNavigation<AveragesComponent.Config>() | ||
|
||
override val childStack: Value<ChildStack<AveragesComponent.Config, AveragesComponent.Child>> = | ||
childStack( | ||
source = navigation, | ||
serializer = AveragesComponent.Config.serializer(), | ||
initialConfiguration = AveragesComponent.Config.List, | ||
handleBackButton = false, // Pop the back stack on back button press | ||
childFactory = ::createChild, | ||
) | ||
|
||
private fun createChild(config: AveragesComponent.Config, componentContext: ComponentContext): AveragesComponent.Child = | ||
when (config) { | ||
is AveragesComponent.Config.List -> AveragesComponent.Child.ListChild(this) | ||
is AveragesComponent.Config.Subject -> AveragesComponent.Child.SubjectChild(this, config.subjectId) | ||
} | ||
|
||
|
||
override val gradesList: MutableValue<List<GradeWithGradeInfo>> = MutableValue( | ||
emptyList() | ||
) | ||
override val cardList = MutableValue(Data.cardList) | ||
|
||
override val manualGradesList: MutableValue<List<ManualGrade>> = MutableValue(Data.manualGrades) | ||
override val addManualGradePopupOpen: MutableValue<Boolean> = MutableValue(false) | ||
override fun addManualGrade(manualGrade: ManualGrade) { | ||
manualGradesList.value = manualGradesList.value.toMutableList().apply { | ||
add(manualGrade) | ||
} | ||
Data.manualGrades = manualGradesList.value | ||
} | ||
|
||
override fun removeManualGrade(manualGrade: ManualGrade) { | ||
manualGradesList.value = manualGradesList.value.toMutableList().apply { | ||
remove(manualGrade) | ||
} | ||
Data.manualGrades = manualGradesList.value | ||
} | ||
|
||
override fun back() { | ||
navigation.pop() | ||
} | ||
|
||
private val scope = componentCoroutineScope() | ||
|
||
init { | ||
refreshGrades() | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreGradeComponent.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,31 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.value.MutableValue | ||
import nl.tiebe.otarium.logic.default.home.children.averages.DefaultAveragesComponent | ||
import nl.tiebe.otarium.logic.default.home.children.grades.DefaultRecentGradesComponent | ||
import nl.tiebe.otarium.logic.root.home.children.averages.AveragesComponent | ||
import nl.tiebe.otarium.logic.root.home.children.grades.RecentGradesComponent | ||
import nl.tiebe.otarium.ui.home.grades.GradesComponent | ||
|
||
class StoreGradeComponent(componentContext: ComponentContext): GradesComponent, ComponentContext by componentContext { | ||
private fun recentGradesComponent(componentContext: ComponentContext) = | ||
StoreRecentGradesComponent( | ||
componentContext = componentContext | ||
) | ||
|
||
private fun averagesComponent(componentContext: ComponentContext) = | ||
StoreAveragesComponent( | ||
componentContext = componentContext | ||
) | ||
|
||
override val recentGradeComponent: RecentGradesComponent = recentGradesComponent(componentContext) | ||
override val averagesComponent: AveragesComponent = averagesComponent(componentContext) | ||
|
||
override val currentPage: MutableValue<Int> = MutableValue(0) | ||
|
||
override fun changeChild(gradesChild: GradesComponent.GradesChild) { | ||
currentPage.value = gradesChild.id | ||
} | ||
|
||
} |
47 changes: 32 additions & 15 deletions
47
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreHomeComponent.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 |
---|---|---|
@@ -1,33 +1,50 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import nl.tiebe.otarium.logic.default.home.DefaultHomeComponent | ||
import com.arkivanov.decompose.router.slot.ChildSlot | ||
import com.arkivanov.decompose.router.slot.SlotNavigation | ||
import com.arkivanov.decompose.router.slot.activate | ||
import com.arkivanov.decompose.router.slot.childSlot | ||
import com.arkivanov.decompose.value.Value | ||
import nl.tiebe.otarium.logic.root.RootComponent | ||
import nl.tiebe.otarium.logic.root.home.HomeComponent | ||
import nl.tiebe.otarium.logic.root.home.MenuItems | ||
import nl.tiebe.otarium.logic.root.home.children.timetable.TimetableRootComponent | ||
import nl.tiebe.otarium.ui.home.grades.GradesComponent | ||
|
||
class StoreHomeComponent(componentContext: ComponentContext, override val navigateRootComponent: (RootComponent.ChildScreen) -> Unit): HomeComponent, ComponentContext by componentContext { | ||
private val dialogNavigation = SlotNavigation<MenuItems>() | ||
|
||
class StoreHomeComponent(componentContext: ComponentContext, override val navigateRootComponent: (RootComponent.ChildScreen) -> Unit | ||
): DefaultHomeComponent(componentContext, navigateRootComponent) { | ||
override val visibleItems: List<MenuItems> = listOf( | ||
MenuItems.Timetable, | ||
MenuItems.Grades, | ||
MenuItems.Settings | ||
MenuItems.Grades | ||
) | ||
|
||
/* | ||
override fun timetableComponent(componentContext: ComponentContext) = | ||
StoreTimetableComponent( | ||
override val dialog: Value<ChildSlot<MenuItems, HomeComponent.MenuItemComponent>> = childSlot<MenuItems, HomeComponent.MenuItemComponent>( | ||
dialogNavigation, | ||
"HomeComponentChildOverlay", | ||
{ MenuItems.Timetable }, | ||
persistent = true, | ||
handleBackButton = false | ||
) { config, componentContext -> | ||
when (config) { | ||
is MenuItems.Timetable -> timetableComponent(componentContext) | ||
is MenuItems.Grades -> gradesComponent(componentContext) | ||
else -> throw RuntimeException() | ||
} | ||
} | ||
|
||
private fun timetableComponent(componentContext: ComponentContext): TimetableRootComponent = | ||
StoreTimetableRootComponent( | ||
componentContext = componentContext, | ||
) | ||
|
||
override fun gradesComponent(componentContext: ComponentContext) = | ||
private fun gradesComponent(componentContext: ComponentContext): GradesComponent = | ||
StoreGradeComponent( | ||
componentContext = componentContext | ||
) | ||
|
||
override fun settingsComponent(componentContext: ComponentContext) = | ||
StoreSettingsComponent( | ||
componentContext = componentContext, | ||
navigateRootComponent = navigateRootComponent | ||
)*/ | ||
override fun navigate(item: MenuItems) { | ||
dialogNavigation.activate(item) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreRecentGradesComponent.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,52 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.value.MutableValue | ||
import dev.tiebe.magisterapi.response.general.year.grades.RecentGrade | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import nl.tiebe.otarium.Data | ||
import nl.tiebe.otarium.logic.default.componentCoroutineScope | ||
import nl.tiebe.otarium.logic.root.home.children.grades.RecentGradesComponent | ||
import nl.tiebe.otarium.utils.calculateAverageGrade | ||
|
||
class StoreRecentGradesComponent(componentContext: ComponentContext) : RecentGradesComponent, ComponentContext by componentContext { | ||
override val refreshState: MutableValue<Boolean> = MutableValue(false) | ||
private val scope = componentCoroutineScope() | ||
|
||
override val grades: MutableValue<List<RecentGrade>> = MutableValue(emptyList()) | ||
|
||
override fun refreshGrades() { | ||
scope.launch { | ||
refreshState.value = true | ||
delay(1000L) | ||
refreshState.value = false | ||
} | ||
} | ||
|
||
|
||
override fun loadNextGrades() { | ||
scope.launch { | ||
refreshState.value = true | ||
delay(700L) | ||
refreshState.value = false | ||
} | ||
} | ||
|
||
override fun calculateAverageBeforeAfter(grade: RecentGrade): Pair<Float, Float> { | ||
val grades = Data.selectedAccount.fullGradeList.filter { it.grade.subject.abbreviation == grade.subject.code }.sortedBy { it.grade.dateEntered } | ||
|
||
val index = grades.find { it.grade.gradeColumn.id == grade.gradeColumnId }?.let { grades.indexOf(it) } ?: return 0f to 0f | ||
|
||
val before = calculateAverageGrade(grades.subList(0, index)) | ||
val after = calculateAverageGrade(grades.subList(0, index + 1)) | ||
|
||
return before to after | ||
} | ||
|
||
|
||
init { | ||
refreshGrades() | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreTimetableComponent.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,66 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.pager.PagerState | ||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.value.MutableValue | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.* | ||
import nl.tiebe.otarium.logic.default.componentCoroutineScope | ||
import nl.tiebe.otarium.logic.default.home.children.timetable.children.timetable.days | ||
import nl.tiebe.otarium.logic.root.home.children.timetable.TimetableRootComponent | ||
import nl.tiebe.otarium.logic.root.home.children.timetable.children.timetable.TimetableComponent | ||
import nl.tiebe.otarium.magister.AgendaItemWithAbsence | ||
import kotlin.math.floor | ||
|
||
class StoreTimetableComponent(componentContext: ComponentContext, override val parentComponent: TimetableRootComponent): TimetableComponent, ComponentContext by componentContext { | ||
override val now: MutableValue<LocalDateTime> = MutableValue(Clock.System.now().toLocalDateTime(TimeZone.of("Europe/Amsterdam"))) | ||
override val currentPage = MutableValue(500 + now.value.date.dayOfWeek.ordinal) | ||
|
||
override val timetable: MutableValue<List<AgendaItemWithAbsence>> = MutableValue(emptyList()) | ||
|
||
override val selectedWeek = MutableValue(floor((currentPage.value - (amountOfDays / 2).toFloat()) / days.size).toInt()) | ||
override val selectedDay = MutableValue(currentPage.value - (amountOfDays / 2) % 7) | ||
|
||
override val selectedWeekIndex = MutableValue(selectedWeek.value + amountOfWeeks / 2) | ||
|
||
override val isRefreshingTimetable = MutableValue(false) | ||
|
||
private val scope = componentCoroutineScope() | ||
|
||
override fun refreshTimetable(from: LocalDate, to: LocalDate) { | ||
scope.launch { | ||
isRefreshingTimetable.value = true | ||
|
||
delay(1000L) | ||
|
||
isRefreshingTimetable.value = false | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalFoundationApi::class) | ||
override fun scrollToPage(coroutineScope: CoroutineScope, page: Int, pagerState: PagerState) { | ||
coroutineScope.launch { | ||
pagerState.animateScrollToPage(page) | ||
} | ||
currentPage.value = page | ||
} | ||
|
||
|
||
init { | ||
selectedWeek.observe { | ||
refreshSelectedWeek() | ||
} | ||
|
||
scope.launch { | ||
while (true) { | ||
now.value = Clock.System.now().toLocalDateTime(TimeZone.of("Europe/Amsterdam")) | ||
|
||
delay(60_000) | ||
} | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
shared/src/commonMain/kotlin/nl/tiebe/otarium/logic/store/StoreTimetableRootComponent.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,34 @@ | ||
package nl.tiebe.otarium.logic.store | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.router.stack.ChildStack | ||
import com.arkivanov.decompose.router.stack.StackNavigation | ||
import com.arkivanov.decompose.router.stack.childStack | ||
import com.arkivanov.decompose.value.Value | ||
import com.arkivanov.essenty.backhandler.BackHandlerOwner | ||
import nl.tiebe.otarium.logic.root.home.children.timetable.TimetableRootComponent | ||
import nl.tiebe.otarium.logic.root.home.children.timetable.children.timetable.TimetableComponent | ||
|
||
class StoreTimetableRootComponent(componentContext: ComponentContext): TimetableRootComponent, ComponentContext by componentContext, BackHandlerOwner { | ||
override val navigation = StackNavigation<TimetableRootComponent.Config>() | ||
|
||
override val timetableComponent: TimetableComponent = StoreTimetableComponent(componentContext, this) | ||
|
||
override val childStack: Value<ChildStack<TimetableRootComponent.Config, TimetableRootComponent.Child>> = | ||
childStack( | ||
source = navigation, | ||
serializer = TimetableRootComponent.Config.serializer(), | ||
initialConfiguration = TimetableRootComponent.Config.Main, | ||
handleBackButton = false, // Pop the back stack on back button press | ||
childFactory = ::createChild, | ||
) | ||
|
||
private fun createChild(config: TimetableRootComponent.Config, @Suppress("UNUSED_PARAMETER") componentContext: ComponentContext): TimetableRootComponent.Child = | ||
when (config) { | ||
is TimetableRootComponent.Config.Main -> TimetableRootComponent.Child.TimetableChild(timetableComponent) | ||
is TimetableRootComponent.Config.TimetablePopup -> TimetableRootComponent.Child.TimetablePopupChild( | ||
timetableComponent, | ||
config.item | ||
) | ||
} | ||
} |
Oops, something went wrong.