Skip to content

Commit

Permalink
Add support for fakes
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Nov 25, 2024
1 parent daf2718 commit 004d693
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 3 additions & 0 deletions composeApp/src/commonMain/kotlin/AppConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AppConfiguration {
val fakesEnabled = true
}
27 changes: 10 additions & 17 deletions composeApp/src/commonMain/kotlin/data/LessonRepository.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
package data

import arrow.core.Either
import arrow.core.right
import ivy.content.lesson.programmingfundamentals.programmingMathInDisguise
import ivy.data.source.LessonDataSource
import ivy.model.CourseId
import ivy.model.ImageUrl
import ivy.model.Lesson
import ivy.model.LessonId
import kotlinx.coroutines.withContext
import util.DispatchersProvider

class LessonRepository(
class LessonRepositoryImpl(
private val dispatchers: DispatchersProvider,
private val datasource: LessonDataSource,
) {
private val fakeLessonEnabled = true
) : LessonRepository {

suspend fun fetchLesson(
override suspend fun fetchLesson(
course: CourseId,
lesson: LessonId
): Either<String, Lesson> = withContext(dispatchers.io) {
fakeLesson()?.right() ?: datasource.fetchLesson(course, lesson)
datasource.fetchLesson(course, lesson)
}
}

private fun fakeLesson(): Lesson? = if (fakeLessonEnabled) {
Lesson(
id = LessonId("fake"),
name = "Programming: Math in disguise",
tagline = "",
image = ImageUrl(""),
content = programmingMathInDisguise(),
)
} else null
interface LessonRepository {
suspend fun fetchLesson(
course: CourseId,
lesson: LessonId
): Either<String, Lesson>
}
14 changes: 13 additions & 1 deletion composeApp/src/commonMain/kotlin/data/di/DataModule.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package data.di

import AppConfiguration
import data.CourseRepository
import data.LessonRepository
import data.LessonRepositoryImpl
import data.TopicsRepository
import data.fake.FakeLessonRepository
import ivy.di.Di
import ivy.di.Di.register
import ivy.di.autowire.autoWire

object DataModule : Di.Module {
override fun init() = Di.appScope {
register { TopicsRepository(Di.get(), Di.get()) }
register { CourseRepository(Di.get(), Di.get()) }
register { LessonRepository(Di.get(), Di.get()) }
autoWire(::LessonRepositoryImpl)
autoWire(::FakeLessonRepository)
register<LessonRepository> {
if (Di.get<AppConfiguration>().fakesEnabled) {
Di.get<FakeLessonRepository>()
} else {
Di.get<LessonRepositoryImpl>()
}
}
}
}
25 changes: 25 additions & 0 deletions composeApp/src/commonMain/kotlin/data/fake/FakeLessonRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package data.fake

import arrow.core.Either
import arrow.core.right
import data.LessonRepository
import ivy.content.lesson.programmingfundamentals.programmingMathInDisguise
import ivy.model.CourseId
import ivy.model.ImageUrl
import ivy.model.Lesson
import ivy.model.LessonId

class FakeLessonRepository : LessonRepository {
override suspend fun fetchLesson(
course: CourseId,
lesson: LessonId
): Either<String, Lesson> {
return Lesson(
id = LessonId("fake"),
name = "Programming: Math in disguise",
tagline = "",
image = ImageUrl(""),
content = programmingMathInDisguise(),
).right()
}
}

0 comments on commit 004d693

Please sign in to comment.