Compare commits

...

2 commits

Author SHA1 Message Date
Peter Cai b2fb3d23cb ui: Usb should use its own refresh progress bar
All checks were successful
/ build-debug (push) Successful in 4m44s
2024-07-06 18:36:58 -04:00
Peter Cai 388f15a531 ui: Scroll to USB fragment when inserted while app is active 2024-07-06 18:29:01 -04:00
3 changed files with 31 additions and 20 deletions

View file

@ -50,22 +50,12 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
}
}
var loading: Boolean
get() = loadingProgress.visibility == View.VISIBLE
set(value) {
loadingProgress.visibility = if (value) {
View.VISIBLE
} else {
View.GONE
}
}
protected lateinit var tm: TelephonyManager
private val usbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == UsbManager.ACTION_USB_DEVICE_ATTACHED || intent?.action == UsbManager.ACTION_USB_DEVICE_DETACHED) {
refresh()
refresh(true)
}
}
}
@ -121,8 +111,8 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
}
}
private suspend fun init() {
loading = true
private suspend fun init(fromUsbEvent: Boolean = false) {
loadingProgress.visibility = View.VISIBLE
viewPager.visibility = View.GONE
tabs.visibility = View.GONE
@ -142,7 +132,7 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
}
withContext(Dispatchers.Main) {
loading = false
loadingProgress.visibility = View.GONE
knownChannels.sortedBy { it.logicalSlotId }.forEach { channel ->
pages.add(Page(
@ -165,19 +155,28 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
pages.add(Page("") { appContainer.uiComponentFactory.createNoEuiccPlaceholderFragment() })
pagerAdapter.notifyDataSetChanged()
}
if (fromUsbEvent && usbDevice != null) {
// If this refresh was triggered by a USB insertion while active, scroll to that page
viewPager.post {
viewPager.setCurrentItem(pages.size - 1, true)
}
} else {
viewPager.currentItem = 0
}
}
}
private fun refresh() {
private fun refresh(fromUsbEvent: Boolean = false) {
lifecycleScope.launch {
loading = true
loadingProgress.visibility = View.VISIBLE
viewPager.visibility = View.GONE
tabs.visibility = View.GONE
pages.clear()
pagerAdapter.notifyDataSetChanged()
init()
init(fromUsbEvent)
}
}
}

View file

@ -14,6 +14,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
@ -69,6 +70,7 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
private lateinit var text: TextView
private lateinit var permissionButton: Button
private lateinit var loadingProgress: ProgressBar
private var usbDevice: UsbDevice? = null
private var usbChannel: EuiccChannel? = null
@ -82,6 +84,7 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
text = view.requireViewById(R.id.usb_reader_text)
permissionButton = view.requireViewById(R.id.usb_grant_permission)
loadingProgress = view.requireViewById(R.id.loading)
permissionButton.setOnClickListener {
usbManager.requestPermission(usbDevice, usbPendingIntent)
@ -135,14 +138,13 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
private suspend fun tryLoadUsbChannel() {
text.visibility = View.GONE
permissionButton.visibility = View.GONE
(requireActivity() as MainActivity).loading = true
loadingProgress.visibility = View.VISIBLE
val (device, channel) = withContext(Dispatchers.IO) {
euiccChannelManager.enumerateUsbEuiccChannel()
}
(requireActivity() as MainActivity).loading = false
loadingProgress.visibility = View.GONE
usbDevice = device
usbChannel = channel

View file

@ -4,6 +4,16 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/usb_reader_text"
android:layout_width="match_parent"