Skip to content

Commit 3dd63df

Browse files
sunkuprfc2822
andauthored
Rewrite RenameAccountFragment to compose (closes #478) (#561)
* Rewrite RenameAccountFragment to compose * Add padding to text field, disable RENAME button when old name = new name --------- Co-authored-by: Ricki Hirner <[email protected]>
1 parent ba08425 commit 3dd63df

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

app/src/main/kotlin/at/bitfire/davdroid/ui/account/RenameAccountFragment.kt

+59-30
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,40 @@ import android.accounts.Account
99
import android.accounts.AccountManager
1010
import android.annotation.SuppressLint
1111
import android.app.Application
12-
import android.app.Dialog
1312
import android.content.ContentResolver
14-
import android.content.DialogInterface
1513
import android.content.pm.PackageManager
1614
import android.os.Bundle
1715
import android.provider.CalendarContract
1816
import android.provider.ContactsContract
19-
import android.widget.EditText
20-
import android.widget.LinearLayout
17+
import android.view.LayoutInflater
18+
import android.view.View
19+
import android.view.ViewGroup
2120
import android.widget.Toast
2221
import androidx.annotation.MainThread
2322
import androidx.annotation.WorkerThread
23+
import androidx.compose.foundation.layout.Column
24+
import androidx.compose.foundation.layout.padding
25+
import androidx.compose.material.AlertDialog
26+
import androidx.compose.material.Text
27+
import androidx.compose.material.TextButton
28+
import androidx.compose.material.TextField
29+
import androidx.compose.runtime.getValue
30+
import androidx.compose.runtime.mutableStateOf
31+
import androidx.compose.runtime.remember
32+
import androidx.compose.runtime.setValue
33+
import androidx.compose.ui.Modifier
34+
import androidx.compose.ui.platform.ComposeView
35+
import androidx.compose.ui.platform.ViewCompositionStrategy
36+
import androidx.compose.ui.res.stringResource
37+
import androidx.compose.ui.unit.dp
2438
import androidx.core.content.ContextCompat
2539
import androidx.fragment.app.DialogFragment
2640
import androidx.fragment.app.viewModels
2741
import androidx.lifecycle.AndroidViewModel
2842
import androidx.lifecycle.MutableLiveData
2943
import androidx.lifecycle.viewModelScope
30-
import at.bitfire.davdroid.*
44+
import at.bitfire.davdroid.InvalidAccountException
45+
import at.bitfire.davdroid.R
3146
import at.bitfire.davdroid.db.AppDatabase
3247
import at.bitfire.davdroid.log.Logger
3348
import at.bitfire.davdroid.resource.LocalAddressBook
@@ -36,7 +51,7 @@ import at.bitfire.davdroid.settings.AccountSettings
3651
import at.bitfire.davdroid.syncadapter.AccountsCleanupWorker
3752
import at.bitfire.davdroid.syncadapter.SyncWorker
3853
import at.bitfire.ical4android.TaskProvider
39-
import com.google.android.material.dialog.MaterialAlertDialogBuilder
54+
import com.google.accompanist.themeadapter.material.MdcTheme
4055
import dagger.hilt.android.AndroidEntryPoint
4156
import dagger.hilt.android.lifecycle.HiltViewModel
4257
import kotlinx.coroutines.Dispatchers
@@ -65,19 +80,9 @@ class RenameAccountFragment: DialogFragment() {
6580
val model by viewModels<Model>()
6681

6782

68-
@SuppressLint("Recycle")
69-
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
83+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
7084
val oldAccount: Account = requireArguments().getParcelable(ARG_ACCOUNT)!!
7185

72-
val editText = EditText(requireActivity()).apply {
73-
setText(oldAccount.name)
74-
requestFocus()
75-
}
76-
val layout = LinearLayout(requireContext())
77-
val density = requireActivity().resources.displayMetrics.density.toInt()
78-
layout.setPadding(8*density, 8*density, 8*density, 8*density)
79-
layout.addView(editText)
80-
8186
model.errorMessage.observe(this) { msg ->
8287
// we use a Toast to show the error message because a Snackbar is not usable for the input dialog fragment
8388
Toast.makeText(requireActivity(), msg, Toast.LENGTH_LONG).show()
@@ -87,19 +92,43 @@ class RenameAccountFragment: DialogFragment() {
8792
requireActivity().finish()
8893
}
8994

90-
return MaterialAlertDialogBuilder(requireActivity())
91-
.setTitle(R.string.account_rename)
92-
.setMessage(R.string.account_rename_new_name)
93-
.setView(layout)
94-
.setPositiveButton(R.string.account_rename_rename, DialogInterface.OnClickListener { _, _ ->
95-
val newName = editText.text.toString()
96-
if (newName == oldAccount.name)
97-
return@OnClickListener
98-
99-
model.renameAccount(oldAccount, newName)
100-
})
101-
.setNegativeButton(android.R.string.cancel) { _, _ -> }
102-
.create()
95+
return ComposeView(requireContext()).apply {
96+
// Dispose of the Composition when the view's LifecycleOwner is destroyed
97+
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
98+
setContent {
99+
MdcTheme {
100+
var accountName by remember { mutableStateOf(oldAccount.name) }
101+
AlertDialog(
102+
onDismissRequest = { dismiss() },
103+
title = { Text(stringResource(R.string.account_rename)) },
104+
text = { Column {
105+
Text(
106+
stringResource(R.string.account_rename_new_name_description),
107+
modifier = Modifier.padding(bottom = 8.dp)
108+
)
109+
TextField(
110+
value = accountName,
111+
onValueChange = { accountName = it },
112+
label = { Text(stringResource(R.string.account_rename_new_name)) },
113+
)
114+
}},
115+
confirmButton = {
116+
TextButton(
117+
onClick = { model.renameAccount(oldAccount, accountName) },
118+
enabled = oldAccount.name != accountName
119+
) {
120+
Text(stringResource(R.string.account_rename_rename).uppercase())
121+
}
122+
},
123+
dismissButton = {
124+
TextButton(onClick = { dismiss() }) {
125+
Text(stringResource(android.R.string.cancel).uppercase())
126+
}
127+
},
128+
)
129+
}
130+
}
131+
}
103132
}
104133

105134

app/src/main/res/values/strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@
243243
<string name="account_synchronize_now">Synchronize now</string>
244244
<string name="account_settings">Account settings</string>
245245
<string name="account_rename">Rename account</string>
246-
<string name="account_rename_new_name">Unsaved local data may be dismissed. Re-synchronization is required after renaming. New account name:</string>
246+
<string name="account_rename_new_name_description">Unsaved local data may be dismissed. Re-synchronization is required after renaming.</string>
247+
<string name="account_rename_new_name">New account name</string>
247248
<string name="account_rename_rename">Rename</string>
248249
<string name="account_rename_exists_already">Account name already taken</string>
249250
<string name="account_rename_couldnt_rename">Couldn\'t rename account</string>

0 commit comments

Comments
 (0)