From 3b6cfb711faa7560b0139cd72c15020a583e0cb6 Mon Sep 17 00:00:00 2001 From: Joe Stein Date: Tue, 2 Jul 2019 18:33:23 +0200 Subject: [PATCH] New ViewModel for loading feed preferences across lifecycle --- app/build.gradle | 5 ++++ .../activity/FeedSettingsActivity.java | 21 +++---------- .../fragment/FeedSettingsFragment.java | 27 ++++++++++------- .../viewmodel/FeedSettingsViewModel.java | 30 +++++++++++++++++++ build.gradle | 1 + 5 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/viewmodel/FeedSettingsViewModel.java diff --git a/app/build.gradle b/app/build.gradle index b4812e083..a9cce57d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -139,6 +139,11 @@ dependencies { implementation "com.android.support:gridlayout-v7:$supportVersion" implementation "com.android.support:recyclerview-v7:$supportVersion" compileOnly 'com.google.android.wearable:wearable:2.2.0' + + // ViewModel and LiveData + implementation "android.arch.lifecycle:extensions:$lifecycle_version" + annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" + implementation "org.apache.commons:commons-lang3:$commonslangVersion" implementation("org.shredzone.flattr4j:flattr4j-core:$flattr4jVersion") { exclude group: "org.json", module: "json" diff --git a/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java index fd526d1db..fbd19f88a 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.activity; +import android.arch.lifecycle.ViewModelProviders; import android.graphics.LightingColorFilter; import android.os.Bundle; import android.support.v4.app.FragmentManager; @@ -7,7 +8,6 @@ import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; - import android.view.MenuItem; import android.view.View; import android.widget.ImageView; @@ -19,10 +19,8 @@ import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.FastBlurTransformation; import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.fragment.FeedSettingsFragment; -import io.reactivex.Maybe; -import io.reactivex.MaybeOnSubscribe; +import de.danoeh.antennapod.viewmodel.FeedSettingsViewModel; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; @@ -46,7 +44,6 @@ public class FeedSettingsActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.feedsettings); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1); imgvCover = findViewById(R.id.imgvCover); txtvTitle = findViewById(R.id.txtvTitle); @@ -57,14 +54,8 @@ public class FeedSettingsActivity extends AppCompatActivity { // https://github.com/bumptech/glide/issues/529 imgvBackground.setColorFilter(new LightingColorFilter(0xff828282, 0x000000)); - disposable = Maybe.create((MaybeOnSubscribe) emitter -> { - Feed feed = DBReader.getFeed(feedId); - if (feed != null) { - emitter.onSuccess(feed); - } else { - emitter.onComplete(); - } - }) + long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1); + disposable = ViewModelProviders.of(this).get(FeedSettingsViewModel.class).getFeed(feedId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { @@ -91,10 +82,6 @@ public class FeedSettingsActivity extends AppCompatActivity { fragmentTransaction.commit(); } - public Feed getFeed() { - return feed; - } - private void showHeader() { txtvTitle.setText(feed.getTitle()); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java index a20384086..4fb3d90f5 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.fragment; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; @@ -7,7 +8,6 @@ import android.support.v14.preference.SwitchPreference; import android.support.v7.preference.ListPreference; import android.support.v7.preference.PreferenceFragmentCompat; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.activity.FeedSettingsActivity; import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedFilter; @@ -16,6 +16,9 @@ import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.dialog.AuthenticationDialog; import de.danoeh.antennapod.dialog.EpisodeFilterDialog; +import de.danoeh.antennapod.viewmodel.FeedSettingsViewModel; + +import static de.danoeh.antennapod.activity.FeedSettingsActivity.EXTRA_FEED_ID; public class FeedSettingsFragment extends PreferenceFragmentCompat { private static final CharSequence PREF_EPISODE_FILTER = "episodeFilter"; @@ -26,17 +29,21 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.feed_settings); - feed = ((FeedSettingsActivity) getActivity()).getFeed(); - feedPreferences = feed.getPreferences(); + long feedId = getArguments().getLong(EXTRA_FEED_ID); + ViewModelProviders.of(getActivity()).get(FeedSettingsViewModel.class).getFeed(feedId) + .subscribe(result -> { + feed = result; + feedPreferences = feed.getPreferences(); - setupAutoDownloadPreference(); - setupKeepUpdatedPreference(); - setupAutoDeletePreference(); - setupAuthentificationPreference(); - setupEpisodeFilterPreference(); + setupAutoDownloadPreference(); + setupKeepUpdatedPreference(); + setupAutoDeletePreference(); + setupAuthentificationPreference(); + setupEpisodeFilterPreference(); - updateAutoDeleteSummary(); - updateAutoDownloadEnabled(); + updateAutoDeleteSummary(); + updateAutoDownloadEnabled(); + }).dispose(); } private void setupEpisodeFilterPreference() { diff --git a/app/src/main/java/de/danoeh/antennapod/viewmodel/FeedSettingsViewModel.java b/app/src/main/java/de/danoeh/antennapod/viewmodel/FeedSettingsViewModel.java new file mode 100644 index 000000000..fe11a645c --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/viewmodel/FeedSettingsViewModel.java @@ -0,0 +1,30 @@ +package de.danoeh.antennapod.viewmodel; + +import android.arch.lifecycle.ViewModel; +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.storage.DBReader; +import io.reactivex.Maybe; + +public class FeedSettingsViewModel extends ViewModel { + private Feed feed; + + public Maybe getFeed(long feedId) { + if (feed == null) { + return loadFeed(feedId); + } else { + return Maybe.just(feed); + } + } + + private Maybe loadFeed(long feedId) { + return Maybe.create(emitter -> { + Feed feed = DBReader.getFeed(feedId); + if (feed != null) { + this.feed = feed; + emitter.onSuccess(feed); + } else { + emitter.onComplete(); + } + }); + } +} diff --git a/build.gradle b/build.gradle index b2fdaff8a..797e7c520 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,7 @@ project.ext { targetSdkVersion = 26 supportVersion = "27.1.1" + lifecycle_version = "1.1.1" workManagerVersion = "1.0.1" awaitilityVersion = "3.1.2" commonsioVersion = "2.5"