Skip to content

Commit

Permalink
Redesign Environment Settings fragment and related dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
nitanmarcel committed Jan 25, 2025
1 parent 8553c7a commit d1c3f94
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EnvironmentVarsSettingsFragment : Fragment() {

private fun setupRecyclerView() {
envVarsAdapter = EnvironmentVarsAdapter(envVarsList,
onItemClick = { position -> showEditDialog(position) },
onItemClick = { position -> showDialog(position) },
onDeleteClick = { position -> deleteEnvironmentVar(position) }
)
rvEnvVars.layoutManager = LinearLayoutManager(context)
Expand All @@ -76,59 +76,54 @@ class EnvironmentVarsSettingsFragment : Fragment() {

private fun setupAddButton() {
btnAddEnvVar.setOnClickListener {
showAddDialog()
showDialog(null)
}
}

private fun showAddDialog() {
private fun showDialog(position: Int?) {
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_env_var, null)
val keyInput = dialogView.findViewById<EditText>(R.id.etDialogKey)
val valueInput = dialogView.findViewById<EditText>(R.id.etDialogValue)

AlertDialog.Builder(context)
.setTitle(getString(R.string.env_add_action))
.setView(dialogView)
.setPositiveButton(getString(R.string.save_text)) { _, _ ->
val key = keyInput.text.toString().trim()
val value = valueInput.text.toString().trim()
val envDialogTitle = dialogView.findViewById<TextView>(R.id.envDialogTitle)

if (key.isNotEmpty() && value.isNotEmpty()) {
val envVar = EnvironmentVariable(key, value)
envVarsList.add(envVar)
envVarsAdapter.notifyItemInserted(envVarsList.size - 1)
saveEnvironmentVariables()
}
}
.setNegativeButton(getString(R.string.cancel_text), null)
.create()
.show()
}
if (position == null)
envDialogTitle.text = getString(R.string.env_add_action)
else
envDialogTitle.text = getString(R.string.env_edit_action)

val dialog = AlertDialog.Builder(requireContext(), R.style.CustomAlertDialog).setView(dialogView).create()

private fun showEditDialog(position: Int) {
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_env_var, null)
val keyInput = dialogView.findViewById<EditText>(R.id.etDialogKey)
val valueInput = dialogView.findViewById<EditText>(R.id.etDialogValue)

val currentVar = envVarsList[position]
keyInput.setText(currentVar.key)
valueInput.setText(currentVar.value)
if (position != null) {
keyInput.setText(envVarsList[position].key)
valueInput.setText(envVarsList[position].value)
}

val buttonCancel = dialogView.findViewById<Button>(R.id.buttonCancel)
val buttonSave = dialogView.findViewById<Button>(R.id.buttonSave)

AlertDialog.Builder(context)
.setTitle(getString(R.string.env_edit_action))
.setView(dialogView)
.setPositiveButton("Save") { _, _ ->
val key = keyInput.text.toString().trim()
val value = valueInput.text.toString().trim()
buttonCancel.setOnClickListener {
dialog.dismiss()
}
buttonSave.setOnClickListener {
val key = keyInput.text.toString().trim()
val value = valueInput.text.toString().trim()

if (key.isNotEmpty()) {
envVarsList[position] = EnvironmentVariable(key, value)
if (key.isNotEmpty() && value.isNotEmpty()) {
val envVar = EnvironmentVariable(key, value)
if (position == null) {
envVarsList.add(envVar)
envVarsAdapter.notifyItemInserted(envVarsList.size - 1)
} else {
envVarsList[position] = envVar
envVarsAdapter.notifyItemChanged(position)
saveEnvironmentVariables()
}
saveEnvironmentVariables()
}
.setNegativeButton("Cancel", null)
.create()
.show()
dialog.dismiss()
}
dialog.show()
}

private fun deleteEnvironmentVar(position: Int) {
Expand Down
62 changes: 46 additions & 16 deletions app/src/main/res/layout/dialog_env_var.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,61 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="16dp">
android:padding="16dp"
android:theme="@style/Theme.MaterialComponents.DayNight">

<com.google.android.material.textfield.TextInputLayout
<TextView
android:id="@+id/envDialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Key">
android:text="@string/env_edit_action"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall"
android:textAlignment="center"
android:layout_marginBottom="16dp"/>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etDialogKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<EditText
android:id="@+id/etDialogKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="text"
android:hint="Key"
tools:ignore="LabelFor">
</EditText>

<com.google.android.material.textfield.TextInputLayout
<EditText
android:id="@+id/etDialogValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Value">
android:importantForAutofill="no"
android:inputType="text"
android:hint="Value"
tools:ignore="LabelFor">
</EditText>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etDialogValue"
android:layout_width="match_parent"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end">

<com.google.android.material.button.MaterialButton
android:id="@+id/buttonCancel"
style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/cancel_text"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/buttonSave"
style="@style/Widget.Material3.Button.IconButton.Filled.Tonal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
android:text="@string/save_text"/>

</LinearLayout>

</LinearLayout>
5 changes: 3 additions & 2 deletions app/src/main/res/layout/fragment_env_vars_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
android:padding="16dp"
tools:context=".fragments.EnvironmentVarsSettingsFragment">

<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/btnAddEnvVar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Environment Variable"/>
android:text="@string/env_add_action"
style="@style/Widget.Material3.Button.ElevatedButton"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvEnvVars"
Expand Down

0 comments on commit d1c3f94

Please sign in to comment.