From 6e68ab19f9412924f406cf0681c8e8262a5571d4 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 4 Oct 2020 07:05:40 +0530 Subject: [PATCH] Convert SavedState to Kotlin and use the Parcelize annotation. --- .../fragments/list/BaseListFragment.java | 2 +- .../newpipe/local/dialog/PlaylistDialog.java | 2 +- .../org/schabi/newpipe/util/SavedState.kt | 25 +++++++ .../org/schabi/newpipe/util/StateSaver.java | 75 +------------------ 4 files changed, 28 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/SavedState.kt diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index 41263bc34..5252024c2 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -48,7 +48,7 @@ public abstract class BaseListFragment extends BaseStateFragment implements ListViewContract, StateSaver.WriteRead, SharedPreferences.OnSharedPreferenceChangeListener { private static final int LIST_MODE_UPDATE_FLAG = 0x32; - protected StateSaver.SavedState savedState; + protected org.schabi.newpipe.util.SavedState savedState; private boolean useDefaultStateSaving = true; private int updateFlags = 0; diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java index 9ca8733cc..2c2987e95 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java @@ -17,7 +17,7 @@ import java.util.Queue; public abstract class PlaylistDialog extends DialogFragment implements StateSaver.WriteRead { private List streamEntities; - private StateSaver.SavedState savedState; + private org.schabi.newpipe.util.SavedState savedState; protected void setInfo(final List entities) { this.streamEntities = entities; diff --git a/app/src/main/java/org/schabi/newpipe/util/SavedState.kt b/app/src/main/java/org/schabi/newpipe/util/SavedState.kt new file mode 100644 index 000000000..313d56192 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/SavedState.kt @@ -0,0 +1,25 @@ +package org.schabi.newpipe.util + +import android.os.Parcelable +import kotlinx.android.parcel.Parcelize + +/** + * Information about the saved state on the disk. + */ +@Parcelize +class SavedState( + /** + * Get the prefix of the saved file. + * + * @return the file prefix + */ + val prefixFileSaved: String, + /** + * Get the path to the saved file. + * + * @return the path to the saved file + */ + val pathFileSaved: String +) : Parcelable { + override fun toString() = "$prefixFileSaved > $pathFileSaved" +} diff --git a/app/src/main/java/org/schabi/newpipe/util/StateSaver.java b/app/src/main/java/org/schabi/newpipe/util/StateSaver.java index ba095a4c5..ce31be1e3 100644 --- a/app/src/main/java/org/schabi/newpipe/util/StateSaver.java +++ b/app/src/main/java/org/schabi/newpipe/util/StateSaver.java @@ -22,8 +22,6 @@ package org.schabi.newpipe.util; import android.content.Context; import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; import android.text.TextUtils; import android.util.Log; @@ -288,7 +286,7 @@ public final class StateSaver { Log.d(TAG, "onDestroy() called with: savedState = [" + savedState + "]"); } - if (savedState != null && !TextUtils.isEmpty(savedState.getPathFileSaved())) { + if (savedState != null && !savedState.getPathFileSaved().isEmpty()) { STATE_OBJECTS_HOLDER.remove(savedState.getPrefixFileSaved()); try { //noinspection ResultOfMethodCallIgnored @@ -348,75 +346,4 @@ public final class StateSaver { */ void readFrom(@NonNull Queue savedObjects) throws Exception; } - - /*////////////////////////////////////////////////////////////////////////// - // Inner - //////////////////////////////////////////////////////////////////////////*/ - - /** - * Information about the saved state on the disk. - */ - public static class SavedState implements Parcelable { - @SuppressWarnings("unused") - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { - @Override - public SavedState createFromParcel(final Parcel in) { - return new SavedState(in); - } - - @Override - public SavedState[] newArray(final int size) { - return new SavedState[size]; - } - }; - private final String prefixFileSaved; - private final String pathFileSaved; - - public SavedState(final String prefixFileSaved, final String pathFileSaved) { - this.prefixFileSaved = prefixFileSaved; - this.pathFileSaved = pathFileSaved; - } - - protected SavedState(final Parcel in) { - prefixFileSaved = in.readString(); - pathFileSaved = in.readString(); - } - - @Override - public String toString() { - return getPrefixFileSaved() + " > " + getPathFileSaved(); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - dest.writeString(prefixFileSaved); - dest.writeString(pathFileSaved); - } - - /** - * Get the prefix of the saved file. - * - * @return the file prefix - */ - public String getPrefixFileSaved() { - return prefixFileSaved; - } - - /** - * Get the path to the saved file. - * - * @return the path to the saved file - */ - public String getPathFileSaved() { - return pathFileSaved; - } - } - - }