Skip to content

Commit

Permalink
Merge remote-tracking branch 'guger/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Mar 10, 2019
2 parents 6dee606 + 23081de commit 7f34ec0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 42 deletions.
51 changes: 38 additions & 13 deletions icondialog/src/main/java/com/maltaisn/icondialog/IconDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ public class IconDialog extends DialogFragment {

@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {VISIBILITY_ALWAYS, VISIBILITY_NEVER, VISIBILITY_IF_LANG_AVAILABLE})
public @interface SearchVisibility {}
public @interface SearchVisibility {
}

@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {VISIBILITY_ALWAYS, VISIBILITY_NEVER, VISIBILITY_IF_NO_SEARCH})
public @interface TitleVisibility {}
public @interface TitleVisibility {
}

public static final int MAX_SELECTION_NONE = -1;

Expand All @@ -101,23 +103,29 @@ public class IconDialog extends DialogFragment {
private int iconSize;
private int[] iconColors;

private @SearchVisibility int searchVisibility;
private @Nullable Locale searchLanguage;
private @SearchVisibility
int searchVisibility;
private @Nullable
Locale searchLanguage;
private boolean showHeaders;
private boolean stickyHeaders;
private boolean showSelectBtn;
private int maxSelection;
private boolean maxSelShowMessage;
private @Nullable String maxSelMessage;
private @Nullable
String maxSelMessage;
private boolean showClearBtn;
private @TitleVisibility int dialogTitleVisibility;
private @Nullable String dialogTitle;
private @TitleVisibility
int dialogTitleVisibility;
private @Nullable
String dialogTitle;
private BaseIconFilter iconFilter;
private boolean loadIconDrawables;

private List<Item> listItems;
private List<Item> selectedItems;
private @Nullable int[] selectedIconsId;
private @Nullable
int[] selectedIconsId;
private String searchText;

private boolean searchIgnoreDelay;
Expand Down Expand Up @@ -155,9 +163,9 @@ public void onCreate(Bundle savedInstanceState) {
iconHelper = IconHelper.getInstance(getActivity());
iconHelper.addLoadCallback(new IconHelper.LoadCallback() {
@Override
public void onDataLoaded() {
public void onDataLoaded(IconHelper helper) {
if (loadIconDrawables) {
iconHelper.loadIconDrawables();
helper.loadIconDrawables();
}
}
});
Expand Down Expand Up @@ -258,7 +266,7 @@ public void onClick(View v) {

iconHelper.addLoadCallback(new IconHelper.LoadCallback() {
@Override
public void onDataLoaded() {
public void onDataLoaded(IconHelper helper) {
loadPb.setVisibility(View.GONE);

final EditText searchEdt = view.findViewById(R.id.icd_edt_search);
Expand Down Expand Up @@ -305,10 +313,12 @@ public void run() {
searchEdt.setText(searchText);
searchEdt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable text) {
Expand Down Expand Up @@ -507,6 +517,7 @@ private Object getCaller() {

/**
* Get the list of icons matching search with category headers
*
* @param search null to get whole list, or text to search among icon labels to filter icons
* @return the list of items
*/
Expand Down Expand Up @@ -597,6 +608,7 @@ public int compare(Item i1, Item i2) {

/**
* Get the position of items in the adapter
*
* @param items array of icon items (varargs)
* @return the array of positions
*/
Expand All @@ -622,6 +634,7 @@ private int[] getItemsPosition(Item... items) {
/**
* Depending on the setting set at {@link #setSearchEnabled(int, Locale)}, checks
* whether search will be enabled or not
*
* @return true if search is enabled
*/
public boolean isSearchAvailable() {
Expand Down Expand Up @@ -658,6 +671,7 @@ public boolean isSearchAvailable() {
* Set whether search is enabled or not
* By default, search is only enabled if device's language is available
* When search is not enabled, a title will be shown on the dialog instead of the search bar
*
* @param visibility {@link #VISIBILITY_ALWAYS} to always enable search.
* {@link #VISIBILITY_NEVER} to always disable search.
* {@link #VISIBILITY_IF_LANG_AVAILABLE} to enable search only if the language is available
Expand All @@ -676,6 +690,7 @@ public IconDialog setSearchEnabled(@SearchVisibility int visibility, @Nullable L

/**
* Set the title of the dialog and when it is shown
*
* @param visibility {@link #VISIBILITY_ALWAYS} to always show the title
* {@link #VISIBILITY_NEVER} to never show the title
* {@link #VISIBILITY_IF_NO_SEARCH} to only show the title if search is not available
Expand All @@ -691,6 +706,7 @@ public IconDialog setTitle(@TitleVisibility int visibility, @Nullable String tit
/**
* Set list header options
* By default, headers are shown and are sticky
*
* @param show whether to show the headers or not
* @param sticky if headers are shown, whether they appear on top of the list when scrolling down
* @return the dialog
Expand All @@ -705,6 +721,7 @@ public IconDialog setShowHeaders(boolean show, boolean sticky) {
* Set whether the select button and the other dialog dialog buttons are shown
* If not, dialog will be dismissed immediately after an icon is clicked.
* By default, selection is shown. It is always shown if multiple selection is allowed
*
* @param show whether select button is shown or not
* @return the dialog
*/
Expand All @@ -715,6 +732,7 @@ public IconDialog setShowSelectButton(boolean show) {

/**
* Set initial selected icons
*
* @param iconIds varargs of icons id, null or empty array for no initial selection
* @return the dialog
*/
Expand All @@ -729,6 +747,7 @@ public IconDialog setSelectedIcons(@Nullable int... iconIds) {

/**
* Set initial selected icons
*
* @param icons varargs of icons, null or empty array for no initial selection
* @return the dialog
*/
Expand All @@ -750,6 +769,7 @@ public IconDialog setSelectedIcons(@Nullable Icon... icons) {

/**
* Set maximum number of icons that can be selected
*
* @param max maximum number
* @param showMessage If true, a message will be shown when maximum selection is reached
* User will need to deselect icons to select others
Expand All @@ -774,6 +794,7 @@ public IconDialog setMaxSelection(int max, boolean showMessage) {
/**
* Set whether to show the neutral clear button to unselect all icons
* By default, this button is not shown.
*
* @param show whether to show it or not
* @return the dialog
*/
Expand All @@ -788,6 +809,7 @@ public IconDialog setShowClearButton(boolean show) {
* and will be sorted by category, then by labels and then by ID
* in its value. Multiple search terms can be separated with either " ", "," or ";"
* A custom icon searcher can be set by subclassing {@link IconFilter}
*
* @param filter icon filter
* @return the dialog
*/
Expand All @@ -802,6 +824,7 @@ public IconDialog setIconFilter(BaseIconFilter filter) {
/**
* Get the IconFilter used to search and sort icons.
* You can set additionnal settings with it
*
* @return the icon searcher
* @see IconFilter#setDisabledCategories(int...)
* @see IconFilter#setDisabledIcons(int...)
Expand All @@ -814,6 +837,7 @@ public BaseIconFilter getIconFilter() {
/**
* Set whether all icon drawables will be preloaded when dialog is shown to allow a smoother
* scrolling in the icon list. By default, drawables are preloaded.
*
* @param load whether to load them or not
* @return the dialog
* @see IconHelper#loadIconDrawables()
Expand Down Expand Up @@ -1053,6 +1077,7 @@ public interface Callback {
/**
* Called on the target fragment, parent fragment or parent activity when
* icons are selected and user confirms the selection.
*
* @param icons selected icons, never null.
*/
void onIconDialogIconsSelected(@NonNull Icon[] icons);
Expand Down
69 changes: 40 additions & 29 deletions icondialog/src/main/java/com/maltaisn/icondialog/IconHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.XmlRes;
import android.util.Log;
import android.util.SparseArray;

Expand All @@ -39,6 +36,10 @@
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.XmlRes;

@SuppressWarnings({"WeakerAccess", "unused"})
public class IconHelper {

Expand All @@ -55,25 +56,25 @@ public class IconHelper {
private static final String XML_ATTR_PATH = "path";
private static final String XML_ATTR_CATEGORY = "category";

public static final int CATEGORY_PEOPLE = 0;
public static final int CATEGORY_HOME = 1;
public static final int CATEGORY_PEOPLE = 0;
public static final int CATEGORY_HOME = 1;
public static final int CATEGORY_TECHNOLOGY = 2;
public static final int CATEGORY_FINANCE = 3;
public static final int CATEGORY_LEISURE = 4;
public static final int CATEGORY_TRANSPORT = 5;
public static final int CATEGORY_FOOD = 6;
public static final int CATEGORY_BUILDINGS = 7;
public static final int CATEGORY_ARTS = 8;
public static final int CATEGORY_SPORTS = 9;
public static final int CATEGORY_AUDIO = 10;
public static final int CATEGORY_PHOTO = 11;
public static final int CATEGORY_NATURE = 12;
public static final int CATEGORY_SCIENCE = 13;
public static final int CATEGORY_TIME = 14;
public static final int CATEGORY_TOOLS = 15;
public static final int CATEGORY_COMPUTER = 16;
public static final int CATEGORY_ARROWS = 17;
public static final int CATEGORY_SYMBOLS = 18;
public static final int CATEGORY_FINANCE = 3;
public static final int CATEGORY_LEISURE = 4;
public static final int CATEGORY_TRANSPORT = 5;
public static final int CATEGORY_FOOD = 6;
public static final int CATEGORY_BUILDINGS = 7;
public static final int CATEGORY_ARTS = 8;
public static final int CATEGORY_SPORTS = 9;
public static final int CATEGORY_AUDIO = 10;
public static final int CATEGORY_PHOTO = 11;
public static final int CATEGORY_NATURE = 12;
public static final int CATEGORY_SCIENCE = 13;
public static final int CATEGORY_TIME = 14;
public static final int CATEGORY_TOOLS = 15;
public static final int CATEGORY_COMPUTER = 16;
public static final int CATEGORY_ARROWS = 17;
public static final int CATEGORY_SYMBOLS = 18;

// Warning here is ignored see: http://stackoverflow.com/a/40235834/5288316
@SuppressLint("StaticFieldLeak")
Expand All @@ -85,8 +86,10 @@ public class IconHelper {
private List<Label> groupLabels;
private SparseArray<Category> categories;

private @XmlRes int extraIconsXml;
private @XmlRes int extraLabelsXml;
private @XmlRes
int extraIconsXml;
private @XmlRes
int extraLabelsXml;
private boolean extraIconsSet;
private boolean extraIconsLoadPending;

Expand Down Expand Up @@ -128,6 +131,7 @@ public void onDone() {

/**
* Get the instance of IconHelper
*
* @param context any context
* @return the instance
*/
Expand All @@ -147,6 +151,7 @@ public boolean isDataLoaded() {

/**
* Get an icon
*
* @param id id of the icon
* @return the icon, null if it doesn't exist or if data isn't loaded
*/
Expand All @@ -162,6 +167,7 @@ SparseArray<Icon> getIcons() {

/**
* Get a label by name
*
* @param name name of the label
* @return the label, null if it doesn't exist or if data isn't loaded
*/
Expand All @@ -187,6 +193,7 @@ public Label getLabel(String name) {

/**
* Get a category
*
* @param id id of the category
* @return the category or null if it doesn't exist or if data isn't loaded
*/
Expand All @@ -198,7 +205,7 @@ public Category getCategory(int id) {

/**
* @return the number of loaded icons
* Returns 0 if data is not loaded
* Returns 0 if data is not loaded
*/
public int getIconCount() {
if (!dataLoaded) return 0;
Expand All @@ -209,7 +216,8 @@ public int getIconCount() {
* Add extra icons for the dialog.
* This can only be called once, subsequent calls will have no effect
* Both XML resources must be valid, no error checking is done
* @param iconXml xml file containing the icons
*
* @param iconXml xml file containing the icons
* @param labelXml xml file containing the labels used by the icons
*/
public synchronized void addExtraIcons(@XmlRes int iconXml, @XmlRes int labelXml) {
Expand Down Expand Up @@ -435,8 +443,9 @@ private class GroupLabelRef {

/**
* Load labels from XML
*
* @param xmlFile xml file to load from
* @param append if true, new labels will be appended to old ones
* @param append if true, new labels will be appended to old ones
*/
@SuppressWarnings({"ConstantConditions", "unchecked"})
private void loadLabels(@XmlRes int xmlFile, boolean append) {
Expand Down Expand Up @@ -589,6 +598,7 @@ private static class LabelRef {

/**
* Create new reference to a label
*
* @param name name of the label to be created. If null, parent mustn't be null
* @param parent label to add alias to. If null, name musn't be null
* @param refText reference raw text
Expand Down Expand Up @@ -617,6 +627,7 @@ private static class LabelRef {
/**
* Normalize given text, removing all diacritics, all
* unicode characters, hyphens, apostrophes and more
*
* @param text text
* @return normalized text
*/
Expand Down Expand Up @@ -674,7 +685,7 @@ public void freeIconDrawables() {
public void addLoadCallback(@NonNull LoadCallback cb) {
if (dataLoaded) {
// Data is already loaded: call callback without adding it
cb.onDataLoaded();
cb.onDataLoaded(this);
return;
}

Expand All @@ -690,7 +701,7 @@ public void removeLoadCallback(@NonNull LoadCallback cb) {
private void callLoadCallbacks() {
if (loadCallbacks != null) {
for (LoadCallback cb : loadCallbacks) {
cb.onDataLoaded();
cb.onDataLoaded(this);
}
loadCallbacks.clear();
}
Expand All @@ -701,7 +712,7 @@ public interface LoadCallback {
* Called when icon data is done loading.
* All calls to get icons, labels and categories will return null before this is called.
*/
void onDataLoaded();
void onDataLoaded(IconHelper helper);
}

}

0 comments on commit 7f34ec0

Please sign in to comment.