From 9e2c5715ed11ce89956c6ecee65b21fc8a9fc6ef Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 20 Mar 2016 14:51:45 +0800 Subject: [PATCH] Update --- .../ehviewer/client/data/GalleryInfo.java | 14 + .../hippo/ehviewer/ui/GalleryActivity.java | 17 +- .../ehviewer/ui/scene/ThumbSpanHelper.java | 272 ++++++++++++++++++ .../java/com/hippo/widget/ContentLayout.java | 2 +- .../GridAutoSpanLayoutManager.java | 28 ++ 5 files changed, 321 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/hippo/ehviewer/ui/scene/ThumbSpanHelper.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java index 5853b46cc..9ec8c7abf 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryInfo.java @@ -93,6 +93,10 @@ public class GalleryInfo implements Parcelable { public int thumbWidth; public int thumbHeight; + public int spanSize; + public int spanIndex; + public int spanGroupIndex; + /** * language from title */ @@ -126,6 +130,11 @@ public class GalleryInfo implements Parcelable { dest.writeFloat(this.rating); dest.writeString(this.simpleLanguage); dest.writeStringArray(this.simpleTags); + dest.writeInt(this.thumbWidth); + dest.writeInt(this.thumbHeight); + dest.writeInt(this.spanSize); + dest.writeInt(this.spanIndex); + dest.writeInt(this.spanGroupIndex); } public GalleryInfo() {} @@ -142,6 +151,11 @@ public class GalleryInfo implements Parcelable { this.rating = in.readFloat(); this.simpleLanguage = in.readString(); this.simpleTags = in.createStringArray(); + this.thumbWidth = in.readInt(); + this.thumbHeight = in.readInt(); + this.spanSize = in.readInt(); + this.spanIndex = in.readInt(); + this.spanGroupIndex = in.readInt(); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java index 38a99aba6..c64f4a4cb 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java @@ -254,15 +254,10 @@ public class GalleryActivity extends EhActivity mSystemUiShowing = true; } - mClock = findViewById(R.id.clock); - mBattery = findViewById(R.id.battery); - - if (mClock != null && !Settings.getShowClock()) { - mClock.setVisibility(View.GONE); - } - if (mBattery != null && !Settings.getShowBattery()) { - mBattery.setVisibility(View.GONE); - } + mClock = ViewUtils.$$(this, R.id.clock); + mBattery = ViewUtils.$$(this, R.id.battery); + mClock.setVisibility(Settings.getShowClock() ? View.VISIBLE : View.GONE); + mBattery.setVisibility(Settings.getShowBattery() ? View.VISIBLE : View.GONE); mSliderPanel = findViewById(R.id.slider_panel); mLeftText = (TextView) mSliderPanel.findViewById(R.id.left); @@ -282,10 +277,10 @@ public class GalleryActivity extends EhActivity getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } - if (Settings.getGuideGallery()) { + //if (Settings.getGuideGallery()) { FrameLayout mainLayout = (FrameLayout) ViewUtils.$$(this, R.id.main); mainLayout.addView(new GalleryGuideView(this)); - } + //} } @Override diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/ThumbSpanHelper.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/ThumbSpanHelper.java new file mode 100644 index 000000000..146e4832e --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ThumbSpanHelper.java @@ -0,0 +1,272 @@ +/* + * Copyright 2016 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 + * 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.hippo.ehviewer.ui.scene; + +import android.util.Log; + +import com.hippo.ehviewer.client.data.GalleryInfo; + +import java.util.Arrays; +import java.util.List; + +// TODO GridLayoutManager do not work at all ! SpaceGroupIndex is useless for layout! I need to create one by myself +public class ThumbSpanHelper { + + private static final int MIN_ARRAY_LENGTH = 50; + private static final int MAX_ROW_INTERVAL = 3; + + private final List mData; + + // true for occupied, false for free + private boolean[] mCells = new boolean[MIN_ARRAY_LENGTH]; + + private int mNextIndex; + private int mNextGroupIndex; + private int mNearSpaceIndex; + private int mNearSpaceGroupIndex; + private int mAttachedCount; + + private int mSpanCount; + + private boolean mEnable; + + public ThumbSpanHelper(List data) { + mData = data; + } + + public void setEnable(boolean enable) { + if (enable == mEnable) { + return; + } + mEnable = enable; + + if (!enable) { + clear(); + mSpanCount = 0; + } else { + if (mSpanCount > 0) { + rebuild(); + } + } + } + + public void setSpanCount(int spanCount) { + if (!mEnable) { + return; + } + + if (spanCount == mSpanCount) { + return; + } + mSpanCount = spanCount; + if (spanCount > 0) { + rebuild(); + } + } + + public void notifyDataSetChanged() { + if (!mEnable || mSpanCount <= 0) { + return; + } + rebuild(); + } + + public void notifyItemRangeRemoved(int positionStart, int itemCount) { + if (!mEnable || mSpanCount <= 0) { + return; + } + rebuild(); + } + + protected void notifyItemRangeInserted(int positionStart, int itemCount) { + if (!mEnable || mSpanCount <= 0) { + return; + } + if (mAttachedCount == positionStart) { + append(); + } else { + rebuild(); + } + } + + private void clear() { + + Log.d("TAG", "=======================clear======================="); + + if (mSpanCount > 0) { + if (mNextGroupIndex > 0 || mNextIndex > 0) { + Arrays.fill(mCells, 0, mNextGroupIndex * mSpanCount + mNextIndex, false); + } + } else { + Arrays.fill(mCells, false); + } + mNextIndex = 0; + mNextGroupIndex = 0; + mNearSpaceIndex = 0; + mNearSpaceGroupIndex = 0; + mAttachedCount = 0; + } + + private void rebuild() { + clear(); + append(); + } + + private void append() { + if (1 == mSpanCount) { + for (int i = mAttachedCount, n = mData.size(); i < n; i++) { + GalleryInfo gi = mData.get(i); + gi.spanSize = 1; + gi.spanGroupIndex = i; + gi.spanIndex = 0; + } + mAttachedCount = mData.size(); + } else if (mSpanCount >= 2) { + for (int i = mAttachedCount, n = mData.size(); i < n; i++) { + GalleryInfo gi = mData.get(i); + int spanSize = gi.thumbWidth <= gi.thumbHeight ? 1 : 2; + gi.spanSize = spanSize; + + if (1 == spanSize) { + // Update near space + updateNearSpace(); + + Log.d("TAG", "Update mNearSpaceGroupIndex = " + mNearSpaceGroupIndex + ", mNearSpaceIndex = " + mNearSpaceIndex); + + if (mNextIndex == mNearSpaceIndex && mNextGroupIndex == mNearSpaceGroupIndex) { + // No space, just append + gi.spanIndex = mNextIndex; + gi.spanGroupIndex = mNextGroupIndex; + + // Update cell + int start = gi.spanGroupIndex * mSpanCount + gi.spanIndex; + fillCell(start, start + 1); + + // Update field + mNextIndex++; + if (mSpanCount == mNextIndex) { + mNextIndex = 0; + mNextGroupIndex++; + } + mNearSpaceIndex = mNextIndex; + mNearSpaceGroupIndex = mNextGroupIndex; + + Log.d("TAG", "type 0"); + Log.d("TAG", "i = " + i + ", spanSize = " + spanSize + ", spanGroupIndex = " + gi.spanGroupIndex + ", spanIndex = " + gi.spanIndex); + Log.d("TAG", "mNextGroupIndex = " + mNextGroupIndex + ", mNextIndex = " + mNextIndex); + Log.d("TAG", "mNearSpaceGroupIndex = " + mNearSpaceGroupIndex + ", mNearSpaceIndex = " + mNearSpaceIndex); + + } else { + // Found space + gi.spanIndex = mNearSpaceIndex; + gi.spanGroupIndex = mNearSpaceGroupIndex; + + // Update cell + int start = gi.spanGroupIndex * mSpanCount + gi.spanIndex; + fillCell(start, start + 1); + + // Find near space + findNearSpace(start + 1); + + Log.d("TAG", "type 1"); + Log.d("TAG", "i = " + i + ", spanSize = " + spanSize + ", spanGroupIndex = " + gi.spanGroupIndex + ", spanIndex = " + gi.spanIndex); + Log.d("TAG", "mNextGroupIndex = " + mNextGroupIndex + ", mNextIndex = " + mNextIndex); + Log.d("TAG", "mNearSpaceGroupIndex = " + mNearSpaceGroupIndex + ", mNearSpaceIndex = " + mNearSpaceIndex); + } + } else { + boolean syncNear = mNextIndex == mNearSpaceIndex && mNextGroupIndex == mNearSpaceGroupIndex; + // false for old, true for new + boolean oldOrNew; + if (mSpanCount - mNextIndex >= 2) { + gi.spanIndex = mNextIndex; + gi.spanGroupIndex = mNextGroupIndex; + oldOrNew = true; + } else { + // Go to next row + gi.spanIndex = 0; + gi.spanGroupIndex = mNextGroupIndex + 1; + oldOrNew = false; + } + + // Update cell + int start = gi.spanGroupIndex * mSpanCount + gi.spanIndex; + fillCell(start, start + 2); + + // Update field + if (syncNear && !oldOrNew) { + mNearSpaceIndex = mNextIndex; + mNearSpaceGroupIndex = mNextGroupIndex; + } + mNextIndex = gi.spanIndex + 2; + mNextGroupIndex = gi.spanGroupIndex; + if (mSpanCount == mNextIndex) { + mNextIndex = 0; + mNextGroupIndex++; + } + if (syncNear && oldOrNew) { + mNearSpaceIndex = mNextIndex; + mNearSpaceGroupIndex = mNextGroupIndex; + } + + Log.d("TAG", "type 2"); + Log.d("TAG", "i = " + i + ", spanSize = " + spanSize + ", spanGroupIndex = " + gi.spanGroupIndex + ", spanIndex = " + gi.spanIndex); + Log.d("TAG", "mNextGroupIndex = " + mNextGroupIndex + ", mNextIndex = " + mNextIndex); + Log.d("TAG", "mNearSpaceGroupIndex = " + mNearSpaceGroupIndex + ", mNearSpaceIndex = " + mNearSpaceIndex); + } + } + mAttachedCount = mData.size(); + } + } + + private void updateNearSpace() { + // Check is space is near enough + if (mNextGroupIndex - mNearSpaceGroupIndex <= MAX_ROW_INTERVAL) { + return; + } + + // The space is too far, find a near one + int start = Math.max(0, mNextGroupIndex - MAX_ROW_INTERVAL) * mSpanCount; + findNearSpace(start); + } + + private void findNearSpace(int start) { + int end = mNextGroupIndex * mSpanCount + mNextIndex; + boolean[] cells = mCells; + for (int i = start; i < end; i++) { + if (!cells[i]) { + // Find space ! + mNearSpaceIndex = i % mSpanCount; + mNearSpaceGroupIndex = i / mSpanCount; + return; + } + } + // Can't find space + mNearSpaceIndex = mNextIndex; + mNearSpaceGroupIndex = mNextGroupIndex; + } + + private void fillCell(int start, int end) { + boolean[] array = mCells; + // Avoid IndexOutOfBoundsException + if (end >= array.length) { + boolean[] newArray = new boolean[end + (end < (MIN_ARRAY_LENGTH / 2) ? MIN_ARRAY_LENGTH : end >> 1)]; + System.arraycopy(array, 0, newArray, 0, array.length); + mCells = array = newArray; + } + Arrays.fill(array, start, end, true); + } +} diff --git a/app/src/main/java/com/hippo/widget/ContentLayout.java b/app/src/main/java/com/hippo/widget/ContentLayout.java index 0ac3057c6..95cf6e686 100644 --- a/app/src/main/java/com/hippo/widget/ContentLayout.java +++ b/app/src/main/java/com/hippo/widget/ContentLayout.java @@ -357,7 +357,7 @@ public class ContentLayout extends FrameLayout { mEmptyString = str; } - public List getDat() { + public List getData() { return mData; } diff --git a/app/src/main/java/com/hippo/widget/recyclerview/GridAutoSpanLayoutManager.java b/app/src/main/java/com/hippo/widget/recyclerview/GridAutoSpanLayoutManager.java index 0d304bbbc..e5672e475 100644 --- a/app/src/main/java/com/hippo/widget/recyclerview/GridAutoSpanLayoutManager.java +++ b/app/src/main/java/com/hippo/widget/recyclerview/GridAutoSpanLayoutManager.java @@ -20,6 +20,9 @@ import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; +import java.util.ArrayList; +import java.util.List; + public class GridAutoSpanLayoutManager extends GridLayoutManager { public static final int STRATEGY_MIN_SIZE = 0; @@ -29,6 +32,8 @@ public class GridAutoSpanLayoutManager extends GridLayoutManager { private boolean mColumnSizeChanged = true; private int mStrategy; + private List mListeners; + public GridAutoSpanLayoutManager(Context context, int columnSize) { super(context, 1); setColumnSize(columnSize); @@ -92,7 +97,30 @@ public class GridAutoSpanLayoutManager extends GridLayoutManager { } setSpanCount(spanCount); mColumnSizeChanged = false; + + if (null != mListeners) { + for (int i = 0, n = mListeners.size(); i < n; i++) { + mListeners.get(i).onUpdateSpanCount(spanCount); + } + } } super.onLayoutChildren(recycler, state); } + + public void addOnUpdateSpanCountListener(OnUpdateSpanCountListener listener) { + if (null == mListeners) { + mListeners = new ArrayList<>(); + } + mListeners.add(listener); + } + + public void removeOnUpdateSpanCountListener(OnUpdateSpanCountListener listener) { + if (null != mListeners) { + mListeners.remove(listener); + } + } + + public interface OnUpdateSpanCountListener { + void onUpdateSpanCount(int spanCount); + } }