Show icon in selection dialogs
This commit is contained in:
1 parent
2da10a5855
commit
7a47b2504d
7 files changed
+213
-6
No files matched your search
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2019 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.dialog;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.TextView;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import com.hippo.ehviewer.R;
|
||||
|
||||
public class SelectItemWithIconAdapter extends BaseAdapter {
|
||||
|
||||
private Context context;
|
||||
private LayoutInflater inflater;
|
||||
|
||||
private CharSequence[] texts;
|
||||
private int[] icons;
|
||||
|
||||
public SelectItemWithIconAdapter(Context context, CharSequence[] texts, int[] icons) {
|
||||
int count = texts.length;
|
||||
if (count != icons.length) {
|
||||
throw new IllegalArgumentException("Length conflict");
|
||||
}
|
||||
this.context = context;
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
this.texts = texts;
|
||||
this.icons = icons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return texts.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return texts[position];
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (convertView == null) {
|
||||
convertView = inflater.inflate(R.layout.dialog_item_select_with_icon, parent, false);
|
||||
}
|
||||
TextView view = (TextView) convertView;
|
||||
|
||||
view.setText(texts[position]);
|
||||
|
||||
Drawable icon = AppCompatResources.getDrawable(context, icons[position]);
|
||||
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
|
||||
view.setCompoundDrawables(icon, null, null, null);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,7 @@ import com.hippo.ehviewer.download.DownloadManager;
|
||||
import com.hippo.ehviewer.ui.CommonOperations;
|
||||
import com.hippo.ehviewer.ui.GalleryActivity;
|
||||
import com.hippo.ehviewer.ui.MainActivity;
|
||||
import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter;
|
||||
import com.hippo.ehviewer.widget.GalleryInfoContentHelper;
|
||||
import com.hippo.ehviewer.widget.SearchBar;
|
||||
import com.hippo.ehviewer.widget.SearchLayout;
|
||||
@@ -1062,17 +1063,23 @@ public final class GalleryListScene extends BaseScene
|
||||
}
|
||||
|
||||
boolean downloaded = mDownloadManager.getDownloadState(gi.gid) != DownloadInfo.STATE_INVALID;
|
||||
boolean favourite = gi.favoriteSlot == -2;
|
||||
boolean favourited = gi.favoriteSlot != -2;
|
||||
|
||||
CharSequence[] items = new CharSequence[] {
|
||||
context.getString(R.string.read),
|
||||
context.getString(downloaded ? R.string.delete_downloads : R.string.download),
|
||||
context.getString(favourite ? R.string.add_to_favourites : R.string.remove_from_favourites),
|
||||
context.getString(favourited ? R.string.remove_from_favourites : R.string.add_to_favourites),
|
||||
};
|
||||
|
||||
int[] icons = new int[] {
|
||||
R.drawable.v_book_open_x24,
|
||||
downloaded ? R.drawable.v_delete_x24 : R.drawable.v_download_x24,
|
||||
favourited ? R.drawable.v_heart_broken_x24 : R.drawable.v_heart_x24,
|
||||
};
|
||||
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(EhUtils.getSuitableTitle(gi))
|
||||
.setItems(items, (dialog, which) -> {
|
||||
.setAdapter(new SelectItemWithIconAdapter(context, items, icons), (dialog, which) -> {
|
||||
switch (which) {
|
||||
case 0: // Read
|
||||
Intent intent = new Intent(activity, GalleryActivity.class);
|
||||
@@ -1088,10 +1095,10 @@ public final class GalleryListScene extends BaseScene
|
||||
}
|
||||
break;
|
||||
case 2: // Favorites
|
||||
if (favourite) {
|
||||
CommonOperations.addToFavorites(activity, gi, new AddToFavoriteListener(context, activity.getStageId(), getTag()));
|
||||
} else {
|
||||
if (favourited) {
|
||||
CommonOperations.removeFromFavorites(activity, gi, new RemoveFromFavoriteListener(context, activity.getStageId(), getTag()));
|
||||
} else {
|
||||
CommonOperations.addToFavorites(activity, gi, new AddToFavoriteListener(context, activity.getStageId(), getTag()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user