Skip to content

Commit

Permalink
feat: Added testing Kocourkov menza
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Jul 3, 2024
1 parent 9eecc45 commit db21b8f
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Strahov
import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Subsystem
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FEL
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FS
import cz.lastaapps.api.core.domain.model.MenzaType.Testing.Kocourkov
import cz.lastaapps.api.core.domain.repo.InfoRepo
import cz.lastaapps.api.core.domain.repo.TodayDishRepo
import cz.lastaapps.api.core.domain.repo.WeekDishRepo
Expand Down Expand Up @@ -65,6 +66,7 @@ val apiCoreModule = module {
is Subsystem -> createScope<Subsystem>()
FEL -> createScope<FEL>()
FS -> createScope<FS>()
Kocourkov -> createScope<Kocourkov>()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand Down Expand Up @@ -31,6 +31,7 @@ sealed interface MenzaType {
named<Agata.Subsystem>(),
named<Buffet.FS>(),
named<Buffet.FEL>(),
named<Testing.Kocourkov>(),
)
}

Expand All @@ -53,4 +54,10 @@ sealed interface MenzaType {
override val id: String = "buffet_fel"
}
}

sealed interface Testing : MenzaType {
data object Kocourkov : Testing {
override val id: String = "kocourkov"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
* Menza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Menza is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Menza. If not, see <https://www.gnu.org/licenses/>.
*/

package cz.lastaapps.api.main.data

import arrow.core.right
import cz.lastaapps.api.core.domain.model.Dish
import cz.lastaapps.api.core.domain.model.DishCategory
import cz.lastaapps.api.core.domain.model.Info
import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.api.core.domain.model.MenzaType
import cz.lastaapps.api.core.domain.model.ServingPlace
import cz.lastaapps.api.core.domain.model.WeekDayDish
import cz.lastaapps.api.core.domain.repo.InfoRepo
import cz.lastaapps.api.core.domain.repo.InfoRepoParams
import cz.lastaapps.api.core.domain.repo.MenzaRepo
import cz.lastaapps.api.core.domain.repo.MenzaRepoParams
import cz.lastaapps.api.core.domain.repo.TodayDishRepo
import cz.lastaapps.api.core.domain.repo.TodayRepoParams
import cz.lastaapps.api.core.domain.repo.WeekDishRepo
import cz.lastaapps.api.core.domain.repo.WeekRepoParams
import cz.lastaapps.api.core.domain.sync.SyncOutcome
import cz.lastaapps.api.core.domain.sync.SyncResult
import cz.lastaapps.api.core.domain.sync.SyncResult.Unavailable
import cz.lastaapps.core.util.extensions.localLogger
import kotlin.random.Random
import kotlin.random.nextInt
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import org.koin.core.component.KoinComponent

internal class KocourkovRepoImpl : MenzaRepo {
override fun isReady(params: MenzaRepoParams): Flow<Boolean> = flow {
emit(true)
}

override fun getData(params: MenzaRepoParams): Flow<ImmutableList<Menza>> = flow {
emit(
persistentListOf(
Menza(
MenzaType.Testing.Kocourkov,
name = "Kocourkov",
isOpened = true,
supportsDaily = true,
supportsWeekly = false,
isExperimental = false,
videoLinks = persistentListOf(
"https://pointerpointer.com/images/${
Random.nextInt(
1..999,
)
}.jpg",
),
),
),
)
}

override suspend fun sync(params: MenzaRepoParams, isForced: Boolean): SyncOutcome {
delay(Random.nextInt(100..1000).milliseconds)
return SyncResult.Skipped.right()
}
}

internal object TodayKocourkovDishRepoImpl : TodayDishRepo {

private val dishLists = listOf(
persistentListOf(
DishCategory(
"Polévky",
"Patoky všeho druhu",
persistentListOf(
Dish(
amount = null,
name = "Sekerková",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = null,
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
Dish(
amount = null,
name = "Ježečková",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = "https://aa.ecn.cz/img_upload/e6ffb6c50bc1424ab10ecf09e063cd63/jezek.jpg",
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
),
),
DishCategory(
"Chlálky",
"Dlabance dne",
persistentListOf(
Dish(
amount = null,
name = "Tuxík na přírodno",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/800px-Tux.svg.png",
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
Dish(
amount = "1 kg",
name = "Strahovská kunda",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = "https://www.bydlimekvalitne.cz/sites/default/files/styles/image_840x400/public/bigstock-czech-smelly-cheese-olomouck-86998046.jpg?itok=WzhMUZt2&c=291a4bb2386048f3eb113e3ec11ccfb2",
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
Dish(
amount = "200 g",
name = "Štěkanátky z maxipsa Fíka",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = "https://www.czecot.cz/results/zobrobr.php?w=ac&id=201171&orig=1",
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
),
),
DishCategory(
"Dezerty",
"Sekce pro tlustá prasátka",
persistentListOf(
Dish(
amount = null,
name = "Krtkův dort",
priceDiscounted = 42.0f,
priceNormal = 69.0f,
allergens = persistentListOf(1, 2, 3),
photoLink = "https://1gr.cz/fotky/idnes/22/121/r7/JVE86eb72_32738_102382307.jpg",
pictogram = persistentListOf(),
servingPlaces = persistentListOf(
ServingPlace("Radnice", "R"),
),
ingredients = persistentListOf(),
isActive = true,
),
),
),
),
persistentListOf(),
)

private val localNotifier = MutableStateFlow(0)

override fun getData(params: TodayRepoParams): Flow<ImmutableList<DishCategory>> =
localNotifier.map {
delay(Random.nextInt(100..500).milliseconds)
dishLists[it % dishLists.size]
}

override suspend fun sync(params: TodayRepoParams, isForced: Boolean): SyncOutcome {
delay(Random.nextInt(100..1000).milliseconds)
return if (Random.nextInt(0..3) != 0) {
localNotifier.value += 1
SyncResult.Updated.right()
} else {
SyncResult.Skipped.right()
}
}
}

internal object InfoKocourkovRepoImpl : InfoRepo, KoinComponent {

override fun getData(params: InfoRepoParams): Flow<Info> = flow { }

override suspend fun sync(params: InfoRepoParams, isForced: Boolean): SyncOutcome =
Unavailable.right()
}

internal object WeekKocourkovRepoImpl : WeekDishRepo {
private val log = localLogger()

override fun getData(params: WeekRepoParams): Flow<ImmutableList<WeekDayDish>> =
flow { emit(persistentListOf<WeekDayDish>()) }
.onStart { log.i { "Starting collection" } }
.onCompletion { log.i { "Completed collection" } }

override suspend fun sync(params: WeekRepoParams, isForced: Boolean): SyncOutcome = run {
log.i { "Starting sync (f: $isForced)" }
Unavailable.right()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ package cz.lastaapps.api.main.di

import cz.lastaapps.api.buffet.di.apiBuffetModule
import cz.lastaapps.api.core.di.apiCoreModule
import cz.lastaapps.api.core.di.registerMenzaType
import cz.lastaapps.api.core.domain.model.MenzaType
import cz.lastaapps.api.core.domain.repo.MenzaRepo
import cz.lastaapps.api.main.data.InfoKocourkovRepoImpl
import cz.lastaapps.api.main.data.KocourkovRepoImpl
import cz.lastaapps.api.main.data.MenzaMasterRepoImpl
import cz.lastaapps.api.main.data.TodayKocourkovDishRepoImpl
import cz.lastaapps.api.main.data.WalletMasterRepository
import cz.lastaapps.api.main.data.WalletMasterRepositoryImpl
import cz.lastaapps.api.main.data.WeekKocourkovRepoImpl
import cz.lastaapps.api.main.domain.usecase.GetImportantRequestParams
import cz.lastaapps.api.main.domain.usecase.GetInfoUC
import cz.lastaapps.api.main.domain.usecase.GetMenzaListUC
Expand Down Expand Up @@ -75,6 +80,13 @@ val apiModule = module {
factoryOf(::WalletLogoutUC)
factoryOf(::WalletRefreshUC)
factoryOf(::GetImportantRequestParams)

registerMenzaType<MenzaType.Testing.Kocourkov>(
menzaRepo = { KocourkovRepoImpl() },
dishRepo = { TodayKocourkovDishRepoImpl },
infoRepo = { InfoKocourkovRepoImpl },
weekRepo = { WeekKocourkovRepoImpl },
)
}

private fun Scope.MenzaMasterRepoImpl() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

package cz.lastaapps.api.main.domain.usecase

import cz.lastaapps.api.core.domain.sync.getData
import cz.lastaapps.api.core.domain.sync.sync
import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Strahov
import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Subsystem
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FEL
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FS
import cz.lastaapps.api.core.domain.model.MenzaType.Testing.Kocourkov
import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.core.util.providers.LinkOpener
Expand All @@ -39,6 +38,7 @@ class OpenMenuUC internal constructor(
Strahov -> "https://agata.suz.cvut.cz/jidelnicky/restauraceSH.php"
is Subsystem -> "https://agata.suz.cvut.cz/jidelnicky/?clPodsystem=${type.subsystemId}"
FEL, FS -> "http://studentcatering.cz/jidelni-listek/"
Kocourkov -> "http://bruxy.regnet.cz/kunda/"
}

linkOpener.openLink(url)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand Down Expand Up @@ -31,6 +31,12 @@ import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Strahov
import cz.lastaapps.api.core.domain.model.MenzaType.Agata.Subsystem
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FEL
import cz.lastaapps.api.core.domain.model.MenzaType.Buffet.FS
import cz.lastaapps.api.core.domain.model.MenzaType.Testing.Kocourkov
import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSourceImpl.Companion.MenzaStoreType.AgataStrahov
import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSourceImpl.Companion.MenzaStoreType.AgataSubsystem
import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSourceImpl.Companion.MenzaStoreType.BuffetFel
import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSourceImpl.Companion.MenzaStoreType.BuffetFs
import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSourceImpl.Companion.MenzaStoreType.TestingKocourkov
import cz.lastaapps.menza.features.settings.domain.model.InitialSelectionBehaviour
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
Expand Down Expand Up @@ -74,16 +80,17 @@ internal class InitMenzaDataSourceImpl(

@Keep
private enum class MenzaStoreType {
AgataStrahov, AgataSubsystem, BuffetFel, BuffetFs,
AgataStrahov, AgataSubsystem, BuffetFel, BuffetFs, TestingKocourkov,
;
}

private fun MenzaType.toStoreKey() =
when (this) {
Strahov -> MenzaStoreType.AgataStrahov
is Subsystem -> MenzaStoreType.AgataSubsystem
FEL -> MenzaStoreType.BuffetFel
FS -> MenzaStoreType.BuffetFs
Strahov -> AgataStrahov
is Subsystem -> AgataSubsystem
FEL -> BuffetFel
FS -> BuffetFs
Kocourkov -> TestingKocourkov
}
}

Expand Down Expand Up @@ -123,10 +130,11 @@ internal class InitMenzaDataSourceImpl(
val type = MenzaStoreType.entries
.firstOrNull { it.name == name } ?: return@combine null
when (type) {
MenzaStoreType.AgataStrahov -> Strahov
MenzaStoreType.AgataSubsystem -> Subsystem(id ?: return@combine null)
MenzaStoreType.BuffetFel -> FEL
MenzaStoreType.BuffetFs -> FS
AgataStrahov -> Strahov
AgataSubsystem -> Subsystem(id ?: return@combine null)
BuffetFel -> FEL
BuffetFs -> FS
TestingKocourkov -> Kocourkov
}
}
}

0 comments on commit db21b8f

Please sign in to comment.