|
| 1 | +package com.appcontrolx.ui |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.view.LayoutInflater |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import androidx.lifecycle.lifecycleScope |
| 8 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 9 | +import androidx.recyclerview.widget.RecyclerView |
| 10 | +import com.appcontrolx.R |
| 11 | +import com.appcontrolx.databinding.BottomSheetBatchProgressBinding |
| 12 | +import com.appcontrolx.databinding.ItemBatchAppBinding |
| 13 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment |
| 14 | +import kotlinx.coroutines.Job |
| 15 | +import kotlinx.coroutines.delay |
| 16 | +import kotlinx.coroutines.launch |
| 17 | + |
| 18 | +class BatchProgressBottomSheet : BottomSheetDialogFragment() { |
| 19 | + |
| 20 | + private var _binding: BottomSheetBatchProgressBinding? = null |
| 21 | + private val binding get() = _binding |
| 22 | + |
| 23 | + private var actionName = "" |
| 24 | + private var appNames = listOf<String>() |
| 25 | + private var packageNames = listOf<String>() |
| 26 | + private var onExecute: (suspend (String) -> Result<Unit>)? = null |
| 27 | + private var onComplete: (() -> Unit)? = null |
| 28 | + |
| 29 | + private val adapter = BatchAppAdapter() |
| 30 | + private var executionJob: Job? = null |
| 31 | + private var isCancelled = false |
| 32 | + |
| 33 | + companion object { |
| 34 | + const val TAG = "BatchProgressBottomSheet" |
| 35 | + private const val COUNTDOWN_SECONDS = 3 |
| 36 | + |
| 37 | + fun newInstance( |
| 38 | + actionName: String, |
| 39 | + appNames: List<String>, |
| 40 | + packageNames: List<String>, |
| 41 | + onExecute: suspend (String) -> Result<Unit>, |
| 42 | + onComplete: () -> Unit |
| 43 | + ): BatchProgressBottomSheet { |
| 44 | + return BatchProgressBottomSheet().apply { |
| 45 | + this.actionName = actionName |
| 46 | + this.appNames = appNames |
| 47 | + this.packageNames = packageNames |
| 48 | + this.onExecute = onExecute |
| 49 | + this.onComplete = onComplete |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
| 55 | + _binding = BottomSheetBatchProgressBinding.inflate(inflater, container, false) |
| 56 | + return _binding?.root |
| 57 | + } |
| 58 | + |
| 59 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 60 | + super.onViewCreated(view, savedInstanceState) |
| 61 | + setupUI() |
| 62 | + startCountdown() |
| 63 | + } |
| 64 | + |
| 65 | + private fun setupUI() { |
| 66 | + val b = binding ?: return |
| 67 | + |
| 68 | + b.tvAction.text = actionName.replace("_", " ") |
| 69 | + b.progressBar.max = packageNames.size |
| 70 | + b.progressBar.progress = 0 |
| 71 | + b.tvProgress.text = getString(R.string.batch_waiting) |
| 72 | + |
| 73 | + // Setup RecyclerView with app list |
| 74 | + adapter.submitList(appNames.mapIndexed { index, name -> |
| 75 | + BatchAppItem(name, packageNames[index], BatchStatus.PENDING) |
| 76 | + }) |
| 77 | + b.recyclerView.layoutManager = LinearLayoutManager(context) |
| 78 | + b.recyclerView.adapter = adapter |
| 79 | + |
| 80 | + b.btnCancel.setOnClickListener { |
| 81 | + isCancelled = true |
| 82 | + executionJob?.cancel() |
| 83 | + dismiss() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private fun startCountdown() { |
| 88 | + executionJob = lifecycleScope.launch { |
| 89 | + // Countdown |
| 90 | + for (i in COUNTDOWN_SECONDS downTo 1) { |
| 91 | + if (isCancelled) return@launch |
| 92 | + binding?.tvCountdown?.text = getString(R.string.batch_countdown, i) |
| 93 | + delay(1000) |
| 94 | + } |
| 95 | + |
| 96 | + binding?.tvCountdown?.text = "" |
| 97 | + executeActions() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private suspend fun executeActions() { |
| 102 | + val b = binding ?: return |
| 103 | + var successCount = 0 |
| 104 | + var failCount = 0 |
| 105 | + |
| 106 | + packageNames.forEachIndexed { index, pkg -> |
| 107 | + if (isCancelled) return |
| 108 | + |
| 109 | + b.tvProgress.text = getString(R.string.batch_progress, index + 1, packageNames.size) |
| 110 | + |
| 111 | + // Update status to processing |
| 112 | + adapter.updateStatus(index, BatchStatus.PROCESSING) |
| 113 | + |
| 114 | + val result = onExecute?.invoke(pkg) ?: Result.failure(Exception("No executor")) |
| 115 | + |
| 116 | + if (result.isSuccess) { |
| 117 | + successCount++ |
| 118 | + adapter.updateStatus(index, BatchStatus.SUCCESS) |
| 119 | + } else { |
| 120 | + failCount++ |
| 121 | + adapter.updateStatus(index, BatchStatus.FAILED) |
| 122 | + } |
| 123 | + |
| 124 | + b.progressBar.progress = index + 1 |
| 125 | + delay(100) // Small delay for UI |
| 126 | + } |
| 127 | + |
| 128 | + // Complete |
| 129 | + b.tvProgress.text = if (failCount == 0) { |
| 130 | + getString(R.string.batch_all_success, successCount) |
| 131 | + } else { |
| 132 | + getString(R.string.batch_partial_success, successCount, failCount) |
| 133 | + } |
| 134 | + b.btnCancel.text = getString(R.string.btn_close) |
| 135 | + b.btnCancel.setOnClickListener { |
| 136 | + onComplete?.invoke() |
| 137 | + dismiss() |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + override fun onDestroyView() { |
| 142 | + super.onDestroyView() |
| 143 | + _binding = null |
| 144 | + } |
| 145 | + |
| 146 | + // Data classes |
| 147 | + enum class BatchStatus { PENDING, PROCESSING, SUCCESS, FAILED } |
| 148 | + |
| 149 | + data class BatchAppItem( |
| 150 | + val appName: String, |
| 151 | + val packageName: String, |
| 152 | + var status: BatchStatus |
| 153 | + ) |
| 154 | + |
| 155 | + // Adapter |
| 156 | + inner class BatchAppAdapter : RecyclerView.Adapter<BatchAppAdapter.ViewHolder>() { |
| 157 | + private val items = mutableListOf<BatchAppItem>() |
| 158 | + |
| 159 | + fun submitList(list: List<BatchAppItem>) { |
| 160 | + items.clear() |
| 161 | + items.addAll(list) |
| 162 | + notifyDataSetChanged() |
| 163 | + } |
| 164 | + |
| 165 | + fun updateStatus(index: Int, status: BatchStatus) { |
| 166 | + if (index in items.indices) { |
| 167 | + items[index].status = status |
| 168 | + notifyItemChanged(index) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { |
| 173 | + val binding = ItemBatchAppBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
| 174 | + return ViewHolder(binding) |
| 175 | + } |
| 176 | + |
| 177 | + override fun onBindViewHolder(holder: ViewHolder, position: Int) { |
| 178 | + holder.bind(items[position]) |
| 179 | + } |
| 180 | + |
| 181 | + override fun getItemCount() = items.size |
| 182 | + |
| 183 | + inner class ViewHolder(private val binding: ItemBatchAppBinding) : RecyclerView.ViewHolder(binding.root) { |
| 184 | + fun bind(item: BatchAppItem) { |
| 185 | + binding.tvAppName.text = item.appName |
| 186 | + |
| 187 | + val (statusText, statusColor) = when (item.status) { |
| 188 | + BatchStatus.PENDING -> "-" to R.color.on_surface_secondary |
| 189 | + BatchStatus.PROCESSING -> "..." to R.color.status_neutral |
| 190 | + BatchStatus.SUCCESS -> getString(R.string.log_status_success) to R.color.status_positive |
| 191 | + BatchStatus.FAILED -> getString(R.string.log_status_failed) to R.color.status_negative |
| 192 | + } |
| 193 | + |
| 194 | + binding.tvStatus.text = statusText |
| 195 | + binding.tvStatus.setTextColor(resources.getColor(statusColor, null)) |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments