@@ -9,25 +9,40 @@ import android.accounts.Account
9
9
import android.accounts.AccountManager
10
10
import android.annotation.SuppressLint
11
11
import android.app.Application
12
- import android.app.Dialog
13
12
import android.content.ContentResolver
14
- import android.content.DialogInterface
15
13
import android.content.pm.PackageManager
16
14
import android.os.Bundle
17
15
import android.provider.CalendarContract
18
16
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
21
20
import android.widget.Toast
22
21
import androidx.annotation.MainThread
23
22
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
24
38
import androidx.core.content.ContextCompat
25
39
import androidx.fragment.app.DialogFragment
26
40
import androidx.fragment.app.viewModels
27
41
import androidx.lifecycle.AndroidViewModel
28
42
import androidx.lifecycle.MutableLiveData
29
43
import androidx.lifecycle.viewModelScope
30
- import at.bitfire.davdroid.*
44
+ import at.bitfire.davdroid.InvalidAccountException
45
+ import at.bitfire.davdroid.R
31
46
import at.bitfire.davdroid.db.AppDatabase
32
47
import at.bitfire.davdroid.log.Logger
33
48
import at.bitfire.davdroid.resource.LocalAddressBook
@@ -36,7 +51,7 @@ import at.bitfire.davdroid.settings.AccountSettings
36
51
import at.bitfire.davdroid.syncadapter.AccountsCleanupWorker
37
52
import at.bitfire.davdroid.syncadapter.SyncWorker
38
53
import at.bitfire.ical4android.TaskProvider
39
- import com.google.android. material.dialog.MaterialAlertDialogBuilder
54
+ import com.google.accompanist.themeadapter. material.MdcTheme
40
55
import dagger.hilt.android.AndroidEntryPoint
41
56
import dagger.hilt.android.lifecycle.HiltViewModel
42
57
import kotlinx.coroutines.Dispatchers
@@ -65,19 +80,9 @@ class RenameAccountFragment: DialogFragment() {
65
80
val model by viewModels<Model >()
66
81
67
82
68
- @SuppressLint(" Recycle" )
69
- override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
83
+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
70
84
val oldAccount: Account = requireArguments().getParcelable(ARG_ACCOUNT )!!
71
85
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
-
81
86
model.errorMessage.observe(this ) { msg ->
82
87
// we use a Toast to show the error message because a Snackbar is not usable for the input dialog fragment
83
88
Toast .makeText(requireActivity(), msg, Toast .LENGTH_LONG ).show()
@@ -87,19 +92,43 @@ class RenameAccountFragment: DialogFragment() {
87
92
requireActivity().finish()
88
93
}
89
94
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
+ }
103
132
}
104
133
105
134
0 commit comments