-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add settings to set custom environment variables (#85)
- Loading branch information
1 parent
16f7b94
commit 4afca3b
Showing
13 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
app/src/main/java/com/micewine/emu/fragments/EnvironmentVarsSettingsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package com.micewine.emu.fragments | ||
|
||
import com.micewine.emu.R | ||
import android.app.AlertDialog | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.EditText | ||
import android.widget.ImageButton | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import androidx.preference.PreferenceManager | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
|
||
class EnvironmentVarsSettingsFragment : Fragment() { | ||
private lateinit var btnAddEnvVar: Button | ||
private lateinit var rvEnvVars: RecyclerView | ||
|
||
private val envVarsList = mutableListOf<EnvironmentVariable>() | ||
private lateinit var envVarsAdapter: EnvironmentVarsAdapter | ||
|
||
companion object { | ||
private const val PREFS_NAME = "EnvironmentVariablesPrefs" | ||
private const val ENV_VARS_KEY = "environment_variables" | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
val view = inflater.inflate(R.layout.fragment_env_vars_settings, container, false) | ||
|
||
btnAddEnvVar = view.findViewById(R.id.btnAddEnvVar) | ||
rvEnvVars = view.findViewById(R.id.rvEnvVars) | ||
|
||
loadEnvironmentVariables() | ||
setupRecyclerView() | ||
setupAddButton() | ||
|
||
return view | ||
} | ||
|
||
private fun loadEnvironmentVariables() { | ||
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) | ||
val savedVarsJson = preferences.getString(ENV_VARS_KEY, null) | ||
savedVarsJson?.let { | ||
val type = object : TypeToken<List<EnvironmentVariable>>() {}.type | ||
val savedVars = Gson().fromJson<List<EnvironmentVariable>>(it, type) | ||
envVarsList.clear() | ||
envVarsList.addAll(savedVars) | ||
} | ||
} | ||
|
||
private fun saveEnvironmentVariables() { | ||
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) | ||
val editor = preferences.edit() | ||
val varsJson = Gson().toJson(envVarsList) | ||
editor.putString(ENV_VARS_KEY, varsJson) | ||
editor.apply() | ||
} | ||
|
||
private fun setupRecyclerView() { | ||
envVarsAdapter = EnvironmentVarsAdapter(envVarsList, | ||
onItemClick = { position -> showEditDialog(position) }, | ||
onDeleteClick = { position -> deleteEnvironmentVar(position) } | ||
) | ||
rvEnvVars.layoutManager = LinearLayoutManager(context) | ||
rvEnvVars.adapter = envVarsAdapter | ||
} | ||
|
||
private fun setupAddButton() { | ||
btnAddEnvVar.setOnClickListener { | ||
showAddDialog() | ||
} | ||
} | ||
|
||
private fun showAddDialog() { | ||
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() | ||
|
||
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() | ||
} | ||
|
||
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) | ||
|
||
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() | ||
|
||
if (key.isNotEmpty()) { | ||
envVarsList[position] = EnvironmentVariable(key, value) | ||
envVarsAdapter.notifyItemChanged(position) | ||
saveEnvironmentVariables() | ||
} | ||
} | ||
.setNegativeButton("Cancel", null) | ||
.create() | ||
.show() | ||
} | ||
|
||
private fun deleteEnvironmentVar(position: Int) { | ||
envVarsList.removeAt(position) | ||
envVarsAdapter.notifyItemRemoved(position) | ||
saveEnvironmentVariables() | ||
} | ||
} | ||
|
||
data class EnvironmentVariable(val key: String, val value: String) | ||
|
||
class EnvironmentVarsAdapter( | ||
private val envVars: List<EnvironmentVariable>, | ||
private val onItemClick: (Int) -> Unit, | ||
private val onDeleteClick: (Int) -> Unit | ||
) : RecyclerView.Adapter<EnvironmentVarsAdapter.ViewHolder>() { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(R.layout.item_env_vars, parent, false) | ||
return ViewHolder(view, onItemClick, onDeleteClick) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind(envVars[position]) | ||
} | ||
|
||
override fun getItemCount() = envVars.size | ||
|
||
class ViewHolder( | ||
itemView: View, | ||
private val onItemClick: (Int) -> Unit, | ||
private val onDeleteClick: (Int) -> Unit | ||
) : RecyclerView.ViewHolder(itemView) { | ||
private val tvKey: TextView = itemView.findViewById(R.id.tvEnvVarKey) | ||
private val tvValue: TextView = itemView.findViewById(R.id.tvEnvVarValue) | ||
private val btnDelete: ImageButton = itemView.findViewById(R.id.btnDeleteEnvVar) | ||
|
||
init { | ||
itemView.setOnClickListener { onItemClick(adapterPosition) } | ||
btnDelete.setOnClickListener { onDeleteClick(adapterPosition) } | ||
} | ||
|
||
fun bind(envVar: EnvironmentVariable) { | ||
tvKey.text = envVar.key | ||
tvValue.text = envVar.value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:padding="16dp"> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:hint="Key"> | ||
|
||
<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> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:hint="Value"> | ||
|
||
<com.google.android.material.textfield.TextInputEditText | ||
android:id="@+id/etDialogValue" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:inputType="text"/> | ||
</com.google.android.material.textfield.TextInputLayout> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:padding="16dp" | ||
tools:context=".fragments.EnvironmentVarsSettingsFragment"> | ||
|
||
<Button | ||
android:id="@+id/btnAddEnvVar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="Add Environment Variable"/> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/rvEnvVars" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_marginTop="16dp"/> | ||
|
||
</LinearLayout> |
Oops, something went wrong.