A local storage management library for Kotlin Multiplatform Mobile iOS and android

Overview

shared-storage-kmm: A local storage management library for Kotlin Multiplatform Mobile iOS and android

Kotlin KMM AGP Gradle Platform

A local storage management library for Kotlin Multiplatform Mobile iOS and android

Features

  • iOS and Android local storage in one interface
  • Provides general storage (UserDefaults, SharedPreferences)
  • Provides secure storage (Keychain, EncryptedPreferences)
  • Annotation is provided to create a custom class only by defining an interface
  • Common interface available on KMM Shared

Requirements

  • iOS
    • Deployment target 10.0 or higher
  • Android
    • minSdkVersion 21

Installation

Default Gradle Settings

Add below gradle settings into your KMP (Kotlin Multiplatform Project)

build.gradle.kts in shared

plugins {
    id("com.android.library")
    kotlin("multiplatform")
}

val sharedStorageVersion = "1.0.1"
val sharedStorage = "com.linecorp.abc:kmm-shared-storage:$sharedStorageVersion"

kotlin {
    sourceSets {
        ios {
            binaries
                .filterIsInstance<Framework>()
                .forEach {
                    it.transitiveExport = true
                    it.export(sharedStorage)
                }
        }
        android()

        val commonMain by getting {
            dependencies {
                implementation(sharedStorage)
            }
        }
        val androidMain by getting {
            dependencies {
                implementation(sharedStorage)
                api(sharedStorage)
            }
        }
        val iosMain by getting {
            dependencies {
                implementation(sharedStorage)
                api(sharedStorage)
            }
        }
    }
}

Usage

To store general value

Android

SharedStorage.save(100, "key::Int")
SharedStorage.save(100f, "key::Float")
SharedStorage.save(102L, "key::Long")
SharedStorage.save(true, "key::Boolean")
SharedStorage.save("String", "key::String")

iOS

SharedStorage.Companion().save(value: 100, forKey: "key::Int")
SharedStorage.Companion().save(value: 100.0, forKey: "key::Float")
SharedStorage.Companion().save(value: 102.0, forKey: "key::Long")
SharedStorage.Companion().save(value: true, forKey: "key::Boolean")
SharedStorage.Companion().save(value: "String", forKey: "key::String")

To get general value

Android

SharedStorage.load("key::Int", 0)
SharedStorage.load("key::Float", 0f)
SharedStorage.load("key::Long", 0L)
SharedStorage.load("key::Boolean", false)
SharedStorage.load("key::String", "")

iOS

SharedStorage.Companion().load(key: "key::Int", default: 0)
SharedStorage.Companion().load(key: "key::Float", default: 0.0)
SharedStorage.Companion().load(key: "key::Long", default: 0.0)
SharedStorage.Companion().load(key: "key::Boolean", default: "")
SharedStorage.Companion().load(key: "key::String", default: "")

To store secure value

Android

SharedStorage.secureSave("SecureString", "key::SecureString")

iOS

SharedStorage.Companion().secureSave(value: "SecureString", forKey: "key::SecureString")

To get secure value

Android

SharedStorage.secureLoad("key::SecureString", "")

iOS

SharedStorage.Companion().secureLoad(key: "key::SecureString", default: "")

Advanced

To Use Annotations for Code Generating (Android only)

settings.gradle.kts in project root

val kotlinVersion = "1.5.21"
val kspVersion = "1.5.21-1.0.0-beta07"

plugins {
    kotlin("jvm") version kotlinVersion
    id("com.google.devtools.ksp") version kspVersion
}

build.gradle.kts in androidApp

val sharedStorageVersion = "1.0.1"

dependencies {
    implementation(project(":shared"))
    implementation("com.linecorp.abc.kmm-shared-storage-annotations:$sharedStorageVersion")
    ksp("com.linecorp.abc:kmm-shared-storage-annotations:$sharedStorageVersion")
}

Define your interface for code generating

package androidApp.sample

import com.linecorp.abc.sharedstorage.annotations.Secure
import com.linecorp.abc.sharedstorage.annotations.SharedStorage

@SharedStorage
interface AppData {
    var someInt: Int
    var someFloat: Float
    val someLong: Long
    val someDouble: Double
    val someBoolean: Boolean
    val someString: String

    @Secure val someSecureString: String
}

Using for your android project

SharedAppData.someInt = 501
SharedAppData.someFloat = 501.5f
SharedAppData.someLong = 500500L
SharedAppData.someBoolean = true
SharedAppData.someString = "I'm Some String"
SharedAppData.someSecureString = "I'm Encrypted String"

Integration with @propertyWrapper on iOS

struct AppData {

    @General(key: "SomeInt", default: 0)
    static var someInt: Int

    @General(key: "SomeFloat", default: 0)
    static var someFloat: Float

    @General(key: "SomeDouble", default: 0)
    static var someDouble: Double

    @General(key: "SomeBool", default: false)
    static var someBool: Bool

    @General(key: "SomeString", default: "")
    static var someString: String

    @Secure(key: "SomeSecure", default: "")
    static var secureString: String
}

@propertyWrapper
struct General<Value> {
    let key: String
    let `default`: Value

    var wrappedValue: Value {
        get { SharedStorage.Companion().load(key: key, default: `default`) as? Value ?? `default` }
        set { SharedStorage.Companion().save(value: newValue, forKey: key) }
    }
}

@propertyWrapper
struct Secure {
    let key: String
    let `default`: String

    var wrappedValue: String {
        get { SharedStorage.Companion().secureLoad(key: key, default: `default`) }
        set { SharedStorage.Companion().secureSave(value: newValue, forKey: key) }
    }
}
You might also like...
A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Multiplatform-DI library for Kotlin Multiplatform Lightweight dependency injection framework for Kotlin Multiplatform application Dependency injection

Simple Kotlin Multiplatform PrayerTimes App for iOS and Android

Kotlin Multiplatform ___ _______ ___ / _ \_______ ___ _____ ___/_ __(_)_ _ ___ ___ / _ | __

A Kotlin Multiplatform Project using TMDB Api. Currently supports Android,iOS,Desktop and web platforms
A Kotlin Multiplatform Project using TMDB Api. Currently supports Android,iOS,Desktop and web platforms

A Kotlin Multiplatform Project using TMDB Api(https://www.themoviedb.org/). Currently this project is implemented in following platforms Andr

BlurHash support for iOS, Android and JVM via Kotlin Multiplatform
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders
Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders

Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders.

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Codename One - Cross Platform Native Apps with Java or Kotlin Codename One is a mobile first cross platform environment for Java and Kotlin developers

A Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a FatFramework for iOS targets, and manages the publishing process in a CocoaPod Repository.

KMP Framework Bundler KMP Framework Bundler is a Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a Fa

Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

Releases(1.0.2)
Owner
LINE
LINE
Location Service Manager for Kotlin Multiplatform Mobile iOS and android

Location Service Manager for Kotlin Multiplatform Mobile iOS and android Features Provides simple permission settings Dramatically reduce the amount o

LINE 55 Dec 10, 2022
Kotlin Multiplatform Mobile demo for Android and iOS - app for viewing Cat pictures

CatViewerDemo Android demo iOS demo Kotlin Multiplatform Mobile demo for Android and iOS. App for viewing Cat pictures from Cats API. This sample show

Martin Rajniak 111 Dec 18, 2022
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Chris Russell 1 Feb 11, 2022
Ethereum Web3 implementation for mobile (android & ios) Kotlin Multiplatform development

Mobile Kotlin web3 This is a Kotlin MultiPlatform library that ... Table of Contents Features Requirements Installation Usage Samples Set Up Locally C

IceRock Development 32 Aug 26, 2022
🍭 GithubSearchKMM - Github Repos Search - Android - iOS - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture

GithubSearchKMM Github Repos Search - Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Depend

Petrus Nguyễn Thái Học 50 Jan 7, 2023
A modular object storage framework for Kotlin multiplatform projects.

ObjectStore A modular object storage framework for Kotlin multiplatform projects. Usage ObjectStore provides a simple key/value storage interface whic

Drew Carlson 4 Nov 10, 2022
Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Kotchaphan Muangsan 3 Nov 15, 2022
KMM RSS Reader: an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile.

KMM RSS Reader This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you ca

Kotlin 1.4k Jan 4, 2023
A kotlin multiplatform BLS12-381 implementation for chia key management

KBLS KBLS is a kotlin multiplatform BLS12-381 implementation built for creating cross platform chia applications. Tips are much appreciated and will d

ChiaChat 7 Nov 21, 2022
Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Gérard Paligot 98 Dec 15, 2022