Skip to content

Commit

Permalink
all: better progress dialog (fixes #1964) (#1965)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored May 1, 2024
1 parent 6f57af1 commit 9d03239
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.treehouses.remote.fragments.dialogfragments

import android.app.AlertDialog
import android.app.Dialog
import android.app.ProgressDialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
Expand All @@ -25,8 +24,8 @@ import io.treehouses.remote.databinding.ActivityRpiDialogFragmentBinding
import io.treehouses.remote.pojo.DeviceInfo
import io.treehouses.remote.ui.home.HomeViewModel
import io.treehouses.remote.utils.DialogUtils
import io.treehouses.remote.utils.DialogUtils.CustomProgressDialog
import io.treehouses.remote.utils.logD
import java.util.*

class RPIDialogFragment : BaseDialogFragment(), DeviceDeleteListener {
private val raspberryDevices: MutableList<BluetoothDevice> = ArrayList()
Expand All @@ -37,7 +36,7 @@ class RPIDialogFragment : BaseDialogFragment(), DeviceDeleteListener {
private var mDialog: AlertDialog? = null
private val raspberryDevicesText: MutableList<DeviceInfo> = ArrayList()
private val allDevicesText: MutableList<DeviceInfo> = ArrayList()
private var pDialog: ProgressDialog? = null
private var pDialog: CustomProgressDialog? = null

private val viewModel: HomeViewModel by viewModels(ownerProducer = { requireParentFragment() })

Expand Down Expand Up @@ -100,7 +99,7 @@ class RPIDialogFragment : BaseDialogFragment(), DeviceDeleteListener {
}

private fun initDialog() {
pDialog = ProgressDialog(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
pDialog = CustomProgressDialog(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
mDialog = getAlertDialog(bind!!.root)
mDialog!!.setTitle(R.string.select_device)
listViewOnClickListener(bind!!.listView)
Expand Down Expand Up @@ -212,10 +211,7 @@ class RPIDialogFragment : BaseDialogFragment(), DeviceDeleteListener {
private fun addToDialog(device: BluetoothDevice, textList: MutableList<DeviceInfo>, mDevices: MutableList<BluetoothDevice>, inRange: Boolean) {
if (!mDevices.contains(device)) {
mDevices.add(device)
textList.add(DeviceInfo("""
${device.name}
${device.address}
""".trimIndent(), pairedDevices!!.contains(device), inRange))
textList.add(DeviceInfo("${device.name}\n ${device.address}".trimIndent(), pairedDevices!!.contains(device), inRange))
} else textList[mDevices.indexOf(device)].isInRange = true
mArrayAdapter!!.notifyDataSetChanged()
}
Expand Down
22 changes: 10 additions & 12 deletions app/src/main/kotlin/io/treehouses/remote/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.treehouses.remote.ui.home

import android.app.AlertDialog
import android.app.ProgressDialog
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -31,6 +30,8 @@ import io.treehouses.remote.callback.NotificationCallback
import io.treehouses.remote.databinding.ActivityHomeFragmentBinding
import io.treehouses.remote.pojo.enum.Resource
import io.treehouses.remote.pojo.enum.Status
import io.treehouses.remote.utils.DialogUtils
import io.treehouses.remote.utils.DialogUtils.CustomProgressDialog
import io.treehouses.remote.utils.SaveUtils
import io.treehouses.remote.utils.Utils
import io.treehouses.remote.utils.Utils.toast
Expand All @@ -42,7 +43,7 @@ class HomeFragment : BaseHomeFragment() {
/**
* Dialog to show that a Network Configuration is being switched
*/
private var progressDialog: ProgressDialog? = null
private var progressDialog: CustomProgressDialog? = null

/**
* Test Connection Dialog
Expand All @@ -52,7 +53,7 @@ class HomeFragment : BaseHomeFragment() {
/**
* Bluetooth connection status dialog
*/
private var connectionDialog: ProgressDialog? = null
private var connectionDialog: CustomProgressDialog? = null

private lateinit var bind: ActivityHomeFragmentBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
Expand Down Expand Up @@ -140,8 +141,10 @@ class HomeFragment : BaseHomeFragment() {
}
Status.LOADING -> {
if (it == null || it.data?.ssid == null) return@Observer
progressDialog = ProgressDialog.show(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle), "Connecting...", "Switching to " + it.data?.ssid, true)
progressDialog?.window!!.setBackgroundDrawableResource(android.R.color.transparent)
progressDialog = CustomProgressDialog(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
progressDialog?.setTitle("Connecting...")
progressDialog?.setMessage("Switching to ${it.data.ssid}")
progressDialog?.setIndeterminate(true)
progressDialog?.show()
}
}
Expand Down Expand Up @@ -268,14 +271,9 @@ class HomeFragment : BaseHomeFragment() {
* Show the connecting to Bluetooth dialog
*/
private fun showBTConnectionDialog() {
connectionDialog = ProgressDialog(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
connectionDialog?.setProgressStyle(ProgressDialog.STYLE_SPINNER)
connectionDialog = CustomProgressDialog(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
connectionDialog?.setTitle("Connecting...")
connectionDialog?.setMessage("""
Device Name: ${viewModel.device?.name}
Device Address: ${viewModel.device?.address}
""".trimIndent())
connectionDialog?.window!!.setBackgroundDrawableResource(android.R.color.transparent)
connectionDialog?.setMessage("Device Name: ${viewModel.device?.name} \n Device Address: ${viewModel.device?.address}".trimIndent())
connectionDialog?.show()
}

Expand Down
84 changes: 84 additions & 0 deletions app/src/main/kotlin/io/treehouses/remote/utils/DialogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package io.treehouses.remote.utils
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import io.treehouses.remote.R
import io.treehouses.remote.databinding.DialogProgressBinding

object DialogUtils {

Expand Down Expand Up @@ -63,4 +67,84 @@ object DialogUtils {
.setNegativeButton(negLabel, negCallback)
.show().window!!.setBackgroundDrawableResource(android.R.color.transparent)
}

class CustomProgressDialog(context: Context) {
private val binding: DialogProgressBinding = DialogProgressBinding.inflate(LayoutInflater.from(context))
private val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context)
private val progressBar = binding.progressBar
private val progressText = binding.progressText
private val progressTitle = binding.progressTitle
private var dialog: AlertDialog? = null
private var positiveButtonAction: (() -> Unit)? = null
private var negativeButtonAction: (() -> Unit)? = null

init {
dialogBuilder.setView(binding.root)
dialogBuilder.setCancelable(false)
binding.buttonPositive.setOnClickListener {
positiveButtonAction?.invoke()
}
binding.buttonNegative.setOnClickListener {
negativeButtonAction?.invoke()
}
}
fun setPositiveButton(text: String, isVisible: Boolean = true, listener: () -> Unit) {
binding.buttonPositive.text = text
positiveButtonAction = listener
binding.buttonPositive.visibility = if (isVisible) View.VISIBLE else View.GONE
}

fun setNegativeButton(text: String = "Cancel", isVisible: Boolean = true, listener: () -> Unit) {
binding.buttonNegative.text = text
negativeButtonAction = listener
binding.buttonNegative.visibility = if (isVisible) View.VISIBLE else View.GONE
}

fun show() {
if (dialog == null) {
dialog = dialogBuilder.create()
dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
}
dialog?.show()
dialog?.show()
}

fun dismiss() {
dialog?.dismiss()
}

fun setCancelable(state: Boolean) {
dialog?.setCancelable(state)
}

fun setIndeterminate(isIndeterminate: Boolean) {
progressBar.isIndeterminate = isIndeterminate
}

fun setMax(maxValue: Int) {
progressBar.max = maxValue
}

fun setProgress(value: Int) {
setIndeterminate(false)
progressBar.progress = value
}

fun setMessage(text: String) {
progressText.text = text
progressText.visibility = View.VISIBLE
}

fun setTitle(text: String?) {
progressTitle.visibility = View.VISIBLE
progressTitle.text = text
}
fun isShowing(): Boolean {
return dialog?.isShowing ?: false
}

fun disableNegativeButton() {
binding.buttonNegative.isEnabled = false
}
}
}
58 changes: 58 additions & 0 deletions app/src/main/res/layout/dialog_progress.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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="20dp">

<TextView
android:id="@+id/progressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAlignment="center"
android:textStyle="bold"
android:visibility="gone" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTint="@color/colorAccent"
android:max="100"
android:progress="0" />

<TextView
android:id="@+id/progressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:visibility="gone" />

<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="end"
android:orientation="horizontal">

<Button
android:id="@+id/buttonNegative"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

<Button
android:id="@+id/buttonPositive"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

</LinearLayout>
</LinearLayout>

0 comments on commit 9d03239

Please sign in to comment.