Skip to content

Commit

Permalink
feature: scheme handler now support refresh in current UI if matched.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Jun 6, 2020
1 parent 3aa1c0a commit 609af67
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 43 deletions.
Expand Up @@ -25,6 +25,7 @@
public @interface ActivityScheme {
String name();
String[] required() default {};
boolean useRefreshIfCurrentMatched() default false;
Class<?> customMatcher() default void.class;
Class<?> customFactory() default void.class;
String[] keysWithIntValue() default {};
Expand Down
Expand Up @@ -26,6 +26,7 @@
String name();
Class<?>[] activities();
String[] required() default {};
boolean useRefreshIfCurrentMatched() default false;
Class<?> customMatcher() default void.class;
boolean forceNewActivity() default false;
String forceNewActivityKey() default "";
Expand Down
Expand Up @@ -172,6 +172,8 @@ public int compare(Item item, Item t1) {
/**/.add("new $T(", ActivitySchemeItem)
/*---*/.add("$T.class", elementName)
/*---*/.add(",")
/*---*/.add("$L", annotation.useRefreshIfCurrentMatched())
/*---*/.add(",")
/*---*/.add(customFactory)
/*---*/.add(",")
/*---*/.add("required")
Expand Down Expand Up @@ -212,6 +214,8 @@ public int compare(Item item, Item t1) {
/**/.add("new $T(", FragmentSchemeItem)
/*---*/.add("$T.class", elementName)
/*---*/.add(",")
/*---*/.add("$L", annotation.useRefreshIfCurrentMatched())
/*---*/.add(",")
/*---*/.add(activities)
/*---*/.add(",")
/*---*/.add(customFactory)
Expand Down
8 changes: 7 additions & 1 deletion arch/src/main/java/com/qmuiteam/qmui/arch/QMUIActivity.java
Expand Up @@ -25,6 +25,7 @@
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qmuiteam.qmui.arch.scheme.ActivitySchemeRefreshable;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

Expand All @@ -41,7 +42,7 @@
import static com.qmuiteam.qmui.arch.SwipeBackLayout.EDGE_RIGHT;
import static com.qmuiteam.qmui.arch.SwipeBackLayout.EDGE_TOP;

public class QMUIActivity extends InnerBaseActivity {
public class QMUIActivity extends InnerBaseActivity implements ActivitySchemeRefreshable {
private static final String TAG = "QMUIActivity";
private SwipeBackLayout.ListenerRemover mListenerRemover;
private SwipeBackgroundView mSwipeBackgroundView;
Expand Down Expand Up @@ -328,4 +329,9 @@ public void finish() {
}
super.finish();
}

@Override
public void refreshFromScheme(@Nullable Intent intent) {

}
}
8 changes: 7 additions & 1 deletion arch/src/main/java/com/qmuiteam/qmui/arch/QMUIFragment.java
Expand Up @@ -61,6 +61,7 @@
import com.qmuiteam.qmui.arch.effect.QMUIFragmentResultEffectHandler;
import com.qmuiteam.qmui.arch.record.LatestVisitArgumentCollector;
import com.qmuiteam.qmui.arch.record.RecordArgumentEditor;
import com.qmuiteam.qmui.arch.scheme.FragmentSchemeRefreshable;
import com.qmuiteam.qmui.arch.scheme.QMUISchemeHandler;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIKeyboardHelper;
Expand Down Expand Up @@ -89,7 +90,7 @@
* Created by cgspine on 15/9/14.
*/
public abstract class QMUIFragment extends Fragment implements
QMUIFragmentLazyLifecycleOwner.Callback, LatestVisitArgumentCollector {
QMUIFragmentLazyLifecycleOwner.Callback, LatestVisitArgumentCollector, FragmentSchemeRefreshable {
static final String SWIPE_BACK_VIEW = "swipe_back_view";
private static final String TAG = QMUIFragment.class.getSimpleName();

Expand Down Expand Up @@ -1324,6 +1325,11 @@ private boolean isParentVisibleToUser() {
return true;
}

@Override
public void refreshFromScheme(@Nullable Bundle bundle) {

}

private void notifyFragmentVisibleToUserChanged(boolean isVisibleToUser) {
if (mLazyViewLifecycleOwner != null) {
mLazyViewLifecycleOwner.setViewVisible(isVisibleToUser);
Expand Down
Expand Up @@ -37,6 +37,7 @@ class ActivitySchemeItem extends SchemeItem {
private final Class<? extends QMUISchemeIntentFactory> mIntentFactoryCls;

public ActivitySchemeItem(@NonNull Class<? extends Activity> activityClass,
boolean useRefreshIfMatchedCurrent,
@Nullable Class<? extends QMUISchemeIntentFactory> intentFactoryCls,
@Nullable ArrayMap<String, String> required,
@Nullable String[] keysForInt,
Expand All @@ -45,7 +46,7 @@ public ActivitySchemeItem(@NonNull Class<? extends Activity> activityClass,
@Nullable String[] keysForFloat,
@Nullable String[] keysForDouble,
@Nullable Class<? extends QMUISchemeMatcher> schemeMatcherCls) {
super(required, keysForInt, keysForBool, keysForLong, keysForFloat, keysForDouble, schemeMatcherCls);
super(required, useRefreshIfMatchedCurrent, keysForInt, keysForBool, keysForLong, keysForFloat, keysForDouble, schemeMatcherCls);
mActivityClass = activityClass;
mIntentFactoryCls = intentFactoryCls;
}
Expand Down Expand Up @@ -79,7 +80,12 @@ public boolean handle(@NonNull QMUISchemeHandler handler,
}

Intent intent = factory.factory(activity, mActivityClass, scheme);
activity.startActivity(intent);

if(isUseRefreshIfMatchedCurrent() && mActivityClass == activity.getClass() && activity instanceof ActivitySchemeRefreshable){
((ActivitySchemeRefreshable) activity).refreshFromScheme(intent);
}else{
activity.startActivity(intent);
}
return true;
}
return false;
Expand Down
@@ -0,0 +1,25 @@
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.qmuiteam.qmui.arch.scheme;

import android.content.Intent;

import androidx.annotation.Nullable;

public interface ActivitySchemeRefreshable {
void refreshFromScheme(@Nullable Intent intent);
}
Expand Up @@ -18,10 +18,12 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.qmuiteam.qmui.QMUILog;
import com.qmuiteam.qmui.arch.QMUIFragment;
Expand All @@ -41,6 +43,7 @@ class FragmentSchemeItem extends SchemeItem {
private final Class<? extends QMUISchemeFragmentFactory> mFragmentFactoryCls;

public FragmentSchemeItem(@NonNull Class<? extends QMUIFragment> fragmentCls,
boolean useRefreshIfMatchedCurrent,
@NonNull Class<? extends QMUIFragmentActivity>[] activityClsList,
@Nullable Class<? extends QMUISchemeFragmentFactory> fragmentFactoryCls,
boolean forceNewActivity,
Expand All @@ -52,7 +55,7 @@ public FragmentSchemeItem(@NonNull Class<? extends QMUIFragment> fragmentCls,
@Nullable String[] keysForFloat,
@Nullable String[] keysForDouble,
@Nullable Class<? extends QMUISchemeMatcher> schemeMatcherCls) {
super(required, keysForInt, keysForBool, keysForLong, keysForFloat, keysForDouble, schemeMatcherCls);
super(required, useRefreshIfMatchedCurrent, keysForInt, keysForBool, keysForLong, keysForFloat, keysForDouble, schemeMatcherCls);
mFragmentCls = fragmentCls;
mActivityClsList = activityClsList;
mForceNewActivity = forceNewActivity;
Expand Down Expand Up @@ -105,14 +108,28 @@ public boolean handle(@NonNull QMUISchemeHandler handler,
}

QMUIFragmentActivity fragmentActivity = (QMUIFragmentActivity) activity;
QMUIFragment fragment = factory.factory(mFragmentCls, scheme);
if (fragment != null) {
Bundle args = factory.factory(scheme);
Fragment currentFragment = fragmentActivity.getCurrentFragment();
if(isUseRefreshIfMatchedCurrent()
&& currentFragment != null
&& currentFragment.getClass() == mFragmentCls
&& currentFragment instanceof FragmentSchemeRefreshable){
((FragmentSchemeRefreshable) currentFragment).refreshFromScheme(args);
return true;
}

try {
QMUIFragment fragment = mFragmentCls.newInstance();
fragment.setArguments(args);
int commitId = fragmentActivity.startFragment(fragment);
if (commitId == -1) {
QMUILog.d(QMUISchemeHandler.TAG, "start fragment failed.");
return false;
}
return true;
} catch (Exception e) {
QMUILog.printErrStackTrace(QMUISchemeHandler.TAG, e,
"Error to create fragment: %s", mFragmentCls.getSimpleName());
}
return false;
}
Expand Down
@@ -0,0 +1,25 @@
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.qmuiteam.qmui.arch.scheme;

import android.os.Bundle;

import androidx.annotation.Nullable;

public interface FragmentSchemeRefreshable {
void refreshFromScheme(@Nullable Bundle bundle);
}
Expand Up @@ -35,36 +35,7 @@ public class QMUIDefaultSchemeFragmentFactory implements QMUISchemeFragmentFacto

@Override
@Nullable
public QMUIFragment factory(@NonNull Class<? extends QMUIFragment> fragmentCls,
@Nullable Map<String, SchemeValue> scheme) {
try {
QMUIFragment fragment = fragmentCls.newInstance();
fragment.setArguments(createBundleForScheme(scheme));
return fragment;
} catch (Exception e) {
QMUILog.printErrStackTrace(QMUISchemeHandler.TAG, e,
"Error to create fragment: %s", fragmentCls.getSimpleName());
return null;
}
}

@Override
@Nullable
public Intent factory(@NonNull Activity activity,
@NonNull Class<? extends QMUIFragmentActivity>[] activityClassList,
@NonNull Class<? extends QMUIFragment> fragmentCls,
@Nullable Map<String, SchemeValue> scheme) {
Bundle bundle = createBundleForScheme(scheme);
if (activityClassList.length == 0) {
return null;
}
Intent intent = QMUIFragmentActivity.intentOf(activity, activityClassList[0], fragmentCls, bundle);
intent.putExtra(ARG_FROM_SCHEME, true);
return intent;
}

@NonNull
private Bundle createBundleForScheme(@Nullable Map<String, SchemeValue> scheme) {
public Bundle factory(@Nullable Map<String, SchemeValue> scheme) {
Bundle bundle = new Bundle();
bundle.putBoolean(QMUISchemeHandler.ARG_FROM_SCHEME, true);
if (scheme != null && !scheme.isEmpty()) {
Expand All @@ -89,6 +60,21 @@ private Bundle createBundleForScheme(@Nullable Map<String, SchemeValue> scheme)
return bundle;
}

@Override
@Nullable
public Intent factory(@NonNull Activity activity,
@NonNull Class<? extends QMUIFragmentActivity>[] activityClassList,
@NonNull Class<? extends QMUIFragment> fragmentCls,
@Nullable Map<String, SchemeValue> scheme) {
Bundle bundle = factory(scheme);
if (activityClassList.length == 0) {
return null;
}
Intent intent = QMUIFragmentActivity.intentOf(activity, activityClassList[0], fragmentCls, bundle);
intent.putExtra(ARG_FROM_SCHEME, true);
return intent;
}

@Override
public boolean shouldBlockJump(@NonNull Activity activity,
@NonNull Class<? extends QMUIFragment> fragmentCls,
Expand Down
Expand Up @@ -18,6 +18,7 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -29,8 +30,7 @@

public interface QMUISchemeFragmentFactory {
@Nullable
QMUIFragment factory(@NonNull Class<? extends QMUIFragment> fragmentCls,
@Nullable Map<String, SchemeValue> scheme);
Bundle factory(@Nullable Map<String, SchemeValue> scheme);

@Nullable
Intent factory(@NonNull Activity activity,
Expand Down
Expand Up @@ -44,14 +44,18 @@ public abstract class SchemeItem {
@Nullable
private Class<? extends QMUISchemeMatcher> mSchemeMatcherCls;

private boolean mUseRefreshIfMatchedCurrent;

public SchemeItem(@Nullable ArrayMap<String, String> required,
boolean useRefreshIfMatchedCurrent,
@Nullable String[] keysForInt,
@Nullable String[] keysForBool,
@Nullable String[] keysForLong,
@Nullable String[] keysForFloat,
@Nullable String[] keysForDouble,
@Nullable Class<? extends QMUISchemeMatcher> schemeMatcherCls) {
mRequired = required;
mUseRefreshIfMatchedCurrent = useRefreshIfMatchedCurrent;
mKeysForInt = keysForInt;
mKeysForBool = keysForBool;
mKeysForLong = keysForLong;
Expand All @@ -60,6 +64,10 @@ public SchemeItem(@Nullable ArrayMap<String, String> required,
mSchemeMatcherCls = schemeMatcherCls;
}

public boolean isUseRefreshIfMatchedCurrent() {
return mUseRefreshIfMatchedCurrent;
}

@Nullable
public Map<String, SchemeValue> convertFrom(@Nullable Map<String, String> schemeParams) {
if (schemeParams == null || schemeParams.isEmpty()) {
Expand Down
Expand Up @@ -17,11 +17,12 @@
package com.qmuiteam.qmuidemo.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.qmuiteam.qmui.arch.annotation.ActivityScheme;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmuidemo.R;
Expand All @@ -31,9 +32,13 @@
import butterknife.BindView;
import butterknife.ButterKnife;

@ActivityScheme(name = "arch", required = {"aa", "bb=3"}, keysWithBoolValue = {"aa"})
@ActivityScheme(name = "arch",
useRefreshIfCurrentMatched = true,
required = {"aa", "bb=3"},
keysWithBoolValue = {"aa"})
public class ArchTestActivity extends BaseActivity {
@BindView(R.id.topbar) QMUITopBarLayout mTopBar;
@BindView(R.id.topbar)
QMUITopBarLayout mTopBar;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -56,4 +61,5 @@ public void onClick(View v) {
mTopBar.setTitle("Arch Test");
QDArchTestFragment.injectEntrance(mTopBar);
}

}

0 comments on commit 609af67

Please sign in to comment.