Skip to content

Commit

Permalink
Convert ProgressFragment to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Oct 31, 2023
1 parent 9e4ea1d commit 9bbe78d
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 168 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ dependencies {

// AndroidX
implementation libs.androidx.appcompat
//implementation libs.androidx.core
implementation libs.androidx.constraintlayout
implementation libs.androidx.lifecycle.process
implementation libs.androidx.preference
Expand Down Expand Up @@ -149,7 +150,7 @@ dependencies {
// Unit Testing
testImplementation project(':shared-test')
testImplementation libs.junit4
testImplementation libs.androidx.core
testImplementation libs.androidx.test.core
testImplementation libs.androidx.runner
testImplementation libs.androidx.rules
testImplementation libs.mockito.core
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.openobservatory.ooniprobe.fragment

import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.RunningActivity
import org.openobservatory.ooniprobe.common.Application
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.TestProgressRepository
import org.openobservatory.ooniprobe.common.service.RunTestService
import org.openobservatory.ooniprobe.databinding.FragmentProgressBinding
import org.openobservatory.ooniprobe.receiver.TestRunBroadRequestReceiver
import javax.inject.Inject

/**
* Monitors and displays progress of [RunTestService].
*/
class ProgressFragment : Fragment() {
private lateinit var receiver: TestRunBroadRequestReceiver
private lateinit var biding: FragmentProgressBinding

@Inject
lateinit var preferenceManager: PreferenceManager

@Inject
lateinit var testProgressRepository: TestProgressRepository

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
biding = FragmentProgressBinding.inflate(inflater, container, false)
(requireActivity().application as Application).fragmentComponent.inject(this)
biding.root.setOnClickListener { _: View? ->
val intent = Intent(context, RunningActivity::class.java)
ActivityCompat.startActivity(requireContext(), intent, null)
}
testProgressRepository.progress.observe(viewLifecycleOwner) { progressValue: Int? ->
if (progressValue != null) {
biding.progress.progress = progressValue
}
}
return biding.root
}

override fun onResume() {
super.onResume()
val filter = IntentFilter("org.openobservatory.ooniprobe.activity.RunningActivity")
receiver = TestRunBroadRequestReceiver(
preferenceManager, TestRunnerEventListener(), testProgressRepository
)
// NOTE: Simple update to ContextCompat#registerReceiver not possible at the moment.
LocalBroadcastManager.getInstance(requireContext()).registerReceiver(receiver, filter)
bindTestService()
}

fun bindTestService() {
if ((requireActivity().application as Application).isTestRunning) {
requireContext().bindService(
Intent(requireContext(), RunTestService::class.java),
receiver,
Context.BIND_AUTO_CREATE
)
biding.progressLayout.visibility = View.VISIBLE
} else {
biding.progressLayout.visibility = View.GONE
}
}

private fun updateUI(service: RunTestService?) {
if ((requireActivity().application as Application).isTestRunning) {
val progressLevel = testProgressRepository.progress.value
when {
progressLevel != null -> {
biding.progress.progress = progressLevel
}
else -> {
biding.progress.isIndeterminate = true
}
}
service?.task?.let { task ->
task.currentSuite?.let {
biding.progress.max = service.task.getMax(preferenceManager)
}
task.currentTest?.let {
biding.name.text = getString(it.labelResId)
}
}
}
}

override fun onPause() {
super.onPause()
if (receiver.isBound) {
requireContext().unbindService(receiver)
receiver.isBound = false
}
LocalBroadcastManager.getInstance(requireContext()).unregisterReceiver(receiver)
}

override fun onDestroy() {
super.onDestroy()
LocalBroadcastManager.getInstance(requireContext()).unregisterReceiver(receiver)
}

private inner class TestRunnerEventListener : TestRunBroadRequestReceiver.EventListener {
override fun onStart(service: RunTestService) = updateUI(service)

override fun onRun(value: String) {
biding.name.text = value
}

override fun onProgress(state: Int, eta: Double) {
if (biding.progress.isIndeterminate) {
updateUI(receiver.service)
}
biding.progress.apply {
isIndeterminate = false
progress = state
}
}

override fun onLog(value: String) {/* nothing */
}

override fun onError(value: String) {/* nothing */
}

override fun onUrl() {
biding.progress.isIndeterminate = false
}

override fun onInterrupt() {
biding.running.text = getString(R.string.Dashboard_Running_Stopping_Title)
}

override fun onEnd(context: Context) {
biding.progressLayout.visibility = View.GONE
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_progress.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:id="@+id/progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_gray9"
android:background="@color/progress_background"
tools:context=".fragment.ProgressFragment">
<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -49,4 +49,4 @@
</LinearLayout>
</LinearLayout>

</FrameLayout>
</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<color name="card_boarder">#868e96</color>
<color name="bottom_navigation_state_checked">@color/color_black</color>
<color name="bottom_navigation_state_unchecked">#DEE2E6</color>
<color name="progress_background">#E9ECEF</color>

<color name="color_black">#fff</color>
<color name="color_white">#1b1b1b</color>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<color name="card_boarder">@color/color_gray3</color>
<color name="bottom_navigation_state_checked">@color/color_black</color>
<color name="bottom_navigation_state_unchecked">@color/color_gray9</color>
<color name="progress_background">@color/color_gray7</color>

<color name="color_black">#000000</color>
<color name="color_white">#ffffff</color>
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ dbflow-lib = { module = "com.github.Raizlabs.DBFlow:dbflow", version.ref = "dbfl
dbflow-core = { module = "com.github.Raizlabs.DBFlow:dbflow-core", version.ref = "dbflow" }
dbflow-processor = { module = "com.github.Raizlabs.DBFlow:dbflow-processor", version.ref = "dbflow" }

androidx-core = { module = "androidx.test:core", version.ref = "androidxCore" }
# androidx-core = { group = "androidx.core", name = "core", version.ref = "androidxCore" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidxCore" }
androidx-rules = { module = "androidx.test:rules", version.ref = "androidxCore" }
androidx-runner = { module = "androidx.test:runner", version.ref = "androidxRunner" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" }
Expand Down

0 comments on commit 9bbe78d

Please sign in to comment.