Add scene dialog

This commit is contained in:
seven332 committed 2015-04-26 21:10:16 +08:00
1 parent 84b8c41173
commit ab861c932a
19 files changed
+499 -26

No files matched your search

@@ -28,6 +28,8 @@ import com.hippo.ehviewer.widget.SearchBar;
import com.hippo.ehviewer.widget.SearchDatabase;
import com.hippo.ehviewer.widget.SearchLayout;
import com.hippo.scene.Scene;
import com.hippo.scene.SimpleDialog;
import com.hippo.util.AppHandler;
import com.hippo.util.Log;
import com.hippo.util.ViewUtils;
@@ -77,6 +79,13 @@ class GalleryListScene extends Scene implements SearchBar.Helper {
// TEST
mContentLayout.showText("四姑拉斯基");
AppHandler.getInstance().postDelayed(new Runnable() {
@Override
public void run() {
new SimpleDialog.Builder().setTitle("NIhao").show();
}
}, 2000);
}
@Override
@@ -25,7 +25,6 @@ import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
@@ -83,7 +82,6 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
private View mNormalView;
private CategoryTable mTableCategory;
private CheckBox mCheckSpecifyAuthor;
private FloatLabelEditText mTextSearch;
private SwitchCompat mSwitchEnableAdvance;
private View mAdvanceView;
@@ -224,7 +222,7 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView == mCheckSpecifyAuthor) {
mTextSearch.setPrefix(isChecked ? "uploader:" : null);
// TODO
} else if (buttonView == mSwitchEnableAdvance) {
mEnableAdvance = isChecked;
@@ -288,10 +286,7 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
if (mTableCategory != null) {
lub.setCategory(mTableCategory.getCategory());
}
if (mTextSearch != null) {
String searchKey = mTextSearch.getText().toString();
lub.setSearchKey(TextUtils.isEmpty(searchKey) ? null : searchKey);
}
// TODO
if (mSwitchEnableAdvance.isChecked() && mTableAdvanceSearch != null) {
lub.setAdvanceSearch(mTableAdvanceSearch.getAdvanceSearch());
lub.setMinRating(mTableAdvanceSearch.getMinRating());
@@ -377,7 +372,6 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
bindActionView((ActionHolder) holder);
} else {
View view = mInflater.inflate(R.layout.search_category, parent, false);
// ViewCompat.setElevation(view, UiUtils.dp2pix(4)); // TODO
holder = new SearchHolder(view);
switch (viewType) {
case ITEM_TYPE_NORMAL:{
@@ -410,7 +404,6 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
mNormalView = holder.content.getChildAt(0);
mTableCategory = (CategoryTable) mNormalView.findViewById(R.id.search_category_table);
mCheckSpecifyAuthor = (CheckBox) mNormalView.findViewById(R.id.search_specify_author);
mTextSearch = (FloatLabelEditText) mNormalView.findViewById(R.id.search_text);
mSwitchEnableAdvance = (SwitchCompat) mNormalView.findViewById(R.id.search_enable_advance);
// Restore state
+25 -8
View File
@@ -15,6 +15,7 @@
package com.hippo.scene;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
@@ -48,6 +49,8 @@ public abstract class Scene {
private @Nullable Curtain mCurtain;
private @Nullable Announcer mAnnouncer;
@SuppressWarnings("deprecation")
private @Nullable AbsoluteLayout mSceneView;
@@ -55,7 +58,7 @@ public abstract class Scene {
private int mState;
private static SceneManager sSceneManager;
protected static SceneManager sSceneManager;
private int mFitPaddingBottom = -1;
@@ -67,10 +70,18 @@ public abstract class Scene {
mCurtain = curtain;
}
void setAnnouncer(@Nullable Announcer announcer) {
mAnnouncer = announcer;
}
@Nullable Curtain getCurtain() {
return mCurtain;
}
@Nullable Announcer getAnnouncer() {
return mAnnouncer;
}
// If there is no StageActivity for SceneManager, yout will get AssertError
public @NonNull StageActivity getStageActivity() {
StageActivity stageActivity = sSceneManager.getStageActivity();
@@ -108,6 +119,11 @@ public abstract class Scene {
mState = state;
}
@SuppressWarnings("deprecation")
protected AbsoluteLayout createSceneView(Context context) {
return new AbsoluteLayout(context);
}
public final void finish() {
sSceneManager.finishScene(this);
}
@@ -116,8 +132,7 @@ public abstract class Scene {
onCreate(savedInstanceState);
if (mSceneView == null) {
//noinspection deprecation
mSceneView = new AbsoluteLayout(getStageActivity());
mSceneView = createSceneView(getStageActivity());
initBackground(mSceneView);
}
@@ -190,8 +205,7 @@ public abstract class Scene {
protected void setContentView(int resId) {
StageActivity sa = getStageActivity();
//noinspection deprecation
mSceneView = new AbsoluteLayout(sa);
mSceneView = createSceneView(sa);
initBackground(mSceneView);
mSceneView.setBackgroundColor(mBackgroundColor);
sa.getLayoutInflater().inflate(resId, mSceneView);
@@ -199,15 +213,18 @@ public abstract class Scene {
protected void setContentView(View view) {
StageActivity sa = getStageActivity();
//noinspection deprecation
mSceneView = new AbsoluteLayout(sa);
mSceneView = createSceneView(sa);
initBackground(mSceneView);
mSceneView.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mSceneView.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
private void initBackground(@NonNull View bg) {
bg.setBackgroundColor(mBackgroundColor);
bg.setClickable(true);
bg.setFocusable(true);
bg.setFocusableInTouchMode(true);
bg.requestFocus();
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.hippo.scene;
import android.support.annotation.Nullable;
public abstract class SceneDialog extends Scene {
public void show() {
sSceneManager.showDialog(this, null);
}
public void show(@Nullable Curtain curtain) {
sSceneManager.showDialog(this, curtain);
}
}
@@ -44,9 +44,18 @@ class SceneManager {
return mStageActivity;
}
private boolean isStageAlive() {
return mStageActivity != null;
}
// TODO check previousState state
void startScene(@NonNull Class sceneClass, @Nullable Announcer announcer,
@Nullable Curtain curtain) {
if (!isStageAlive()) {
Log.w(TAG, "Stage is not alive, but attemp to create " + sceneClass.getSimpleName());
return;
}
Scene scene;
try {
scene = (Scene) sceneClass.newInstance();
@@ -58,8 +67,24 @@ class SceneManager {
} catch (ClassCastException e) {
throw new IllegalStateException(sceneClass.getName() + " can not cast to scene");
}
scene.setAnnouncer(announcer);
scene.setCurtain(curtain);
startScene(scene, curtain);
}
void showDialog(@NonNull SceneDialog dialog, @Nullable Curtain curtain) {
if (!isStageAlive()) {
Log.w(TAG, "Stage is not alive, but attemp to show dialog " + dialog.toString());
return;
}
dialog.setCurtain(curtain);
startScene(dialog, curtain);
}
private void startScene(Scene scene, Curtain curtain) {
Scene previousState = getTopState();
mSceneStack.push(scene);
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.hippo.scene;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.TextView;
import com.hippo.ehviewer.R;
public class SimpleDialog extends SceneDialog {
private static final int BACKGROUND_COLOR = 0x61000000;
private Builder mBuilder;
private StageActivity mActivity;
private SimpleDialogFrame mFrame;
private TextView mTitle;
private SimpleDialog(Builder builder) {
mBuilder = builder;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO
setBackgroundColor(BACKGROUND_COLOR);
mActivity = getStageActivity();
mFrame = new SimpleDialogFrame(mActivity);
setContentView(mFrame);
mActivity.getLayoutInflater().inflate(R.layout.simple_dialog, mFrame);
mTitle = (TextView) findViewById(R.id.title);
bindDialog();
}
private void bindDialog() {
mTitle.setText(mBuilder.mTitle);
}
@Override
protected void onGetFitPaddingBottom(int b) {
mFrame.setFitPaddingBottom(b);
}
public static class Builder {
private String mTitle;
public Builder setTitle(String title) {
mTitle = title;
return this;
}
public @NonNull SceneDialog build() {
return new SimpleDialog(this);
}
public void show() {
new SimpleDialog(this).show();
}
}
}
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.hippo.scene;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.widget.AbsoluteLayout;
import com.hippo.ehviewer.R;
import com.hippo.util.ResourcesUtils;
public class SimpleDialogFrame extends CardView {
private int mPadding;
private int mFixedWidth;
private float mFixedWidthPercent;
private float mFixedHeightPercent;
private int mFitPaddingBottom;
public SimpleDialogFrame(Context context) {
super(context);
init(context);
}
public SimpleDialogFrame(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SimpleDialogFrame(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
Resources resources = context.getResources();
mPadding = resources.getDimensionPixelOffset(R.dimen.simple_dialog_padding);
mFixedWidth = resources.getDimensionPixelOffset(R.dimen.simple_dialog_fixed_width);
mFixedWidthPercent = ResourcesUtils.getFloat(resources, R.dimen.simple_dialog_fixed_width_percent);
mFixedHeightPercent = ResourcesUtils.getFloat(resources, R.dimen.simple_dialog_fixed_height_percent);
}
public void setFitPaddingBottom(int fitPaddingBottom) {
if (mFitPaddingBottom != fitPaddingBottom) {
mFitPaddingBottom = fitPaddingBottom;
requestLayout();
}
}
@SuppressWarnings("deprecation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Width
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) {
int fixedWidth = Math.min((int) (originalWidth * mFixedWidthPercent), mFixedWidth) -
(2 * mPadding);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(fixedWidth, MeasureSpec.EXACTLY);
}
// Height
int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.UNSPECIFIED) {
int fixedHeight = (int) ((originalHeight - mFitPaddingBottom) * mFixedHeightPercent) -
(2 * mPadding);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(fixedHeight, MeasureSpec.AT_MOST);
}
// Measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Update x, y
int width = getMeasuredWidth();
int height = getMeasuredHeight();
AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) getLayoutParams();
lp.x = (originalWidth - width) / 2;
lp.y = (originalHeight - mFitPaddingBottom - height) / 2;
}
}
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.hippo.util;
import android.content.res.Resources;
import android.util.TypedValue;
public final class ResourcesUtils {
public static float getFloat(Resources resources, int resId) {
TypedValue outValue = new TypedValue();
resources.getValue(resId, outValue, true);
return outValue.getFloat();
}
}