Unify sharing codec and wire iOS KMP actuals
This commit is contained in:
@@ -1,20 +1,56 @@
|
||||
package com.example.casera.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.interop.LocalUIViewController
|
||||
import com.example.casera.data.DataManager
|
||||
import com.example.casera.models.CaseraShareCodec
|
||||
import com.example.casera.models.Contractor
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.usePinned
|
||||
import platform.Foundation.*
|
||||
import platform.UIKit.UIActivityViewController
|
||||
import platform.UIKit.UIViewController
|
||||
|
||||
// Architecture Decision: iOS sharing is implemented natively in Swift
|
||||
// (ContractorSharingManager.swift) because UIActivityViewController and
|
||||
// other iOS-native sharing APIs cannot be driven from Kotlin Multiplatform.
|
||||
// This is an intentional no-op stub. The Android implementation is in androidMain.
|
||||
|
||||
/**
|
||||
* iOS implementation is a no-op - sharing is handled in Swift layer via ContractorSharingManager.swift.
|
||||
* The iOS ContractorDetailView uses the Swift sharing manager directly.
|
||||
*/
|
||||
@Composable
|
||||
actual fun rememberShareContractor(): (Contractor) -> Unit {
|
||||
return { _: Contractor ->
|
||||
// No-op on iOS - sharing handled in Swift layer
|
||||
val viewController = LocalUIViewController.current
|
||||
|
||||
return share@{ contractor: Contractor ->
|
||||
val currentUsername = DataManager.currentUser.value?.username ?: "Unknown"
|
||||
val jsonContent = CaseraShareCodec.encodeContractorPackage(contractor, currentUsername)
|
||||
val fileUrl = writeShareFile(jsonContent, contractor.name) ?: return@share
|
||||
presentShareSheet(viewController, fileUrl)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
private fun writeShareFile(jsonContent: String, displayName: String): NSURL? {
|
||||
val fileName = CaseraShareCodec.safeShareFileName(displayName)
|
||||
val filePath = NSTemporaryDirectory().plus(fileName)
|
||||
|
||||
val bytes = jsonContent.encodeToByteArray()
|
||||
val data = bytes.usePinned { pinned ->
|
||||
NSData.create(bytes = pinned.addressOf(0), length = bytes.size.toULong())
|
||||
}
|
||||
val didCreate = NSFileManager.defaultManager.createFileAtPath(
|
||||
path = filePath,
|
||||
contents = data,
|
||||
attributes = null
|
||||
)
|
||||
if (!didCreate) return null
|
||||
|
||||
return NSURL.fileURLWithPath(filePath)
|
||||
}
|
||||
|
||||
private fun presentShareSheet(viewController: UIViewController, fileUrl: NSURL) {
|
||||
val activityViewController = UIActivityViewController(
|
||||
activityItems = listOf(fileUrl),
|
||||
applicationActivities = null
|
||||
)
|
||||
viewController.presentViewController(
|
||||
viewControllerToPresent = activityViewController,
|
||||
animated = true,
|
||||
completion = null
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,20 +1,89 @@
|
||||
package com.example.casera.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.interop.LocalUIViewController
|
||||
import com.example.casera.models.CaseraShareCodec
|
||||
import com.example.casera.models.Residence
|
||||
import com.example.casera.network.APILayer
|
||||
import com.example.casera.network.ApiResult
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.usePinned
|
||||
import platform.Foundation.*
|
||||
import platform.UIKit.UIActivityViewController
|
||||
import platform.UIKit.UIViewController
|
||||
|
||||
// Architecture Decision: iOS sharing is implemented natively in Swift
|
||||
// (ResidenceSharingManager.swift) because UIActivityViewController and
|
||||
// other iOS-native sharing APIs cannot be driven from Kotlin Multiplatform.
|
||||
// This is an intentional no-op stub. The Android implementation is in androidMain.
|
||||
|
||||
/**
|
||||
* iOS implementation is a no-op - sharing is handled in Swift layer via ResidenceSharingManager.swift.
|
||||
*/
|
||||
@Composable
|
||||
actual fun rememberShareResidence(): Pair<ResidenceSharingState, (Residence) -> Unit> {
|
||||
val state = remember { ResidenceSharingState() }
|
||||
val noOp: (Residence) -> Unit = { /* No-op on iOS - handled in Swift layer */ }
|
||||
return Pair(state, noOp)
|
||||
val viewController = LocalUIViewController.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var state by remember { mutableStateOf(ResidenceSharingState()) }
|
||||
|
||||
val shareFunction: (Residence) -> Unit = share@{ residence ->
|
||||
scope.launch {
|
||||
state = ResidenceSharingState(isLoading = true)
|
||||
|
||||
when (val result = APILayer.generateSharePackage(residence.id)) {
|
||||
is ApiResult.Success -> {
|
||||
val jsonContent = CaseraShareCodec.encodeSharedResidence(result.data)
|
||||
val fileUrl = writeShareFile(jsonContent, residence.name)
|
||||
if (fileUrl == null) {
|
||||
state = ResidenceSharingState(isLoading = false, error = "Failed to create share package")
|
||||
return@launch
|
||||
}
|
||||
|
||||
state = ResidenceSharingState(isLoading = false)
|
||||
presentShareSheet(viewController, fileUrl)
|
||||
}
|
||||
is ApiResult.Error -> {
|
||||
state = ResidenceSharingState(
|
||||
isLoading = false,
|
||||
error = result.message.ifBlank { "Failed to generate share package" }
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
state = ResidenceSharingState(isLoading = false, error = "Failed to generate share package")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Pair(state, shareFunction)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
private fun writeShareFile(jsonContent: String, displayName: String): NSURL? {
|
||||
val fileName = CaseraShareCodec.safeShareFileName(displayName)
|
||||
val filePath = NSTemporaryDirectory().plus(fileName)
|
||||
|
||||
val bytes = jsonContent.encodeToByteArray()
|
||||
val data = bytes.usePinned { pinned ->
|
||||
NSData.create(bytes = pinned.addressOf(0), length = bytes.size.toULong())
|
||||
}
|
||||
val didCreate = NSFileManager.defaultManager.createFileAtPath(
|
||||
path = filePath,
|
||||
contents = data,
|
||||
attributes = null
|
||||
)
|
||||
if (!didCreate) return null
|
||||
|
||||
return NSURL.fileURLWithPath(filePath)
|
||||
}
|
||||
|
||||
private fun presentShareSheet(viewController: UIViewController, fileUrl: NSURL) {
|
||||
val activityViewController = UIActivityViewController(
|
||||
activityItems = listOf(fileUrl),
|
||||
applicationActivities = null
|
||||
)
|
||||
viewController.presentViewController(
|
||||
viewControllerToPresent = activityViewController,
|
||||
animated = true,
|
||||
completion = null
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.example.casera.storage
|
||||
|
||||
import platform.Foundation.NSUserDefaults
|
||||
import kotlin.concurrent.Volatile
|
||||
|
||||
/**
|
||||
@@ -20,13 +19,12 @@ interface KeychainDelegate {
|
||||
* iOS implementation of TokenManager.
|
||||
*
|
||||
* Uses iOS Keychain via [KeychainDelegate] for secure token storage.
|
||||
* Falls back to NSUserDefaults if delegate is not set (should not happen
|
||||
* in production — delegate is set in iOSApp.init before DataManager init).
|
||||
* If delegate is missing, operations fail closed (no insecure fallback).
|
||||
*
|
||||
* On first read, migrates any existing NSUserDefaults token to Keychain.
|
||||
*/
|
||||
actual class TokenManager {
|
||||
private val prefs = NSUserDefaults.standardUserDefaults
|
||||
private val prefs = platform.Foundation.NSUserDefaults.standardUserDefaults
|
||||
|
||||
actual fun saveToken(token: String) {
|
||||
val delegate = keychainDelegate
|
||||
@@ -36,9 +34,8 @@ actual class TokenManager {
|
||||
prefs.removeObjectForKey(TOKEN_KEY)
|
||||
prefs.synchronize()
|
||||
} else {
|
||||
// Fallback (should not happen in production)
|
||||
prefs.setObject(token, forKey = TOKEN_KEY)
|
||||
prefs.synchronize()
|
||||
// Fail closed: never store auth tokens in insecure storage.
|
||||
println("TokenManager: Keychain delegate not set, refusing to save token insecurely")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +59,9 @@ actual class TokenManager {
|
||||
return null
|
||||
}
|
||||
|
||||
// Fallback to NSUserDefaults (should not happen in production)
|
||||
return prefs.stringForKey(TOKEN_KEY)
|
||||
// Fail closed: no insecure fallback reads.
|
||||
println("TokenManager: Keychain delegate not set, refusing insecure token read")
|
||||
return null
|
||||
}
|
||||
|
||||
actual fun clearToken() {
|
||||
|
||||
Reference in New Issue
Block a user