Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android で Multiplatform Settings を使う

Android で Multiplatform Settings を使う

Yumemi.apk #5 ( https://yumemi.connpass.com/event/219861/ ) の資料

D8281434c0409ba2051cd3f7590e4c2f?s=128

Hiroyuki Kusu

September 10, 2021
Tweet

Transcript

  1. Android Ͱ Multiplatform Settings Λ࢖͏ 2021.9.10 YUMEMI.apk #5 Hiroyuki Kusu

    ( @hkusu_ )
  2. About me 

  3. ΈΜͳ͕஌ͬͯΔ͋ͷαʔϏεɺ ࣮͸ΏΊΈ͕࡞ͬͯ·͢ɻ શੈք5000ສMAUʹୡ͢ΔωοταʔϏεΛΦʔμʔϝΠυͰاըઃܭ͠ɺ γεςϜߏஙӡ༻͔ΒվળఏҊ·Ͱαϙʔτ͍͖ͯ͠·͢ɻ 

  4.  Multiplatform Settings https://github.com/russhwolf/multiplatform-settings • key-value σʔλӬଓԽͷҝͷ Kotlin Multiplatform ϥΠϒϥϦ

    • Android ͷ৔߹͸ SharedPreferences ͕಺෦తʹར༻͞ΕΔ
  5. Kotlin Multiplatform ϓϩδΣΫτͰ ͳͯ͘΋௨ৗͷ Android ͷϓϩδΣΫτ Ͱ΋ར༻Ͱ͖ΔͷͰར༻ͯ͠ΈΔ  ຊ೔ͷ࿩୊

  6.  implementation("com.russhwolf:multiplatform-settings:0.7.4" ) implementation("com.russhwolf:multiplatform-settings-coroutines:0.7.4") build.gradle.kts ಋೖ ※ ݱ࣌఺ͷ࠷৽όʔδϣϯ͸ 0.8 ͷΑ͏Ͱ͢

    Coroutines ༻ͷ֦ு
  7.  val context: Context = .. . val sharedPreferences: SharedPreferences

    = context.getSharedPreferences("main", Context.MODE_PRIVATE ) val flowSettings: FlowSettings = AndroidSettings(sharedPreferences, commit = true).toFlowSettings() FlowSettings Πϯελϯεͷ࡞੒
  8.  @ExperimentalSettingsAp i class AccountDao(private val settings: FlowSettings) { val

    nameFlow: Flow<String?> = settings.getStringOrNullFlow(KEY_NAME ) suspend fun getName(): String? { return settings.getStringOrNull(KEY_NAME ) } suspend fun setName(name: String) { settings.putString(KEY_NAME, name ) } companion object { private const val KEY_NAME = "name " } } Dao Ϋϥεͷ༻ҙ FlowSettings ΠϯελϯεΛͦͷ··ར༻ͯ͠΋Α͍͕ɺςετ࣌ʹϞοΫ͍ͨ͠ͷͰϥούʔΫϥεΛ༻ҙ ʢkey Λར༻ऀ͔ΒӅṭͨ͠Γɺૢ࡞Λந৅Խ͢Δ౳ͷ໨త΋͋Δʣ
  9.  @ExperimentalSettingsAp i class AccountDao(private val settings: FlowSettings) { val

    nameFlow: Flow<String?> = settings.getStringOrNullFlow(KEY_NAME ) suspend fun getName(): String? { return settings.getStringOrNull(KEY_NAME ) } suspend fun setName(name: String) { settings.putString(KEY_NAME, name ) } companion object { private const val KEY_NAME = "name " } } DaoΫϥεͷ༻ҙ Suspending Function ͰಡΈॻ͖
  10.  @ExperimentalSettingsAp i class AccountDao(private val settings: FlowSettings) { val

    nameFlow: Flow<String?> = settings.getStringOrNullFlow(KEY_NAME ) suspend fun getName(): String? { return settings.getStringOrNull(KEY_NAME ) } suspend fun setName(name: String) { settings.putString(KEY_NAME, name ) } companion object { private const val KEY_NAME = "name " } } DaoΫϥεͷ༻ҙ σʔλʹมߋΠϕϯτΛྲྀ͢ Flow
  11.  val context: Context = .. . val sharedPreferences: SharedPreferences

    = context.getSharedPreferences("main", Context.MODE_PRIVATE ) val flowSettings: FlowSettings = AndroidSettings(sharedPreferences, commit = true).toFlowSettings() FlowSettings Πϯελϯεͷ࡞੒ʢ࠶ܝʣ SharedPreferences ΁ͷσʔλอଘͰ͸ commitʢಉظʣͱ applyʢඇಉظʣͷ2ܗ͕ࣜ͋Δ͕ɺCoroutines Խ ͨ͜͠ͱʹΑΓ commit ͷσϝϦοτʢεϨουͷϒϩοΫʣ͕ബ·͍ͬͯΔͷͰ commit Λ࠾༻ 㱺 Coroutines ͷ࢓૊Έʹ৐Δ͜ͱͰɺ࣮ߦॱ΍εϨουΛؾʹͤͣσʔλͷಡΈॻ͖͕Մೳʹ
  12.  • Multiplatform ϥΠϒϥϦͰ͋Δ͕ Android ୯ମͰར༻ͯ͠ ΋ศར • ଞʹ΋ศརͦ͏ͳ Multiplatform

    ϥΠϒϥϦ͕͋ΔͷͰಋೖΛ ݕ౼ͯ͠΋Α͍͔΋ʁ • Retro fi t ͷସΘΓʹ Ktor • Room ͷସΘΓʹ SQLDelight ͳͲͳͲ.. ͓ΘΓʹ
  13. Thank you ! @hkusu_ ฐࣾ͸ Android ΤϯδχΞ ઈࢍืूத Ͱ͢ʂ