|
|
|
@ -4,6 +4,7 @@ import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import android.widget.PopupMenu
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.fragment.app.viewModels
|
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
@ -13,6 +14,7 @@ import androidx.recyclerview.widget.DividerItemDecoration
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import dev.lowrespalmtree.comet.Identities.Identity
|
|
|
|
|
import dev.lowrespalmtree.comet.databinding.FragmentIdentitiesBinding
|
|
|
|
|
import dev.lowrespalmtree.comet.utils.confirm
|
|
|
|
|
import dev.lowrespalmtree.comet.utils.toast
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
@ -50,6 +52,32 @@ class IdentitiesFragment : Fragment(), IdentitiesAdapter.Listener, IdentityDialo
|
|
|
|
|
IdentityDialog(requireContext(), identity, this).show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onIdentityLongClick(identity: Identity, view: View) {
|
|
|
|
|
PopupMenu(requireContext(), view)
|
|
|
|
|
.apply {
|
|
|
|
|
menuInflater.inflate(R.menu.identity, this.menu)
|
|
|
|
|
setOnMenuItemClickListener { item ->
|
|
|
|
|
when (item.itemId) {
|
|
|
|
|
R.id.item_edit -> {
|
|
|
|
|
IdentityDialog(
|
|
|
|
|
requireContext(),
|
|
|
|
|
identity,
|
|
|
|
|
this@IdentitiesFragment
|
|
|
|
|
).show()
|
|
|
|
|
}
|
|
|
|
|
R.id.item_delete -> {
|
|
|
|
|
confirm(requireContext(), R.string.confirm_identity_delete) {
|
|
|
|
|
vm.deleteIdentity(identity)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else -> {}
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSaveIdentity(identity: Identity) {
|
|
|
|
|
vm.saveIdentity(identity)
|
|
|
|
|
}
|
|
|
|
@ -57,7 +85,10 @@ class IdentitiesFragment : Fragment(), IdentitiesAdapter.Listener, IdentityDialo
|
|
|
|
|
private fun openNewIdentityEditor() {
|
|
|
|
|
toast(requireContext(), R.string.generating_keypair)
|
|
|
|
|
vm.newIdentity.observe(viewLifecycleOwner) { identity ->
|
|
|
|
|
if (identity == null)
|
|
|
|
|
return@observe
|
|
|
|
|
vm.newIdentity.removeObservers(viewLifecycleOwner)
|
|
|
|
|
vm.newIdentity.value = null
|
|
|
|
|
IdentityDialog(requireContext(), identity, this).show()
|
|
|
|
|
}
|
|
|
|
|
vm.createNewIdentity()
|
|
|
|
@ -74,16 +105,25 @@ class IdentitiesFragment : Fragment(), IdentitiesAdapter.Listener, IdentityDialo
|
|
|
|
|
val newIdentityId = Identities.insert(alias)
|
|
|
|
|
newIdentity.postValue(Identities.get(newIdentityId))
|
|
|
|
|
}
|
|
|
|
|
.invokeOnCompletion { refreshIdentities() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun refreshIdentities() {
|
|
|
|
|
viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
|
identities.postValue(Identities.getAll())
|
|
|
|
|
}
|
|
|
|
|
viewModelScope.launch(Dispatchers.IO) { identities.postValue(Identities.getAll()) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun saveIdentity(identity: Identity) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.IO) { Identities.update(identity) }
|
|
|
|
|
.invokeOnCompletion { refreshIdentities() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun deleteIdentity(identity: Identity) {
|
|
|
|
|
viewModelScope.launch(Dispatchers.IO) { Identities.delete(identity) }
|
|
|
|
|
.invokeOnCompletion { refreshIdentities() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private const val TAG = "IdentitiesFragment"
|
|
|
|
|
}
|
|
|
|
|
}
|