clear latest visit storage

This commit is contained in:
cgspine
2019-08-06 16:26:43 +08:00
parent e41fb39249
commit 9df2e15b5b
5 changed files with 43 additions and 6 deletions
@@ -72,6 +72,7 @@ class InnerBaseActivity extends AppCompatActivity implements LatestVisitArgument
private void checkLatestVisitRecord() {
Class<? extends InnerBaseActivity> cls = getClass();
if (!cls.isAnnotationPresent(LatestVisitRecord.class)) {
QMUILatestVisit.getInstance(this).clearActivityLatestVisitRecord();
return;
}
QMUILatestVisit.getInstance(this).performLatestVisitRecord(this);
@@ -152,9 +152,11 @@ public abstract class QMUIFragment extends Fragment implements
private void checkLatestVisitRecord() {
Class<? extends QMUIFragment> cls = getClass();
Activity activity = getActivity();
if (!cls.isAnnotationPresent(LatestVisitRecord.class)
|| getParentFragment() != null
|| !(activity instanceof QMUIFragmentActivity)) {
if(getParentFragment() != null || !(activity instanceof QMUIFragmentActivity)){
return;
}
if (!cls.isAnnotationPresent(LatestVisitRecord.class)) {
QMUILatestVisit.getInstance(getContext()).clearFragmentLatestVisitRecord();
return;
}
if (!activity.getClass().isAnnotationPresent(LatestVisitRecord.class)) {
@@ -172,6 +172,14 @@ public class QMUILatestVisit {
return bundle;
}
void clearFragmentLatestVisitRecord(){
getStorage().clearFragmentStorage();
}
void clearActivityLatestVisitRecord(){
getStorage().clearActivityStorage();
}
void performLatestVisitRecord(QMUIFragment fragment) {
getStorage().saveFragmentRecordInfo(getRecordInfo(fragment.getClass(), fragment));
}
@@ -112,6 +112,24 @@ public class DefaultLatestVisitStorage implements QMUILatestVisitStorage {
return null;
}
@Override
public void clearFragmentStorage() {
mLastFragmentRecord = null;
SharedPreferences.Editor editor = sp.edit();
editor.remove(SP_FRAGMENT_RECORD_ID);
clearArgument(editor, SP_FRAGMENT_ARG_PREFIX);
editor.apply();
}
@Override
public void clearActivityStorage() {
mLastActivityRecord = null;
SharedPreferences.Editor editor = sp.edit();
editor.remove(SP_ACTIVITY_RECORD_ID);
clearArgument(editor, SP_ACTIVITY_ARG_PREFIX);
editor.apply();
}
@Override
public void saveFragmentRecordInfo(@NonNull RecordInfo recordInfo) {
if (mLastFragmentRecord != null && mLastFragmentRecord.equals(recordInfo)) {
@@ -138,14 +156,18 @@ public class DefaultLatestVisitStorage implements QMUILatestVisitStorage {
editor.apply();
}
private void putArguments(SharedPreferences.Editor editor,
String prefix, RecordInfo.Argument[] arguments) {
// clear first
private void clearArgument(SharedPreferences.Editor editor, String prefix) {
for (String key : sp.getAll().keySet()) {
if (key.startsWith(prefix)) {
editor.remove(key);
}
}
}
private void putArguments(SharedPreferences.Editor editor,
String prefix, RecordInfo.Argument[] arguments) {
// clear first
clearArgument(editor, prefix);
if (arguments != null && arguments.length > 0) {
for (RecordInfo.Argument argument : arguments) {
@@ -21,6 +21,8 @@ public interface QMUILatestVisitStorage {
Boolean getFragmentBoolArgument(String key);
void clearFragmentStorage();
// Activity Stuff
void saveActivityRecordInfo(@NonNull RecordInfo recordInfo);
@@ -36,4 +38,6 @@ public interface QMUILatestVisitStorage {
Float getActivityFloatArgument(String key);
Boolean getActivityBoolArgument(String key);
void clearActivityStorage();
}