Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Compose at top screen #407

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ fun Calendar.getTodayStringOnlyFigure(): String {

fun Calendar.getTodayString(pattern: String): String {
return DateFormat.format(pattern, this).toString()
}

fun Date.format(format: String): String {
return DateFormat.format(format, this).toString()
}
40 changes: 40 additions & 0 deletions app/src/main/kotlin/com/numero/sojodia/ui/board/BusBoardUiState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.numero.sojodia.ui.board

import com.numero.sojodia.model.BusTime
import com.numero.sojodia.model.Route
import java.util.*

data class BusBoardUiState(
val tkBusBoardSchedule: BusBoardSchedule,
val tndBusBoardSchedule: BusBoardSchedule,
val currentDate: Date
) {
companion object {
fun empty() = BusBoardUiState(
tkBusBoardSchedule = BusBoardSchedule(
route = Route.TK,
nearBusTime = null,
nextBusTime = null,
canPrevBusTime = false,
canNextBusTime = false
),
tndBusBoardSchedule = BusBoardSchedule(
route = Route.TND,
nearBusTime = null,
nextBusTime = null,
canPrevBusTime = false,
canNextBusTime = false
),
currentDate = Calendar.getInstance().time
)
}
}

data class BusBoardSchedule(
val route: Route,
val nearBusTime: BusTime?,
val nextBusTime: BusTime?,
val canPrevBusTime: Boolean,
val canNextBusTime: Boolean,
// TODO count time
)
114 changes: 114 additions & 0 deletions app/src/main/kotlin/com/numero/sojodia/ui/board/BusBoardViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.numero.sojodia.ui.board

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.numero.sojodia.model.*
import com.numero.sojodia.repository.BusDataRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class BusBoardViewModel : ViewModel() {

private val _uiState = MutableStateFlow(BusBoardUiState.empty())
val uiState: StateFlow<BusBoardUiState> = _uiState

private var week = Week.getCurrentWeek()

fun fetchBusData(
busDataRepository: BusDataRepository,
reciprocate: Reciprocate
) {
viewModelScope.launch {
try {
withContext(Dispatchers.Main) {
val current = Time()
val busData = busDataRepository.getBusData()
val tkTimeList = busData.filterTkList(reciprocate, week)
val tndTimeList = busData.filterTndList(reciprocate, week)
val nearTkBusTime = tkTimeList.findNearBusTime(current)
val nearTndBusTime = tndTimeList.findNearBusTime(current)
val nextTkBusTime = tkTimeList.findNextBusTime(current)
val nextTndBusTime = tndTimeList.findNextBusTime(current)

_uiState.value = uiState.value.copy(
tkBusBoardSchedule = BusBoardSchedule(
route = Route.TK,
nearBusTime = nearTkBusTime,
nextBusTime = nextTkBusTime,
canPrevBusTime = true,
canNextBusTime = true
),
tndBusBoardSchedule = BusBoardSchedule(
route = Route.TND,
nearBusTime = nearTndBusTime,
nextBusTime = nextTndBusTime,
canPrevBusTime = true,
canNextBusTime = true
)
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}

fun prevTkBusTime() {
}

fun nextTkBusTime() {
// FIXME example
val tkBusSchedule = uiState.value.tkBusBoardSchedule
_uiState.value = uiState.value.copy(
tkBusBoardSchedule = tkBusSchedule.copy(
nearBusTime = tkBusSchedule.nextBusTime,
nextBusTime = null,
canPrevBusTime = false,
canNextBusTime = false
)
)
}

fun prevTndBusTime() {
}

fun nextTndBusTime() {
}

private fun BusData.filterTkList(
reciprocate: Reciprocate,
week: Week
): TkBusTimeList {
val list = when (reciprocate) {
Reciprocate.GOING -> tkBusTimeListGoing
.asSequence()
.filter { it.week == week }
.toList()
Reciprocate.RETURN -> tkBusTimeListReturn
.asSequence()
.filter { it.week == week }
.toList()
}
return TkBusTimeList(list)
}

private fun BusData.filterTndList(
reciprocate: Reciprocate,
week: Week
): TndBusTimeList {
val list = when (reciprocate) {
Reciprocate.GOING -> tndBusTimeListGoing
.asSequence()
.filter { busTime -> busTime.week == week }
.toList()
Reciprocate.RETURN -> tndBusTimeListReturn
.asSequence()
.filter { busTime -> busTime.week == week }
.toList()
}
return TndBusTimeList(list)
}
}
Loading