diff --git a/app/src/main/java/com/hippo/ehviewer/widget/AdvanceSearchTable.java b/app/src/main/java/com/hippo/ehviewer/widget/AdvanceSearchTable.java index 4849ad8f6..fc3d149bc 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/AdvanceSearchTable.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/AdvanceSearchTable.java @@ -16,16 +16,24 @@ package com.hippo.ehviewer.widget; import android.content.Context; +import android.os.Bundle; +import android.os.Parcelable; import android.util.AttributeSet; import android.view.LayoutInflater; +import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.Spinner; import android.widget.TableLayout; import com.hippo.ehviewer.R; +import com.hippo.util.Utils; public class AdvanceSearchTable extends TableLayout { + private static final String STATE_KEY_SUPER = "super"; + private static final String STATE_KEY_ADVANCE_SEARCH = "advance_search"; + private static final String STATE_KEY_MIN_RATING = "min_rating"; + public static final int SNAME = 0x1; public static final int STAGS = 0x2; public static final int SDESC = 0x4; @@ -60,16 +68,25 @@ public class AdvanceSearchTable extends TableLayout { LayoutInflater inflater = LayoutInflater.from(getContext()); inflater.inflate(R.layout.widget_advance_search_table, this); - mSname = (CheckBox) findViewById(R.id.search_sname); - mStags = (CheckBox) findViewById(R.id.search_stags); - mSdesc = (CheckBox) findViewById(R.id.search_sdesc); - mStorr = (CheckBox) findViewById(R.id.search_storr); - mSto = (CheckBox) findViewById(R.id.search_sto); - mSdt1 = (CheckBox) findViewById(R.id.search_sdt1); - mSdt2 = (CheckBox) findViewById(R.id.search_sdt2); - mSh = (CheckBox) findViewById(R.id.search_sh); - mSr = (CheckBox) findViewById(R.id.search_sr); - mMinRating = (Spinner) findViewById(R.id.search_min_rating); + ViewGroup row0 = (ViewGroup) getChildAt(0); + mSname = (CheckBox) row0.getChildAt(0); + mStags = (CheckBox) row0.getChildAt(1); + + ViewGroup row1 = (ViewGroup) getChildAt(1); + mSdesc = (CheckBox) row1.getChildAt(0); + mStorr = (CheckBox) row1.getChildAt(1); + + ViewGroup row2 = (ViewGroup) getChildAt(2); + mSto = (CheckBox) row2.getChildAt(0); + mSdt1 = (CheckBox) row2.getChildAt(1); + + ViewGroup row3 = (ViewGroup) getChildAt(3); + mSdt2 = (CheckBox) row3.getChildAt(0); + mSh = (CheckBox) row3.getChildAt(1); + + ViewGroup row4 = (ViewGroup) getChildAt(4); + mSr = (CheckBox) row4.getChildAt(0); + mMinRating = (Spinner) row4.getChildAt(1); } public int getAdvanceSearch() { @@ -86,11 +103,50 @@ public class AdvanceSearchTable extends TableLayout { } public int getMinRating() { - if (mSr.isChecked()) { - return mMinRating.getSelectedItemPosition() + 2; + int position = mMinRating.getSelectedItemPosition(); + if (mSr.isChecked() && position >= 0) { + return position + 2; } else { return -1; } } + public void setAdvanceSearch(int advanceSearch) { + mSname.setChecked(Utils.int2boolean(advanceSearch & SNAME)); + mStags.setChecked(Utils.int2boolean(advanceSearch & STAGS)); + mSdesc.setChecked(Utils.int2boolean(advanceSearch & SDESC)); + mStorr.setChecked(Utils.int2boolean(advanceSearch & STORR)); + mSto.setChecked(Utils.int2boolean(advanceSearch & STO)); + mSdt1.setChecked(Utils.int2boolean(advanceSearch & SDT1)); + mSdt2.setChecked(Utils.int2boolean(advanceSearch & SDT2)); + mSh.setChecked(Utils.int2boolean(advanceSearch & SH)); + } + + public void setMinRating(int minRating) { + if (minRating >= 2 && minRating <= 5) { + mSr.setChecked(true); + mMinRating.setSelection(minRating - 2); + } else { + mSr.setChecked(false); + } + } + + @Override + public Parcelable onSaveInstanceState() { + final Bundle state = new Bundle(); + state.putParcelable(STATE_KEY_SUPER, super.onSaveInstanceState()); + state.putInt(STATE_KEY_ADVANCE_SEARCH, getAdvanceSearch()); + state.putInt(STATE_KEY_MIN_RATING, getMinRating()); + return state; + } + + @Override + public void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + final Bundle savedState = (Bundle) state; + super.onRestoreInstanceState(savedState.getParcelable(STATE_KEY_SUPER)); + setAdvanceSearch(savedState.getInt(STATE_KEY_ADVANCE_SEARCH)); + setMinRating(savedState.getInt(STATE_KEY_MIN_RATING)); + } + } } diff --git a/app/src/main/res/layout/widget_advance_search_table.xml b/app/src/main/res/layout/widget_advance_search_table.xml index 10cf6965e..c9b60dbbf 100644 --- a/app/src/main/res/layout/widget_advance_search_table.xml +++ b/app/src/main/res/layout/widget_advance_search_table.xml @@ -18,7 +18,6 @@