Skip to content

Commit

Permalink
fix store login
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiebe committed Jan 30, 2024
1 parent da552f6 commit d20ecaf
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import nl.tiebe.otarium.logic.root.home.children.timetable.TimetableRootComponen
import nl.tiebe.otarium.ui.home.grades.DefaultGradesComponent
import nl.tiebe.otarium.ui.home.grades.GradesComponent

open class DefaultHomeComponent(componentContext: ComponentContext, override val navigateRootComponent: (RootComponent.ChildScreen) -> Unit): HomeComponent, ComponentContext by componentContext {
class DefaultHomeComponent(componentContext: ComponentContext, override val navigateRootComponent: (RootComponent.ChildScreen) -> Unit): HomeComponent, ComponentContext by componentContext {
private val dialogNavigation = SlotNavigation<MenuItems>()

override val visibleItems: List<MenuItems> = listOf(
Expand Down Expand Up @@ -53,33 +53,33 @@ open class DefaultHomeComponent(componentContext: ComponentContext, override val
}
}

open fun timetableComponent(componentContext: ComponentContext): TimetableRootComponent =
private fun timetableComponent(componentContext: ComponentContext): TimetableRootComponent =
DefaultTimetableRootComponent(
componentContext = componentContext,
)

open fun gradesComponent(componentContext: ComponentContext): GradesComponent =
private fun gradesComponent(componentContext: ComponentContext): GradesComponent =
DefaultGradesComponent(
componentContext = componentContext
)

open fun messagesComponent(componentContext: ComponentContext): MessagesComponent =
private fun messagesComponent(componentContext: ComponentContext): MessagesComponent =
DefaultMessagesComponent(
componentContext = componentContext
)

open fun eloComponent(componentContext: ComponentContext): ELOComponent =
private fun eloComponent(componentContext: ComponentContext): ELOComponent =
DefaultELOComponent(
componentContext = componentContext
)

open fun settingsComponent(componentContext: ComponentContext): SettingsComponent =
private fun settingsComponent(componentContext: ComponentContext): SettingsComponent =
DefaultSettingsComponent(
componentContext = componentContext,
navigateRootComponent = navigateRootComponent
)

open fun debugComponent(componentContext: ComponentContext): DebugComponent =
private fun debugComponent(componentContext: ComponentContext): DebugComponent =
DefaultDebugComponent(
componentContext = componentContext,
navigateRootComponent = navigateRootComponent
Expand Down
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()
}
}
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
}

}
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)
}
}
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()
}

}
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)
}
}
}

}
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
)
}
}
Loading

0 comments on commit d20ecaf

Please sign in to comment.