Commit f0e581f7 authored by Matt Jones's avatar Matt Jones Committed by Commit Bot

Update Android MVC list tutorial

The ModelListAdapter now uses data providers, this patch updates the
tutorial to reflect that.

Bug: 967054
Change-Id: I012e0451dc0d72102fdc8b07d05ecbb9d051fa37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1680652Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683995}
parent f2570bf4
...@@ -10,30 +10,34 @@ label. ...@@ -10,30 +10,34 @@ label.
### Additional Resources ### Additional Resources
[Introductory MVC tutorial][mvc_tutorial_link] [Introductory MVC tutorial][mvc_tutorial_link]
#### Todo
[Update this doc to use data providers](https://crbug.com/967054)
### File Structure ### File Structure
The file structure of our component will be the following: The file structure of our component will be the following:
* ./chrome/android/java/src/org/chromium/chrome/browser/simple_menu/ * ./chrome/android/java/src/org/chromium/chrome/browser/simple_menu/
* [`SimpleMenuCoordinator.java`](#SimpleMenuCoordinator) * [`SimpleMenuCoordinator.java`](#SimpleMenuCoordinator)
* [`SimpleMenuMediator.java`](#SimpleMenuMediator)
* [`SimpleMenuItemViewBinder.java`](#SimpleMenuItemViewBinder) * [`SimpleMenuItemViewBinder.java`](#SimpleMenuItemViewBinder)
* [`SimpleMenuProperties.java`](#SimpleMenuProperties) * [`SimpleMenuProperties.java`](#SimpleMenuProperties)
* ./chrome/android/java/res/layout/ * ./chrome/android/java/res/layout/
* [`simple_menu_item.xml`](#simple_menu_item_xml) * [`simple_menu_item.xml`](#simple_menu_item_xml)
### SimpleMenuCoordinator ### SimpleMenuCoordinator
This class will own the ListAdapter that knows how to show PropertyModels. In this example we'll be This class will own the ```ModelListAdapter``` that knows how to show ```PropertyModels```. In
combining the responsibilities of what would otherwise be the coordinator and mediator for this example we'll be combining the responsibilities of what would otherwise be the coordinator
simplicity. and mediator for simplicity.
```java ```java
public class SimpleMenuCoordinator { public class SimpleMenuCoordinator {
private SimpleMenuMediator mMediator;
public SimpleMenuCoordinator(Context context, ListView listView) { public SimpleMenuCoordinator(Context context, ListView listView) {
ModelListAdapter adapter = new ModelListAdapter(); ModelList listItems = new ModelList();
final LayoutInflater layoutInflater = context.getSystemService(LAYOUT_INFLATER_SERVICE); // Once this is attached to the ListView, there is no need to hold a reference to it.
ModelListAdapter adapter = new ModelListAdapter(listItems);
final LayoutInflater layoutInflater =
(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
// If this is a heterogeneous list, register more than one type. // If this is a heterogeneous list, register more than one type.
adapter.registerType( adapter.registerType(
...@@ -43,27 +47,34 @@ public class SimpleMenuCoordinator { ...@@ -43,27 +47,34 @@ public class SimpleMenuCoordinator {
listView.setAdapter(adapter); listView.setAdapter(adapter);
List<Pair<Integer, PropertyModel>> items = new ArrayList<>(); mMediator = new SimpleMenuMediator(context, listItems);
PropertyModel listModel1 = generateListItem( }
ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.icon), }
context.getResources().getString(R.string.label)); ```
// The list adapter needs to be told what kind of view should render the data; hence the
// pair object. ### SimpleMenuMediator
list.add(new Pair(ListItemType.DEFAULT, listModel1)); This class is responsible for pushing updates into the ```ModelList```. Updates to that
object are automatically pushed and bound to the list view. For a more complex system, the
```ModelList``` may be part of a larger ```PropertyModel``` that the mediator maintains.
```java
class SimpleMenuMediator {
// ... add other list items as needed. Typically this work is done in the mediator piece of private ModelList mModelList;
// the component.
adapter.updateModels(items); SimpleMenuMediator(Context context, ModelList modelList) {
mModelList = modelList;
PropertyModel itemModel = generateListItem(
ApiCompatibilityUtils.getDrawable(context.getResources(), R.drawable.icon),
context.getResources().getString(R.string.label));
mModelList.add(new ModelListAdapter.ListItem(ListItemType.DEFAULT, itemModel));
} }
public PropertyModel generateListItem(Drawable icon, String text) { private PropertyModel generateListItem(Drawable icon, String text) {
return PropertyModel.Builder(SimpleMenuProperties.ALL_KEYS) return new PropertyModel.Builder(SimpleMenuProperties.ALL_KEYS)
.with(SimpleMenuProperties.ICON, icon) .with(SimpleMenuProperties.ICON, icon)
.with(SimpleMenuProperties.LABEL, text) .with(SimpleMenuProperties.LABEL, text)
.with(SimpleMenuProperties.CLICK_LISTENER, (view) -> handleClick(view)) .with(SimpleMenuProperties.CLICK_LISTENER, (view) -> handleClick(view))
.build(); .build();
// Click handling can be done as above or the listener can be passed in.
} }
private void handleClick(View view) { private void handleClick(View view) {
...@@ -72,6 +83,7 @@ public class SimpleMenuCoordinator { ...@@ -72,6 +83,7 @@ public class SimpleMenuCoordinator {
} }
``` ```
### SimpleMenuProperties ### SimpleMenuProperties
These are the types of data that we want to apply to each list item in our menu. These are the types of data that we want to apply to each list item in our menu.
```java ```java
...@@ -122,7 +134,6 @@ class SimpleMenuItemViewBinder { ...@@ -122,7 +134,6 @@ class SimpleMenuItemViewBinder {
view.setOnClickListener(model.get(SimpleMenuProperties.CLICK_LISTENER)); view.setOnClickListener(model.get(SimpleMenuProperties.CLICK_LISTENER));
} }
} }
} }
``` ```
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment