Unify sharing codec and wire iOS KMP actuals
This commit is contained in:
@@ -5,16 +5,12 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.content.FileProvider
|
||||
import com.example.casera.data.DataManager
|
||||
import com.example.casera.models.CaseraShareCodec
|
||||
import com.example.casera.models.Contractor
|
||||
import com.example.casera.models.SharedContractor
|
||||
import com.example.casera.models.resolveSpecialtyIds
|
||||
import com.example.casera.models.toCreateRequest
|
||||
import com.example.casera.models.toSharedContractor
|
||||
import com.example.casera.network.APILayer
|
||||
import com.example.casera.network.ApiResult
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -22,12 +18,6 @@ import java.io.File
|
||||
*/
|
||||
object ContractorSharingManager {
|
||||
|
||||
private val json = Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a share Intent for a contractor.
|
||||
* The contractor data is written to a temporary .casera file and shared via FileProvider.
|
||||
@@ -39,16 +29,8 @@ object ContractorSharingManager {
|
||||
fun createShareIntent(context: Context, contractor: Contractor): Intent? {
|
||||
return try {
|
||||
val currentUsername = DataManager.currentUser.value?.username ?: "Unknown"
|
||||
val sharedContractor = contractor.toSharedContractor(currentUsername)
|
||||
|
||||
val jsonString = json.encodeToString(SharedContractor.serializer(), sharedContractor)
|
||||
|
||||
// Create safe filename
|
||||
val safeName = contractor.name
|
||||
.replace(" ", "_")
|
||||
.replace("/", "-")
|
||||
.take(50)
|
||||
val fileName = "${safeName}.casera"
|
||||
val jsonString = CaseraShareCodec.encodeContractorPackage(contractor, currentUsername)
|
||||
val fileName = CaseraShareCodec.safeShareFileName(contractor.name)
|
||||
|
||||
// Create shared directory
|
||||
val shareDir = File(context.cacheDir, "shared")
|
||||
@@ -97,15 +79,10 @@ object ContractorSharingManager {
|
||||
val jsonString = inputStream.bufferedReader().use { it.readText() }
|
||||
inputStream.close()
|
||||
|
||||
// Parse JSON
|
||||
val sharedContractor = json.decodeFromString(SharedContractor.serializer(), jsonString)
|
||||
|
||||
// Resolve specialty names to IDs
|
||||
val specialties = DataManager.contractorSpecialties.value
|
||||
val specialtyIds = sharedContractor.resolveSpecialtyIds(specialties)
|
||||
|
||||
// Create the request
|
||||
val createRequest = sharedContractor.toCreateRequest(specialtyIds)
|
||||
val createRequest = CaseraShareCodec.createContractorImportRequestOrNull(
|
||||
jsonContent = jsonString,
|
||||
availableSpecialties = DataManager.contractorSpecialties.value
|
||||
) ?: return@withContext ApiResult.Error("Invalid contractor share package")
|
||||
|
||||
// Call API
|
||||
APILayer.createContractor(createRequest)
|
||||
|
||||
@@ -5,14 +5,13 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.content.FileProvider
|
||||
import com.example.casera.data.DataManager
|
||||
import com.example.casera.models.CaseraShareCodec
|
||||
import com.example.casera.models.JoinResidenceResponse
|
||||
import com.example.casera.models.Residence
|
||||
import com.example.casera.models.SharedResidence
|
||||
import com.example.casera.network.APILayer
|
||||
import com.example.casera.network.ApiResult
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -22,12 +21,6 @@ import java.io.File
|
||||
*/
|
||||
object ResidenceSharingManager {
|
||||
|
||||
private val json = Json {
|
||||
prettyPrint = true
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a share Intent for a residence.
|
||||
* This first calls the backend to generate a share code, then creates the file.
|
||||
@@ -45,14 +38,8 @@ object ResidenceSharingManager {
|
||||
when (result) {
|
||||
is ApiResult.Success -> {
|
||||
val sharedResidence = result.data
|
||||
val jsonString = json.encodeToString(SharedResidence.serializer(), sharedResidence)
|
||||
|
||||
// Create safe filename
|
||||
val safeName = residence.name
|
||||
.replace(" ", "_")
|
||||
.replace("/", "-")
|
||||
.take(50)
|
||||
val fileName = "${safeName}.casera"
|
||||
val jsonString = CaseraShareCodec.encodeSharedResidence(sharedResidence)
|
||||
val fileName = CaseraShareCodec.safeShareFileName(residence.name)
|
||||
|
||||
// Create shared directory
|
||||
val shareDir = File(context.cacheDir, "shared")
|
||||
@@ -108,11 +95,11 @@ object ResidenceSharingManager {
|
||||
val jsonString = inputStream.bufferedReader().use { it.readText() }
|
||||
inputStream.close()
|
||||
|
||||
// Parse JSON
|
||||
val sharedResidence = json.decodeFromString(SharedResidence.serializer(), jsonString)
|
||||
val shareCode = CaseraShareCodec.extractResidenceShareCodeOrNull(jsonString)
|
||||
?: return@withContext ApiResult.Error("Invalid residence share package")
|
||||
|
||||
// Call API with share code
|
||||
APILayer.joinWithCode(sharedResidence.shareCode)
|
||||
APILayer.joinWithCode(shareCode)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
ApiResult.Error("Failed to join residence: ${e.message}")
|
||||
|
||||
Reference in New Issue
Block a user