Skip to content

Platform specific application home and cache directories for KMP

License

Notifications You must be signed in to change notification settings

psuzn/multiplatform-paths

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multiplatform Paths

Release CI

kotlin

license

Note

Follow migration guide to migrate from v0.1 to v0.2.

Platform-specific application home and cache directories for KMP.

badge-JVM badge-Android badge-iOS badge-macOS badge-Js(Node)

Path Mapping

Platform Cache Directory Data Directory
Android context.cacheDir ApplicationInfo.dataDir
IOS/IpadOs/WatchOs NSCachesDirectory NSApplicationSupportDirectory
Mac (native/jvm/node) ~/Library/Caches/<app-id> ~/Library/Application Support/<app-id>
Windows (jvm/node) C:\Users\<user>\AppData/Caches/<app-id> C:\Users\<user>\AppData/<app-id>>
Linux (jvm/node) ~/.cache/<app-id> ~/local/share/<app-id>

Setup

repositories {
  mavenCentral()
}

dependencies {
  implementation("me.sujanpoudel.multiplatform.utils:multiplatform-paths:0.2.2")
}

Usage

App data directory

import me.sujanpoudel.utils.paths.appDataDirectory

val packageName = "example.com.app"

val dataDirectory = appDataDirectory(packageName)

This will return ApplicationInfo.dataDir on android, NSApplicationSupportDirectory on IOS and equivalent platform specific data directory on other platforms.

App cache directory

import me.sujanpoudel.utils.paths.appCacheDirectory

val packageName = "example.com.app"

val dataDirectory = appCacheDirectory(packageName)

This will return Context.cacheDir on android, NSCachesDirectory on IOS and equivalent platform specific caches directory on other platforms

Other Libraries from this Repository.

  • platform-identifier : Identify the current platform.
  • context-provider : Get android context anywhere on your android source set.

Table of content

Platform Identifier

JVM(desktop) Android Android-native iOS macOS windows Js(Node)

Setup

repositories {
  mavenCentral()
}

dependencies {
  implementation("me.sujanpoudel.multiplatform.utils:platform-identifier:0.2.2")
}

Usage

Get current running platform info

import me.sujanpoudel.multiplatform.utils.platformIdentifier.platform

val currentPlatform = platform()

Possible return values

It returns Platform sealed class:

sealed class Platform {
  sealed class JS : Platform() {
    data class Node(val os: OS, val nodeVersion: String, val v8Version: String) : JS()
    data class Browser(val userAgent: String) : JS()
  }

  sealed class OS(open val arch: Arch) : Platform() {
    data class Unknown(override val arch: Arch, val version: String) : OS(arch)
    data class MacOs(override val arch: Arch, val version: String) : OS(arch)
    data class IOS(override val arch: Arch, val version: String, val isSimulator: Boolean) : OS(arch)
    data class WatchOs(override val arch: Arch, val version: String, val isSimulator: Boolean) : OS(arch)
    data class TvOs(override val arch: Arch, val version: String, val isSimulator: Boolean) : OS(arch)
    data class Android(override val arch: Arch, val buildNumber: Int, val androidVersion: String, val isWatch: Boolean, val isTv: Boolean) : OS(arch)

    data class Linux(override val arch: Arch, val version: String) : OS(arch)
    data class Windows(override val arch: Arch, val version: String) : OS(arch)
  }
}

where,Arch is an enum representing CPU architecture

enum class Arch {
  UNKNOWN,
  X64,
  X86,
  ARM_X64,
  ARM_X32,
}

Context Provider

Android

Setup

repositories {
  mavenCentral()
}

dependencies {
  implementation("me.sujanpoudel.multiplatform.utils:context-provider:0.2.2")
}

Usage

Get Android Context

import me.sujanpoudel.utils.contextProvider.applicationContext
import android.content.Context

val context: Context = applicationContext

Contributions

Contributions are always welcome!. If you'd like to contribute, please feel free to create a PR or open an issue.

License

Copyright 2023 Sujan Poudel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.