Add hint to search bar

This commit is contained in:
seven332 committed 2015-04-25 19:19:22 +08:00
1 parent fbeffe53f6
commit 84b8c41173
9 files changed
+228 -5

No files matched your search

@@ -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.ehviewer;
import com.hippo.util.Messenger;
public final class Constants {
public static final int MESSENGER_ID_SOURCE;
static {
Messenger messenger = Messenger.getInstance();
MESSENGER_ID_SOURCE = messenger.newId();
}
}
@@ -47,6 +47,10 @@ public final class EhClient {
"http://forums.e-hentai.org/index.php?act=Login&CODE=01";
private static final String FORUMS_URL = "http://forums.e-hentai.org/index.php";
public static final String HOST_G = "G.E-Hentai";
public static final String HOST_EX = "ExHentai";
public static final String HOST_LOFI = "Lofi.E-Hentai";
public static final String HEADER_G = "http://g.e-hentai.org/";
public static final String HEADER_EX = "http://exhentai.org/";
public static final String HEADER_LOFI = "http://lofi.e-hentai.org/";
@@ -74,8 +78,20 @@ public final class EhClient {
1L, TimeUnit.SECONDS, requestWorkQueue, threadFactory);
}
public static String getUrlHeader(int mode) {
switch (mode) {
public static String getReadableHost(int source) {
switch (source) {
default:
case SOURCE_G:
return HOST_G;
case SOURCE_EX:
return HOST_EX;
case SOURCE_LOFI:
return HOST_LOFI;
}
}
public static String getUrlHeader(int source) {
switch (source) {
default:
case SOURCE_G:
return HEADER_G;
@@ -18,15 +18,20 @@ package com.hippo.ehviewer.widget;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@@ -40,8 +45,11 @@ import android.widget.TextView;
import com.hippo.animation.SimpleAnimatorListener;
import com.hippo.drawable.AddDeleteDrawable;
import com.hippo.drawable.DrawerArrowDrawable;
import com.hippo.ehviewer.Constants;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.util.MathUtils;
import com.hippo.util.Messenger;
import com.hippo.util.UiUtils;
import com.hippo.util.ViewUtils;
import com.hippo.widget.SimpleImageView;
@@ -52,7 +60,7 @@ import java.util.List;
public class SearchBar extends CardView implements View.OnClickListener,
TextView.OnEditorActionListener, TextWatcher,
SearchEditText.SearchEditTextListener {
SearchEditText.SearchEditTextListener, Messenger.Receiver {
private static final long ANIMATION_TIME = 300;
@@ -81,6 +89,8 @@ public class SearchBar extends CardView implements View.OnClickListener,
private boolean mInAnimation = false;
private boolean mFirstLayout = true;
private int mSource;
public SearchBar(Context context) {
super(context);
init(context);
@@ -123,9 +133,25 @@ public class SearchBar extends CardView implements View.OnClickListener,
mEditText.addTextChangedListener(this);
mSuggestionList = new ArrayList<>();
// TODO
mSuggestionAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1, mSuggestionList);
mList.setAdapter(mSuggestionAdapter);
// TODO get source from config
setSource(EhClient.SOURCE_EX);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Messenger.getInstance().register(Constants.MESSENGER_ID_SOURCE, this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
Messenger.getInstance().unregister(Constants.MESSENGER_ID_SOURCE, this);
}
private void updateSuggestions() {
@@ -136,6 +162,23 @@ public class SearchBar extends CardView implements View.OnClickListener,
mSuggestionAdapter.notifyDataSetChanged();
}
@SuppressWarnings({"deprecation", "ConstantConditions"})
public void setSource(int source) {
if (mSource != source) {
Resources resources = getContext().getResources();
Drawable searchImage = resources.getDrawable(R.drawable.ic_search);
SpannableStringBuilder ssb = new SpannableStringBuilder(" ");
ssb.append(String.format(resources.getString(R.string.search_bar_hint),
EhClient.getReadableHost(source)));
int textSize = (int) (mEditText.getTextSize() * 1.25);
searchImage.setBounds(0, 0, textSize, textSize);
ssb.setSpan(new ImageSpan(searchImage), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mEditText.setHint(ssb);
mSource = source;
}
}
public void setHelper(Helper helper) {
mHelper = helper;
}
@@ -357,6 +400,16 @@ public class SearchBar extends CardView implements View.OnClickListener,
mHelper.onBackPressed();
}
@Override
public void onReceive(int id, Object obj) {
if (id == Constants.MESSENGER_ID_SOURCE) {
if (obj instanceof Integer) {
int source = (Integer) obj;
setSource(source);
}
}
}
public interface Helper {
void onClickTitle();
void onClickMenu();
@@ -0,0 +1,35 @@
/*
* 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.os.Handler;
import android.os.Looper;
public class AppHandler extends Handler {
private static Handler sInstance;
public static Handler getInstance() {
if (sInstance == null) {
sInstance = new AppHandler(Looper.getMainLooper());
}
return sInstance;
}
public AppHandler(Looper mainLooper) {
super(mainLooper);
}
}
@@ -22,7 +22,7 @@ public final class IntIdGenerator {
// Empty
}
public int nextId() {
public synchronized int nextId() {
return mId++;
}
@@ -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.util;
import android.util.SparseArray;
import java.util.HashSet;
import java.util.Set;
public class Messenger {
private IntIdGenerator mIdGenerator;
private SparseArray<Set<Receiver>> mReceiverSetMap;
private static Messenger sInstance;
public static Messenger getInstance() {
if (sInstance == null) {
sInstance = new Messenger();
}
return sInstance;
}
private Messenger() {
mIdGenerator = IntIdGenerator.create();
mReceiverSetMap = new SparseArray<>();
}
public int newId() {
return mIdGenerator.nextId();
}
public void notify(final int id, final Object obj) {
// Make sure do it in UI thread
AppHandler.getInstance().post(new Runnable() {
@Override
public void run() {
Set<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet != null) {
for (Receiver receiver : receiverSet) {
receiver.onReceive(id, obj);
}
}
}
});
}
public void register(int id, Receiver receiver) {
Set<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet == null) {
receiverSet = new HashSet<>();
mReceiverSetMap.put(id, receiverSet);
}
receiverSet.add(receiver);
}
public void unregister(int id, Receiver receiver) {
Set<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet != null) {
receiverSet.remove(receiver);
if (receiverSet.isEmpty()) {
mReceiverSetMap.remove(id);
}
}
}
public interface Receiver {
void onReceive(final int id, final Object obj);
}
}