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

Android で Multiplatform Settings を使う

Hiroyuki Kusu
September 10, 2021

Android で Multiplatform Settings を使う

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

Hiroyuki Kusu

September 10, 2021
Tweet

More Decks by Hiroyuki Kusu

Other Decks in Programming

Transcript

  1.  val context: Context = .. . val sharedPreferences: SharedPreferences

    = context.getSharedPreferences("main", Context.MODE_PRIVATE ) val flowSettings: FlowSettings = AndroidSettings(sharedPreferences, commit = true).toFlowSettings() FlowSettings Πϯελϯεͷ࡞੒
  2.  @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 Λར༻ऀ͔ΒӅṭͨ͠Γɺૢ࡞Λந৅Խ͢Δ౳ͷ໨త΋͋Δʣ
  3.  @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 ͰಡΈॻ͖
  4.  @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
  5.  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 ͷ࢓૊Έʹ৐Δ͜ͱͰɺ࣮ߦॱ΍εϨουΛؾʹͤͣσʔλͷಡΈॻ͖͕Մೳʹ
  6.  • Multiplatform ϥΠϒϥϦͰ͋Δ͕ Android ୯ମͰར༻ͯ͠ ΋ศར • ଞʹ΋ศརͦ͏ͳ Multiplatform

    ϥΠϒϥϦ͕͋ΔͷͰಋೖΛ ݕ౼ͯ͠΋Α͍͔΋ʁ • Retro fi t ͷସΘΓʹ Ktor • Room ͷସΘΓʹ SQLDelight ͳͲͳͲ.. ͓ΘΓʹ