Skip to content

Commit

Permalink
refactor(theme): migrate to kmp module.
Browse files Browse the repository at this point in the history
* create core module for string resources.
* common source set.
  • Loading branch information
GerardPaligot committed Mar 11, 2024
1 parent 1e53b65 commit 4312e83
Show file tree
Hide file tree
Showing 162 changed files with 1,598 additions and 762 deletions.
2 changes: 2 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dependencies {
implementation(projects.themeM3.main.mainDi)
implementation(projects.shared.core)
implementation(projects.shared.coreDi)
implementation(projects.shared.resources)
implementation(libs.settings)

implementation(libs.google.material)
Expand All @@ -87,6 +88,7 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(compose.ui)
implementation(compose.components.resources)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.res.stringResource
import androidx.core.app.ShareCompat
import androidx.core.content.FileProvider
import androidx.navigation.NavHostController
Expand All @@ -22,8 +21,13 @@ import androidx.work.WorkManager
import com.russhwolf.settings.ExperimentalSettingsApi
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import org.gdglille.devfest.android.shared.resources.Resource
import org.gdglille.devfest.android.shared.resources.text_export_subject
import org.gdglille.devfest.android.shared.resources.text_report_app_target
import org.gdglille.devfest.android.shared.resources.text_report_subject
import org.gdglille.devfest.android.theme.Main
import org.gdglille.devfest.android.theme.m3.style.R
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.stringResource
import java.io.File

@Suppress("LongMethod")
Expand All @@ -38,6 +42,7 @@ class MainActivity : ComponentActivity() {
navController.handleDeepLink(intent)
}

@OptIn(ExperimentalResourceApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -56,9 +61,9 @@ class MainActivity : ComponentActivity() {
onDispose {}
}
navController = rememberNavController()
val exportSubject = stringResource(id = R.string.text_export_subject)
val reportSubject = stringResource(id = R.string.text_report_subject)
val reportAppTarget = stringResource(id = R.string.text_report_app_target)
val exportSubject = stringResource(Resource.string.text_export_subject)
val reportSubject = stringResource(Resource.string.text_report_subject)
val reportAppTarget = stringResource(Resource.string.text_report_app_target)
Main(
openfeedbackFirebaseConfig = openfeedbackFirebaseConfig,
launchUrl = { launchUrl(it) },
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include(":androidApp")
include(":shared:core")
include(":shared:core-di")
include(":shared:models")
include(":shared:resources")
include(":shared:ui-models")
include(":backend")
include(":benchmark")
Expand Down
1 change: 1 addition & 0 deletions shared/core-di/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kotlin {
export(projects.shared.core)
export(projects.shared.models)
export(projects.shared.uiModels)
export(projects.shared.resources)
// Required https://github.com/cashapp/sqldelight/issues/1442
linkerOpts.add("-lsqlite3")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ val databasesModule = module {
single { EventDao(db = get(), settings = get()) }
single { FeaturesActivatedDao(db = get(), settings = get()) }
single { PartnerDao(db = get(), platform = get()) }
single { ScheduleDao(db = get(), settings = get(), platform = get()) }
single { SpeakerDao(db = get(), platform = get()) }
single { TalkDao(db = get(), platform = get()) }
single { ScheduleDao(db = get(), settings = get()) }
single { SpeakerDao(db = get()) }
single { TalkDao(db = get()) }
single { UserDao(db = get(), platform = get()) }
}
11 changes: 8 additions & 3 deletions shared/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.gradle.internal.os.OperatingSystem

plugins {
id("conferences4hall.multiplatform.library")
id("conferences4hall.android.library.compose")
id("conferences4hall.quality")
id("kotlinx-serialization")
id("com.rickclephas.kmp.nativecoroutines")
Expand All @@ -27,6 +28,7 @@ kotlin {
export(libs.settings)
export(projects.shared.models)
export(projects.shared.uiModels)
export(projects.shared.resources)
// Required https://github.com/cashapp/sqldelight/issues/1442
linkerOpts.add("-lsqlite3")
}
Expand All @@ -38,7 +40,10 @@ kotlin {
dependencies {
api(projects.shared.models)
api(projects.shared.uiModels)
implementation(libs.jetbrains.kotlinx.coroutines)
api(projects.shared.resources)

implementation(compose.runtime)
implementation(compose.components.resources)

implementation(libs.ktor.client.core)
implementation(libs.ktor.client.json)
Expand All @@ -49,6 +54,7 @@ kotlin {

implementation(libs.jetbrains.kotlinx.datetime)
implementation(libs.jetbrains.kotlinx.collections)
implementation(libs.jetbrains.kotlinx.coroutines)

implementation(libs.cash.sqldelight.runtime)
implementation(libs.cash.sqldelight.coroutines)
Expand All @@ -66,9 +72,8 @@ kotlin {
}
val androidMain by getting {
dependencies {
implementation(projects.themeM3.style.theme)
implementation(libs.ktor.client.android)
implementation(libs.cash.sqldelight.android)
implementation(libs.ktor.client.android)
implementation(libs.google.zxing)
implementation(libs.zxing.android.embedded)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@ import kotlinx.datetime.TimeZone
import kotlinx.datetime.minus
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import org.gdglille.devfest.android.theme.m3.style.R
import org.gdglille.devfest.android.shared.resources.Resource
import org.gdglille.devfest.android.shared.resources.title_notif_reminder_talk
import org.gdglille.devfest.models.ui.TalkItemUi
import org.gdglille.devfest.repositories.AgendaRepository
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.getString
import java.util.Locale

class AlarmScheduler(
private val repository: AgendaRepository,
private val alarmManager: AlarmManager,
private val alarmIntentFactory: AlarmIntentFactory
) {
@OptIn(ExperimentalResourceApi::class)
@SuppressLint("UnspecifiedImmutableFlag")
suspend fun schedule(context: Context, talkItem: TalkItemUi) = coroutineScope {
val isFavorite = !talkItem.isFavorite
repository.markAsRead(talkItem.id, isFavorite)
val title = context.getString(
R.string.title_notif_reminder_talk, talkItem.room.lowercase(Locale.getDefault())
val title = getString(
Resource.string.title_notif_reminder_talk,
talkItem.room.lowercase(Locale.getDefault())
)
val intent = alarmIntentFactory.create(context, talkItem.id, title, talkItem.title)
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,14 @@ import java.io.ByteArrayOutputStream
data class AndroidContext(val context: Context)
actual typealias PlatformContext = AndroidContext

actual class Platform actual constructor(private val context: PlatformContext) {
actual class Platform actual constructor(context: PlatformContext) {
actual val httpEngine: HttpClientEngine = Android.create()
actual val fileEngine = FileEngine(
fileSystem = FileSystem.SYSTEM,
tempFolderPath = context.context.cacheDir?.absolutePath?.toPath()
?: FileSystem.SYSTEM_TEMPORARY_DIRECTORY
)
actual val hasSupportSVG: Boolean = true
actual fun getString(key: String): String {
val androidContext = context.context
val resourceId =
androidContext.resources.getIdentifier(key, "string", androidContext.packageName)
if (resourceId == 0) return key
return androidContext.getString(resourceId)
}

actual fun getString(key: String, count: Int, args: List<Any>): String {
val androidContext = context.context
val resourceId =
androidContext.resources.getIdentifier(key, "plurals", androidContext.packageName)
if (resourceId == 0) return key
return androidContext.resources.getQuantityString(resourceId, count, *args.toTypedArray())
}

actual fun getString(key: String, args: List<Any>): String {
val androidContext = context.context
val resourceId =
androidContext.resources.getIdentifier(key, "string", androidContext.packageName)
if (resourceId == 0) return key
return androidContext.getString(resourceId, *args.toTypedArray())
}
}

actual class DecimalFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ expect class Platform(context: PlatformContext) {
val httpEngine: HttpClientEngine
val fileEngine: FileEngine
val hasSupportSVG: Boolean
fun getString(key: String): String
fun getString(key: String, count: Int, args: List<Any>): String
fun getString(key: String, args: List<Any>): String
}

expect class DecimalFormat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import org.gdglille.devfest.Platform
import org.gdglille.devfest.database.mappers.convertCategoryUi
import org.gdglille.devfest.database.mappers.convertFormatUi
import org.gdglille.devfest.database.mappers.convertTalkItemUi
Expand All @@ -32,8 +31,7 @@ import org.gdglille.devfest.models.ui.FormatUi
@ExperimentalSettingsApi
class ScheduleDao(
private val db: Conferences4HallDatabase,
private val settings: ObservableSettings,
private val platform: Platform
private val settings: ObservableSettings
) {
fun fetchSchedules(eventId: String): Flow<ImmutableMap<String, AgendaUi>> = combine(
db.sessionQueries
Expand Down Expand Up @@ -84,13 +82,8 @@ class ScheduleDao(
} else {
emptyList()
}
it.convertTalkItemUi(
getString = platform::getString,
getStringArg = platform::getString,
getPluralsArg = platform::getString,
speakers = speakers
)
} + breaks.map { it.convertTalkItemUi(platform::getString, platform::getString) }
it.convertTalkItemUi(speakers = speakers)
} + breaks.map { it.convertTalkItemUi() }
sessions.distinctBy { it.date }
.associate {
it.date to AgendaUi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import org.gdglille.devfest.Platform
import org.gdglille.devfest.database.mappers.convertTalkItemUi
import org.gdglille.devfest.database.mappers.convertToSpeakerItemUi
import org.gdglille.devfest.database.mappers.convertToSpeakerUi
Expand All @@ -19,10 +18,7 @@ import org.gdglille.devfest.models.ui.SpeakerItemUi
import org.gdglille.devfest.models.ui.SpeakerUi
import org.gdglille.devfest.models.ui.TalkItemUi

class SpeakerDao(
private val db: Conferences4HallDatabase,
private val platform: Platform
) {
class SpeakerDao(private val db: Conferences4HallDatabase) {
fun fetchSpeaker(eventId: String, speakerId: String): Flow<SpeakerUi> {
return combine(
db.speakerQueries.selectSpeaker(speakerId, eventId)
Expand All @@ -31,7 +27,6 @@ class SpeakerDao(
fetchTalksBySpeakerId(eventId, speakerId),
transform = { speaker, talks ->
return@combine speaker.convertToSpeakerUi(
getStringArg = platform::getString,
talks = talks.toImmutableList()
)
}
Expand All @@ -50,9 +45,6 @@ class SpeakerDao(
talks
.map { talk ->
talk.convertTalkItemUi(
getString = platform::getString,
getStringArg = platform::getString,
getPluralsArg = platform::getString,
session = db.sessionQueries
.selectSessionByTalkId(eventId, talk.id)
.executeAsOne(),
Expand All @@ -71,7 +63,7 @@ class SpeakerDao(
.mapToList(Dispatchers.IO)
.map { speakers ->
speakers
.map { speaker -> speaker.convertToSpeakerItemUi(platform::getString) }
.map { speaker -> speaker.convertToSpeakerItemUi() }
.toImmutableList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import org.gdglille.devfest.Platform
import org.gdglille.devfest.database.mappers.convertTalkUi
import org.gdglille.devfest.db.Conferences4HallDatabase
import org.gdglille.devfest.models.ui.TalkUi

class TalkDao(private val db: Conferences4HallDatabase, private val platform: Platform) {
class TalkDao(private val db: Conferences4HallDatabase) {
fun fetchTalk(eventId: String, talkId: String): Flow<TalkUi> = db.transactionWithResult {
combine(
flow = db.sessionQueries
Expand All @@ -28,7 +27,7 @@ class TalkDao(private val db: Conferences4HallDatabase, private val platform: Pl
.asFlow()
.mapToOne(Dispatchers.IO),
transform = { speakers, openfeedback, talk ->
talk.convertTalkUi(platform::getString, speakers, openfeedback)
talk.convertTalkUi(speakers, openfeedback)
}
)
}
Expand Down
Loading

0 comments on commit 4312e83

Please sign in to comment.