-
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
1 parent
daf2718
commit 004d693
Showing
4 changed files
with
51 additions
and
18 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
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
27
composeApp/src/commonMain/kotlin/data/LessonRepository.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,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> | ||
} |
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,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
25
composeApp/src/commonMain/kotlin/data/fake/FakeLessonRepository.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,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() | ||
} | ||
} |