New ViewModel for loading feed preferences across lifecycle

This commit is contained in:
Joe Stein 2019-07-02 18:33:23 +02:00
parent 082a452156
commit 3b6cfb711f
5 changed files with 57 additions and 27 deletions

View File

@ -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"

View File

@ -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<Feed>) 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());

View File

@ -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() {

View File

@ -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<Feed> getFeed(long feedId) {
if (feed == null) {
return loadFeed(feedId);
} else {
return Maybe.just(feed);
}
}
private Maybe<Feed> loadFeed(long feedId) {
return Maybe.create(emitter -> {
Feed feed = DBReader.getFeed(feedId);
if (feed != null) {
this.feed = feed;
emitter.onSuccess(feed);
} else {
emitter.onComplete();
}
});
}
}

View File

@ -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"