Skip to content

Commit

Permalink
Show icon in selection dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Jun 20, 2019
1 parent 2da10a5 commit 7a47b25
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
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;
Expand Down Expand Up @@ -1062,17 +1063,23 @@ public boolean onItemLongClick(EasyRecyclerView parent, View view, int position,
}

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);
Expand All @@ -1088,10 +1095,10 @@ public boolean onItemLongClick(EasyRecyclerView parent, View view, int position,
}
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;
}
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/drawable/v_download_x24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="?attr/drawableColorPrimary"
android:pathData="@string/pd_download"/>

</vector>
29 changes: 29 additions & 0 deletions app/src/main/res/drawable/v_heart_broken_x24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="?attr/drawableColorPrimary"
android:pathData="@string/pd_heart_broken"/>

</vector>
29 changes: 29 additions & 0 deletions app/src/main/res/drawable/v_heart_x24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->

<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="?attr/drawableColorPrimary"
android:pathData="@string/pd_heart"/>

</vector>
34 changes: 34 additions & 0 deletions app/src/main/res/layout/dialog_item_select_with_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 The Android Open Source Project
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.
-->

<!--
This layout file is used by the AlertDialog when displaying a list of items.
This layout file is inflated and used as the TextView to display individual
items.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingRight="?attr/listPreferredItemPaddingRight"
android:drawablePadding="@dimen/keyline_margin"
android:ellipsize="marquee"/>
1 change: 1 addition & 0 deletions app/src/main/res/values/pathdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<string name="pd_homepage" translatable="false">M10,20V14H14V20H19V12H22L12,3L2,12H5V20H10Z</string>
<string name="pd_fire" translatable="false">M11.71,19C9.93,19 8.5,17.59 8.5,15.86C8.5,14.24 9.53,13.1 11.3,12.74C13.07,12.38 14.9,11.53 15.92,10.16C16.31,11.45 16.5,12.81 16.5,14.2C16.5,16.84 14.36,19 11.71,19M13.5,0.67C13.5,0.67 14.24,3.32 14.24,5.47C14.24,7.53 12.89,9.2 10.83,9.2C8.76,9.2 7.2,7.53 7.2,5.47L7.23,5.1C5.21,7.5 4,10.61 4,14A8,8 0 0,0 12,22A8,8 0 0,0 20,14C20,8.6 17.41,3.8 13.5,0.67Z</string>
<string name="pd_heart" translatable="false">M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C9.24,3 10.91,3.81 12,5.08C13.09,3.81 14.76,3 16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35Z</string>
<string name="pd_heart_broken" translatable="false">M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C8.17,3 8.82,3.12 9.44,3.33L13,9.35L9,14.35L12,21.35V21.35M16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35L11,14.35L15.5,9.35L12.85,4.27C13.87,3.47 15.17,3 16.5,3Z</string>
<string name="pd_history" translatable="false">M11,7V12.11L15.71,14.9L16.5,13.62L12.5,11.25V7M12.5,2C8.97,2 5.91,3.92 4.27,6.77L2,4.5V11H8.5L5.75,8.25C6.96,5.73 9.5,4 12.5,4A7.5,7.5 0 0,1 20,11.5A7.5,7.5 0 0,1 12.5,19C9.23,19 6.47,16.91 5.44,14H3.34C4.44,18.03 8.11,21 12.5,21C17.74,21 22,16.75 22,11.5A9.5,9.5 0 0,0 12.5,2Z</string>
<string name="pd_book_open" translatable="false">M21,4C19.9,3.7,18.7,3.5,17.5,3.5C15.6,3.5,13.4,3.9,12,5C10.6,3.9,8.4,3.5,6.5,3.5S2.5,3.9,1,5V19.6C1,19.9,1.3,20.1,1.5,20.1C1.6,20.1,1.6,20.1,1.8,20.1C3.1,19.5,5.1,19,6.5,19C8.4,19,10.6,19.4,12,20.5C13.4,19.6,15.8,19,17.5,19C19.1,19,20.9,19.3,22.3,20C22.4,20.1,22.4,20.1,22.6,20.1C22.9,20.1,23.1,19.8,23.1,19.6V5C22.4,4.6,21.8,4.3,21,4M21,17.5C19.9,17.1,18.7,17,17.5,17C15.8,17,13.4,17.6,12,18.5V7C13.4,6.2,15.8,5.5,17.5,5.5C18.7,5.5,19.9,5.7,21,6V17.5Z</string>
<string name="pd_delete" translatable="false">M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z</string>
Expand Down

0 comments on commit 7a47b25

Please sign in to comment.