-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/native storage.shared.preferences (#261)
* Prevent NativeStorage to be an object. But allow to get it from views: Views.storage Make android to use SharedPreferences instead of files
- Loading branch information
Showing
12 changed files
with
76 additions
and
76 deletions.
There are no files selected for viewing
52 changes: 16 additions & 36 deletions
52
korge/src/androidMain/kotlin/com/soywiz/korge/service/storage/NativeStorage.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,51 +1,31 @@ | ||
package com.soywiz.korge.service.storage | ||
|
||
import java.io.* | ||
import java.util.* | ||
import android.content.* | ||
import com.soywiz.korge.view.* | ||
import com.soywiz.korgw.* | ||
|
||
actual object NativeStorage : IStorage { | ||
val props = Properties() | ||
actual class NativeStorage actual constructor(val views: Views) : IStorage { | ||
private val context = (views.gameWindow as AndroidGameWindow).androidContext | ||
private val preferences = context.getSharedPreferences("KorgeNativeStorage", Context.MODE_PRIVATE) | ||
|
||
init { | ||
load() | ||
} | ||
|
||
private fun load() { | ||
try { | ||
FileInputStream(File("game.storage")).use { fis -> | ||
props.load(fis) | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
private fun save() { | ||
try { | ||
FileOutputStream(File("game.storage")).use { fout -> | ||
props.store(fout, "") | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
private inline fun edit(block: SharedPreferences.Editor.() -> Unit) { | ||
preferences.edit().apply { | ||
block() | ||
apply() | ||
} | ||
} | ||
|
||
actual override fun set(key: String, value: String) { | ||
props[key] = value | ||
save() | ||
edit { putString(key, value) } | ||
} | ||
|
||
actual override fun getOrNull(key: String): String? { | ||
return props[key]?.toString() | ||
} | ||
actual override fun getOrNull(key: String): String? = preferences.getString(key, null) | ||
|
||
actual override fun remove(key: String) { | ||
props.remove(key) | ||
save() | ||
edit { remove(key) } | ||
} | ||
|
||
actual override fun removeAll() { | ||
props.clear() | ||
save() | ||
edit { removeAll() } | ||
} | ||
} |
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
8 changes: 7 additions & 1 deletion
8
korge/src/commonMain/kotlin/com/soywiz/korge/service/storage/NativeStorage.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,9 +1,15 @@ | ||
package com.soywiz.korge.service.storage | ||
|
||
import com.soywiz.kds.* | ||
import com.soywiz.korge.view.* | ||
|
||
/** Cross-platform way of synchronously storing small data */ | ||
expect object NativeStorage : IStorage { | ||
expect class NativeStorage(views: Views) : IStorage { | ||
override fun set(key: String, value: String) | ||
override fun getOrNull(key: String): String? | ||
override fun remove(key: String) | ||
override fun removeAll() | ||
} | ||
|
||
val Views.storage: NativeStorage by Extra.PropertyThis<Views, NativeStorage> { NativeStorage(this) } | ||
val ViewsContainer.storage: NativeStorage get() = this.views.storage |
3 changes: 2 additions & 1 deletion
3
korge/src/commonMain/kotlin/com/soywiz/korge/service/storage/Storage.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,6 +1,7 @@ | ||
package com.soywiz.korge.service.storage | ||
|
||
import com.soywiz.korge.view.* | ||
import com.soywiz.korinject.* | ||
|
||
//@Singleton | ||
open class Storage : IStorage by NativeStorage | ||
open class Storage(views: Views) : IStorage by NativeStorage(views) |
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
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
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
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
24 changes: 9 additions & 15 deletions
24
korge/src/commonTest/kotlin/com/soywiz/korge/storage/NativeStorageTest.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
3 changes: 2 additions & 1 deletion
3
korge/src/jsMain/kotlin/com/soywiz/korge/service/storage/NativeStorage.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
24 changes: 12 additions & 12 deletions
24
korge/src/jvmMain/kotlin/com/soywiz/korge/service/storage/NativeStorage.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
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