Skip to content

Commit

Permalink
[TIMOB-16494]: Android - added support for multiple markers for ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
hieupham007 committed Apr 17, 2015
1 parent 4bd8cb6 commit efcfd92
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
Expand Up @@ -866,6 +866,10 @@ private boolean hideHeaderOrFooter() {
return (listview.getSearchText() != null && filterIndices.isEmpty());
}

public boolean hasHeader() {
return (headerTitle != null || headerView != null);
}

public boolean isHeaderView(int pos) {
return (headerView != null && pos == 0);
}
Expand Down
Expand Up @@ -25,7 +25,6 @@

import ti.modules.titanium.ui.UIModule;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;

@Kroll.proxy(creatableInModule = UIModule.class, propertyAccessors = {
Expand Down Expand Up @@ -60,7 +59,7 @@ public class ListViewProxy extends TiViewProxy {
//indicate if user attempts to add/modify/delete sections before TiListView is created
private boolean preload = false;
private ArrayList<ListSectionProxy> preloadSections;
private HashMap<String, Integer> preloadMarker;
private ArrayList<HashMap<String, Integer>> preloadMarkers;

public ListViewProxy() {
super();
Expand All @@ -72,6 +71,7 @@ public TiUIView createView(Activity activity) {

public void handleCreationArgs(KrollModule createdInModule, Object[] args) {
preloadSections = new ArrayList<ListSectionProxy>();
preloadMarkers = new ArrayList<HashMap<String, Integer>>();
defaultValues.put(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE, UIModule.LIST_ITEM_TEMPLATE_DEFAULT);
defaultValues.put(TiC.PROPERTY_CASE_INSENSITIVE_SEARCH, true);
super.handleCreationArgs(createdInModule, args);
Expand Down Expand Up @@ -108,9 +108,9 @@ public void setPreload(boolean pload)
preload = pload;
}

public HashMap<String, Integer> getPreloadMarker()
public ArrayList<HashMap<String, Integer>> getPreloadMarkers()
{
return preloadMarker;
return preloadMarkers;
}

private void addPreloadSections(Object secs, int index, boolean arrayOnly) {
Expand Down Expand Up @@ -181,11 +181,25 @@ public void setMarker(Object marker) {
if (listView != null) {
((TiListView)listView).setMarker(m);
} else {
preloadMarker = m;
preloadMarkers.clear();
preloadMarkers.add(m);
}
}
}

@Kroll.method
public void addMarker(Object marker)
{
if (marker instanceof HashMap) {
HashMap<String, Integer> m = (HashMap<String, Integer>) marker;
TiUIView listView = peekView();
if (listView != null) {
((TiListView)listView).addMarker(m);
} else {
preloadMarkers.add(m);
}
}
}

@Override
public boolean handleMessage(final Message msg) {
Expand Down
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -73,7 +74,7 @@ public class TiListView extends TiUIView implements OnSearchChangeListener {
private int headerFooterId;
public static LayoutInflater inflater;
private int titleId;
private int[] marker = new int[2];
private ArrayList<Pair<Integer,Integer>> markers = new ArrayList<Pair<Integer,Integer>>();
private View headerView;
private View footerView;
private String searchText;
Expand Down Expand Up @@ -225,12 +226,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
ListSectionProxy section = info.first;
int sectionItemIndex = info.second.second;
int sectionIndex = info.second.first;
//check marker
if (sectionIndex > marker[0] || (sectionIndex == marker[0] && sectionItemIndex >= marker[1])) {
proxy.fireEvent(TiC.EVENT_MARKER, null, false);
resetMarker();
}


View content = convertView;

//Handles header/footer views and titles.
Expand All @@ -246,6 +242,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
return content;
}

//check marker and fire appropriate events
checkMarker(sectionIndex, sectionItemIndex, section.hasHeader());

//Handling templates
KrollDict data = section.getListItemData(sectionItemIndex);
TiListViewTemplate template = section.getTemplateByIndex(sectionItemIndex);
Expand Down Expand Up @@ -278,12 +277,10 @@ public TiListView(TiViewProxy proxy, Activity activity) {
caseInsensitive = true;

//handling marker
HashMap<String, Integer> preloadMarker = ((ListViewProxy)proxy).getPreloadMarker();
if (preloadMarker != null) {
setMarker(preloadMarker);
} else {
resetMarker();
}
ArrayList<HashMap<String, Integer>> preloadMarkers = ((ListViewProxy)proxy).getPreloadMarkers();
if (preloadMarkers != null) {
setMarkers(preloadMarkers);
}

//initializing listView and adapter
ListViewWrapper wrapper = new ListViewWrapper(activity);
Expand Down Expand Up @@ -384,11 +381,6 @@ public boolean getCaseInsensitive() {
return caseInsensitive;
}

private void resetMarker()
{
marker[0] = Integer.MAX_VALUE;
marker[1] = Integer.MAX_VALUE;
}

public void setHeaderTitle(String title) {
TextView textView = (TextView) headerView.findViewById(titleId);
Expand All @@ -414,9 +406,48 @@ public void registerForTouch()

public void setMarker(HashMap<String, Integer> markerItem)
{
marker[0] = markerItem.get(TiC.PROPERTY_SECTION_INDEX);
marker[1] = markerItem.get(TiC.PROPERTY_ITEM_INDEX);
markers.clear();
addMarker(markerItem);
}


public void setMarkers(ArrayList<HashMap<String, Integer>> markerItems)
{
markers.clear();
for (int i = 0; i < markerItems.size(); ++i) {
HashMap<String, Integer> markerItem = markerItems.get(i);
addMarker(markerItem);
}
}

public void checkMarker(int sectionIndex, int sectionItemIndex, boolean hasHeader)
{
if (markers.isEmpty()) {
return;
}

if (hasHeader) {
sectionItemIndex--;
}

Iterator<Pair<Integer, Integer>> iterator = markers.iterator();
while (iterator.hasNext()) {
Pair<Integer, Integer> marker = iterator.next();
if (sectionIndex == marker.first && sectionItemIndex == marker.second) {
KrollDict data = new KrollDict();
data.put(TiC.PROPERTY_SECTION_INDEX, sectionIndex);
data.put(TiC.PROPERTY_ITEM_INDEX, sectionItemIndex);
proxy.fireEvent(TiC.EVENT_MARKER, data, false);
iterator.remove();
}
}
}

public void addMarker(HashMap<String, Integer> markerItem)
{
int sectionIndex = markerItem.get(TiC.PROPERTY_SECTION_INDEX);
int itemIndex = markerItem.get(TiC.PROPERTY_ITEM_INDEX);
markers.add(new Pair<Integer, Integer>(sectionIndex, itemIndex));
}

public void processProperties(KrollDict d) {
Expand Down
29 changes: 25 additions & 4 deletions apidoc/Titanium/UI/ListView.yml
Expand Up @@ -268,9 +268,9 @@ events:
type: Boolean

- name: marker
summary: Fired when the list view displays the reference item or an item beyond the reference item.
summary: Fired when the list view displays the reference item.
description: |
This event is fired only once. Use this in conjunction with the [setMarker](Titanium.UI.ListView.setMarker) method.
This event is fired only once per reference item. Use this in conjunction with the [setMarker](Titanium.UI.ListView.setMarker) and [addMarker](Titanium.UI.ListView.addMarker) methods.
On iOS this method does not fire when list view is in search mode.
platforms: [iphone, ipad, android]
since: 3.2.0
Expand All @@ -279,6 +279,14 @@ events:
summary: false. This event does not bubble.
type: Boolean

- name: sectionIndex
summary: section index of the reference item.
type: Number

- name: itemIndex
summary: section item index of the reference item.
type: Number

- name: move
summary: Fired when a list row is moved to a different location by the user.
description: |
Expand Down Expand Up @@ -888,13 +896,25 @@ methods:
- name: setMarker
summary: Sets a reference item in the list view.
description: |
This method replace previous reference items with the current one.
See [marker](Titanium.UI.ListView.marker) event for details.
parameters:
- name: markerProps
summary: Dictionary to describe the reference item.
type: ListViewMarkerProps
platforms: [iphone, ipad, android]
since: 3.2.0

- name: addMarker
summary: Adds a reference item in the list view.
description: |
See [marker](Titanium.UI.ListView.marker) event for details.
parameters:
- name: markerProps
summary: Dictionary to describe the reference item.
type: ListViewMarkerProps
platforms: [iphone, ipad, android]
since: 4.1.0

examples:
- title: List View Sections
Expand Down Expand Up @@ -1372,11 +1392,12 @@ properties:

---
name: ListViewMarkerProps
summary: The parameter for [setMarker](Titanium.UI.ListView.setMarker) method.
summary: The parameter for [setMarker](Titanium.UI.ListView.setMarker) and [addMarker](Titanium.UI.ListView.addMarker) methods.
description: |
Use this in conjunction with the [setMarker](Titanium.UI.ListView.setMarker) method. For example:
Use this in conjunction with [setMarker](Titanium.UI.ListView.setMarker) and [addMarker](Titanium.UI.ListView.addMarker) methods. For example:
setMarker({sectionIndex:5, itemIndex:10});
addMarker({sectionIndex:7, itemIndex: 2});
since: 3.2.0
platforms: [iphone, ipad, android]

Expand Down

0 comments on commit efcfd92

Please sign in to comment.