Skip to content

Commit

Permalink
refactor: Scene library remove APPCOMPAT_LIB dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
qii committed Nov 5, 2019
1 parent 9c1c96f commit 11dca0b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
1 change: 0 additions & 1 deletion library/scene/build.gradle
Expand Up @@ -15,7 +15,6 @@ android {
}

dependencies {
implementation APPCOMPAT_LIB
implementation LIFECYCLE_EXTENSIONS_LIB
implementation LIFECYCLE_RUNTIME_LIB

Expand Down
Expand Up @@ -19,7 +19,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.ViewGroup;

Expand Down
Expand Up @@ -20,12 +20,12 @@
import android.os.Message;
import android.support.annotation.*;
import android.support.v4.util.Pools;
import android.support.v7.view.ContextThemeWrapper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.scene.view.SceneContextThemeWrapper;

import java.util.concurrent.ArrayBlockingQueue;

Expand All @@ -41,7 +41,7 @@ public final class SceneAsyncLayoutInflater {
InflateThread mInflateThread;

public SceneAsyncLayoutInflater(@NonNull Context sceneContext) {
Context contextThemeWrapper = new ContextThemeWrapper(sceneContext, sceneContext.getTheme()) {
Context contextThemeWrapper = new SceneContextThemeWrapper(sceneContext, sceneContext.getTheme()) {
@Override
public Object getSystemService(@NonNull String name) {
if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
Expand Down
Expand Up @@ -21,17 +21,18 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import com.bytedance.scene.R;
import com.bytedance.scene.utlity.Utility;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

/**
* A ContextWrapper that allows you to modify the theme from what is in the
* wrapped context.
*
* <p>
* source code is copied from android.support.v7.view.ContextThemeWrapper, the difference is setTheme() method,
* SceneContextThemeWrapper setTheme will not modify Activity's theme
*
Expand All @@ -46,17 +47,6 @@ public class SceneContextThemeWrapper extends ContextWrapper {
private Resources mResources;
private boolean mIsThemeFromActivity;

/**
* Creates a new context wrapper with no theme and no base context.
* <p class="note">
* <strong>Note:</strong> A base context <strong>must</strong> be attached
* using {@link #attachBaseContext(Context)} before calling any other
* method on the newly constructed context wrapper.
*/
public SceneContextThemeWrapper() {
super(null);
}

/**
* Creates a new context wrapper with the specified theme.
* <p>
Expand All @@ -68,9 +58,12 @@ public SceneContextThemeWrapper() {
* @param themeResId the resource ID of the theme to be applied on top of
* the base context's theme
*/
public SceneContextThemeWrapper(Context base, @StyleRes int themeResId) {
public SceneContextThemeWrapper(@NonNull Context base, @StyleRes int themeResId) {
super(base);
mThemeResource = themeResId;
if (mThemeResource == 0) {
throw new IllegalArgumentException("themeResId can't be zero");
}
}

/**
Expand All @@ -82,9 +75,9 @@ public SceneContextThemeWrapper(Context base, @StyleRes int themeResId) {
* @param base the base context
* @param theme the theme against which resources should be inflated
*/
public SceneContextThemeWrapper(Context base, Resources.Theme theme) {
public SceneContextThemeWrapper(@NonNull Context base, @NonNull Resources.Theme theme) {
super(base);
mTheme = theme;
mTheme = Utility.requireNonNull(theme, "theme can't be null");
mIsThemeFromActivity = true;
}

Expand Down Expand Up @@ -142,7 +135,7 @@ private Resources getResourcesInternal() {
}

@Override
public void setTheme(int resid) {
public void setTheme(@StyleRes int resid) {
if (mThemeResource != resid) {
mThemeResource = resid;
if (mIsThemeFromActivity) {
Expand All @@ -164,11 +157,7 @@ public Resources.Theme getTheme() {
return mTheme;
}

if (mThemeResource == 0) {
mThemeResource = R.style.Theme_AppCompat_Light;
}
initializeTheme();

return mTheme;
}

Expand Down
Expand Up @@ -8,7 +8,7 @@ class TestAppCompatActivity : AppCompatActivity() {
lateinit var mFrameLayout: FrameLayout

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(com.bytedance.scene.R.style.Theme_AppCompat)
setTheme(com.bytedance.scene.ktx.R.style.Theme_AppCompat)
super.onCreate(savedInstanceState)
mFrameLayout = FrameLayout(this)
setContentView(mFrameLayout)
Expand Down

0 comments on commit 11dca0b

Please sign in to comment.