Merge pull request #3254 from jas14/feed-rotation-crash-3245

New ViewModel for loading feed preferences across lifecycle
This commit is contained in:
H. Lehmann 2019-07-06 15:10:57 +02:00 committed by GitHub
commit 9e72780bc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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:gridlayout-v7:$supportVersion"
implementation "com.android.support:recyclerview-v7:$supportVersion" implementation "com.android.support:recyclerview-v7:$supportVersion"
compileOnly 'com.google.android.wearable:wearable:2.2.0' 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.apache.commons:commons-lang3:$commonslangVersion"
implementation("org.shredzone.flattr4j:flattr4j-core:$flattr4jVersion") { implementation("org.shredzone.flattr4j:flattr4j-core:$flattr4jVersion") {
exclude group: "org.json", module: "json" exclude group: "org.json", module: "json"

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.activity; package de.danoeh.antennapod.activity;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.LightingColorFilter; import android.graphics.LightingColorFilter;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
@ -7,7 +8,6 @@ import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ImageView; 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.ApGlideSettings;
import de.danoeh.antennapod.core.glide.FastBlurTransformation; import de.danoeh.antennapod.core.glide.FastBlurTransformation;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.fragment.FeedSettingsFragment; import de.danoeh.antennapod.fragment.FeedSettingsFragment;
import io.reactivex.Maybe; import de.danoeh.antennapod.viewmodel.FeedSettingsViewModel;
import io.reactivex.MaybeOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -46,7 +44,6 @@ public class FeedSettingsActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.feedsettings); setContentView(R.layout.feedsettings);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1);
imgvCover = findViewById(R.id.imgvCover); imgvCover = findViewById(R.id.imgvCover);
txtvTitle = findViewById(R.id.txtvTitle); txtvTitle = findViewById(R.id.txtvTitle);
@ -57,14 +54,8 @@ public class FeedSettingsActivity extends AppCompatActivity {
// https://github.com/bumptech/glide/issues/529 // https://github.com/bumptech/glide/issues/529
imgvBackground.setColorFilter(new LightingColorFilter(0xff828282, 0x000000)); imgvBackground.setColorFilter(new LightingColorFilter(0xff828282, 0x000000));
disposable = Maybe.create((MaybeOnSubscribe<Feed>) emitter -> { long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1);
Feed feed = DBReader.getFeed(feedId); disposable = ViewModelProviders.of(this).get(FeedSettingsViewModel.class).getFeed(feedId)
if (feed != null) {
emitter.onSuccess(feed);
} else {
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { .subscribe(result -> {
@ -91,10 +82,6 @@ public class FeedSettingsActivity extends AppCompatActivity {
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
public Feed getFeed() {
return feed;
}
private void showHeader() { private void showHeader() {
txtvTitle.setText(feed.getTitle()); txtvTitle.setText(feed.getTitle());

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.fragment; package de.danoeh.antennapod.fragment;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; 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.ListPreference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FeedSettingsActivity;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedFilter; 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.core.storage.DBWriter;
import de.danoeh.antennapod.dialog.AuthenticationDialog; import de.danoeh.antennapod.dialog.AuthenticationDialog;
import de.danoeh.antennapod.dialog.EpisodeFilterDialog; 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 { public class FeedSettingsFragment extends PreferenceFragmentCompat {
private static final CharSequence PREF_EPISODE_FILTER = "episodeFilter"; private static final CharSequence PREF_EPISODE_FILTER = "episodeFilter";
@ -26,17 +29,21 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.feed_settings); addPreferencesFromResource(R.xml.feed_settings);
feed = ((FeedSettingsActivity) getActivity()).getFeed(); long feedId = getArguments().getLong(EXTRA_FEED_ID);
feedPreferences = feed.getPreferences(); ViewModelProviders.of(getActivity()).get(FeedSettingsViewModel.class).getFeed(feedId)
.subscribe(result -> {
feed = result;
feedPreferences = feed.getPreferences();
setupAutoDownloadPreference(); setupAutoDownloadPreference();
setupKeepUpdatedPreference(); setupKeepUpdatedPreference();
setupAutoDeletePreference(); setupAutoDeletePreference();
setupAuthentificationPreference(); setupAuthentificationPreference();
setupEpisodeFilterPreference(); setupEpisodeFilterPreference();
updateAutoDeleteSummary(); updateAutoDeleteSummary();
updateAutoDownloadEnabled(); updateAutoDownloadEnabled();
}).dispose();
} }
private void setupEpisodeFilterPreference() { 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 targetSdkVersion = 26
supportVersion = "27.1.1" supportVersion = "27.1.1"
lifecycle_version = "1.1.1"
workManagerVersion = "1.0.1" workManagerVersion = "1.0.1"
awaitilityVersion = "3.1.2" awaitilityVersion = "3.1.2"
commonsioVersion = "2.5" commonsioVersion = "2.5"