-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Improve performance when listing big credentials (#41)
This PR adds a CredentialPackViewModel to solve performance issues with large credentials.
- Loading branch information
1 parent
cd8a5c2
commit 52d6546
Showing
9 changed files
with
118 additions
and
87 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
example/src/main/java/com/spruceid/mobilesdkexample/viewmodels/CredentialPacksViewModel.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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.spruceid.mobilesdkexample.viewmodels | ||
|
||
import StorageManager | ||
import android.app.Application | ||
import android.content.Context | ||
import androidx.compose.runtime.toMutableStateList | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import com.spruceid.mobile.sdk.CredentialPack | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class CredentialPacksViewModel(application: Application) : AndroidViewModel(application) { | ||
private val storageManager = StorageManager(context = (application as Context)) | ||
private val _credentialPacks = MutableStateFlow(listOf<CredentialPack>()) | ||
val credentialPacks = _credentialPacks.asStateFlow() | ||
|
||
init { | ||
viewModelScope.launch { | ||
_credentialPacks.value = CredentialPack.loadPacks(storageManager) | ||
} | ||
} | ||
|
||
fun saveCredentialPack(credentialPack: CredentialPack) { | ||
credentialPack.save(storageManager) | ||
val tmpCredentialPacksList = _credentialPacks.value.toMutableStateList() | ||
tmpCredentialPacksList.add(credentialPack) | ||
_credentialPacks.value = tmpCredentialPacksList | ||
} | ||
|
||
fun deleteAllCredentialPacks() { | ||
_credentialPacks.value.forEach { credentialPack -> | ||
credentialPack.remove(storageManager) | ||
} | ||
_credentialPacks.value = emptyList() | ||
} | ||
|
||
fun deleteCredentialPack(credentialPack: CredentialPack) { | ||
credentialPack.remove(storageManager) | ||
val tmpCredentialPacksList = _credentialPacks.value.toMutableStateList() | ||
tmpCredentialPacksList.remove(credentialPack) | ||
_credentialPacks.value = tmpCredentialPacksList | ||
} | ||
} | ||
|
||
class CredentialPacksViewModelFactory(private val application: Application) : | ||
ViewModelProvider.NewInstanceFactory() { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
CredentialPacksViewModel(application) as T | ||
} |
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
Oops, something went wrong.