Skip to content

Commit

Permalink
feat: update AutoUpdateDescriptorsWorker callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed May 27, 2024
1 parent 6f13727 commit d0b8841
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.ExistingWorkPolicy;
Expand Down Expand Up @@ -167,22 +168,39 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}

private void scheduleWorkers() {

PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 60, TimeUnit.MINUTES)
.setConstraints(
new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
).build();

WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(
AutoUpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 60, TimeUnit.MINUTES)
.setConstraints(
new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
).build()
periodicWorkRequest
);

WorkManager.getInstance(this)
.getWorkInfoByIdLiveData(periodicWorkRequest.getId())
.observe(this, this::onPeriodicWorkComplete);
// TODO(aanorbel): add rules before checking updates
fetchManualUpdate();
registerReviewLauncher(binding.bottomNavigation, () -> null);
}

private void onPeriodicWorkComplete(WorkInfo workInfo) {
if (workInfo!=null && workInfo.getState() == WorkInfo.State.SUCCEEDED) {
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (fragment instanceof DashboardFragment) {
((DashboardFragment) fragment).updateDescriptors();
}
}
}
}

public void fetchManualUpdate() {
OneTimeWorkRequest manualWorkRequest = new OneTimeWorkRequest.Builder(ManualUpdateDescriptorsWorker.class)
.setConstraints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,20 @@ class ManualUpdateDescriptorsWorker(
companion object {
@JvmField
var UPDATED_DESCRIPTORS_WORK_NAME =
"${AutoUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"
"${ManualUpdateDescriptorsWorker::class.java.name}.UPDATED_DESCRIPTORS_WORK_NAME"

private val TAG = AutoUpdateDescriptorsWorker::class.java.simpleName
private val TAG = ManualUpdateDescriptorsWorker::class.java.simpleName

private val UPDATE_DESCRIPTOR_CHANNEL: String =
AutoUpdateDescriptorsWorker::class.java.simpleName
ManualUpdateDescriptorsWorker::class.java.simpleName

@JvmField
var KEY_UPDATED_DESCRIPTORS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"
"${ManualUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"

@JvmField
var KEY_DESCRIPTOR_IDS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS"
"${ManualUpdateDescriptorsWorker::class.java.name}.KEY_DESCRIPTOR_IDS"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ class DashboardFragment : Fragment(), View.OnClickListener {
binding.recycler.adapter = adapter
}

viewModel.getItemList().observe(viewLifecycleOwner) { items ->
descriptors.apply {
clear()
addAll(items)
}
}
updateDescriptors()

testStateRepository.testGroupStatus.observe(viewLifecycleOwner) { status ->
if (status == TestGroupStatus.RUNNING) {
Expand Down Expand Up @@ -114,6 +109,10 @@ class DashboardFragment : Fragment(), View.OnClickListener {
&& preferenceManager.isWarnVPNInUse
) binding.vpn.visibility = View.VISIBLE else binding.vpn.visibility = View.GONE

updateDescriptors()
}

fun updateDescriptors() {
viewModel.getItemList().observe(viewLifecycleOwner) { items ->
descriptors.apply {
clear()
Expand Down

0 comments on commit d0b8841

Please sign in to comment.