Skip to content

Commit

Permalink
feat: remember the last used account type for creating contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 17, 2023
1 parent c60e3a4 commit ace7fa2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@ class DeviceContactsRepository(private val context: Context) : ContactsRepositor
@RequiresPermission(Manifest.permission.WRITE_CONTACTS)
override suspend fun createContact(contact: ContactData) {
withContext(Dispatchers.IO) {
val lastChosenAccount = Preferences.getLastChosenAccount()
val ops = listOfNotNull(
getCreateAction(
contact.accountType ?: ANDROID_ACCOUNT_TYPE,
contact.accountName ?: ANDROID_CONTACTS_NAME
contact.accountType ?: lastChosenAccount.first,
contact.accountName ?: lastChosenAccount.second
),
getInsertAction(
StructuredName.CONTENT_ITEM_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import com.bnyro.contacts.ui.components.editor.DatePickerEditor
import com.bnyro.contacts.ui.components.editor.TextFieldEditor
import com.bnyro.contacts.ui.models.ContactsModel
import com.bnyro.contacts.util.ContactsHelper
import com.bnyro.contacts.repo.DeviceContactsRepository
import com.bnyro.contacts.util.ImageHelper
import com.bnyro.contacts.util.Preferences

@OptIn(ExperimentalFoundationApi::class)
@Composable
Expand Down Expand Up @@ -146,9 +146,10 @@ fun ContactEditor(
}

var selectedAccount by remember {
val lastChosenAccount = Preferences.getLastChosenAccount()
mutableStateOf(
(contact?.accountType ?: DeviceContactsRepository.ANDROID_ACCOUNT_TYPE) to
(contact?.accountName ?: DeviceContactsRepository.ANDROID_CONTACTS_NAME)
(contact?.accountType ?: lastChosenAccount.first) to
(contact?.accountName ?: lastChosenAccount.second)
)
}

Expand Down Expand Up @@ -431,6 +432,9 @@ fun ContactEditor(
items(availableAccounts) {
ClickableText(text = it.second) {
selectedAccount = it
Preferences.edit {
putString(Preferences.lastChosenAccount, "${it.first}|${it.second}")
}
showAccountTypeDialog = false
}
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/bnyro/contacts/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.SnapshotMutationPolicy
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.bnyro.contacts.enums.BackupType
import com.bnyro.contacts.repo.DeviceContactsRepository

object Preferences {
private const val prefFile = "preferences"
Expand All @@ -27,6 +28,7 @@ object Preferences {
const val encryptBackupsKey = "encryptBackups"
const val encryptBackupPasswordKey = "encryptBackupsPassword"
const val storeSmsLocallyKey = "storeSmsLocally"
const val lastChosenAccount = "lastChosenAccount"

fun init(context: Context) {
preferences = context.getSharedPreferences(prefFile, Context.MODE_PRIVATE)
Expand All @@ -44,6 +46,17 @@ object Preferences {
fun getBackupType(): BackupType {
return BackupType.fromInt(getInt(backupTypeKey, BackupType.NONE.ordinal))
}

fun getLastChosenAccount(): Pair<String, String> {
getString(lastChosenAccount, "")
.takeIf { !it.isNullOrBlank() }
?.let {
val split = it.split("|")
return split.first() to split.last()
}

return DeviceContactsRepository.ANDROID_ACCOUNT_TYPE to DeviceContactsRepository.ANDROID_CONTACTS_NAME
}
}

@Composable
Expand Down

0 comments on commit ace7fa2

Please sign in to comment.