Matthew Jones | 871569f1 | 2019-04-03 16:25:48 | [diff] [blame] | 1 | # So, you want to do MVC... |
| 2 | |
| 3 | ### Overview |
| 4 | A full explanation of the MVC framework can be found [here](https://docs.google.com/document/d/1nP9NjTvsSMZvkR_aWRZPdy67wRINomgQ7AfEtbsIwzg). This document is intended to go over the logistics of the most basic implementation of the framework in Chrome’s codebase. |
| 5 | |
| 6 | For this example, we’ll be implementing a simple progress bar; a rectangle that changes length based on the loading state of the underlying webpage. |
| 7 | |
| 8 | #### Additional Resources |
Matthew Jones | b514f98 | 2019-05-30 17:49:18 | [diff] [blame^] | 9 | [Simple MVC lists](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ui/android/mvc_simple_list_tutorial.md) |
Matthew Jones | 871569f1 | 2019-04-03 16:25:48 | [diff] [blame] | 10 | [Testing MVC primer doc](https://docs.google.com/document/d/1Mel7f4lE_osFjnttkxu1wcUf_k9CmIzPv6oxwCw9tx4/edit#) |
| 11 | |
| 12 | #### File Structure |
| 13 | The file structure of our component will be the following: |
| 14 | * ./org/chromium/chrome/browser/simple_progress/ |
| 15 | * [`SimpleProgressCoordinator.java`](#SimpleProgressCoordinator) |
| 16 | * [`SimpleProgressMediator.java`](#SimpleProgressMediator) |
| 17 | * [`SimpleProgressViewBinder.java`](#SimpleProgressViewBinder) |
| 18 | * [`SimpleProgressProperties.java`](#SimpleProgressProperties) |
| 19 | * `SimpleProgressView.java` (assuming it requires interesting logic) |
| 20 | |
| 21 | #### SimpleProgressCoordinator |
| 22 | The class responsible for setting up the component. This should be the only public class in the component's package and is the only class with direct access to the mediator. |
| 23 | |
| 24 | ```java |
| 25 | public class SimpleProgressCoordinator { |
| 26 | |
| 27 | private SimpleProgressMediator mMediator; |
| 28 | |
| 29 | public SimpleProgressCoordinator (Tab tabProgressBarIsFor) { |
| 30 | |
| 31 | PropertyModel model = new PropertyModel.Builder(SimpleProgressProperties.ALL_KEYS) |
| 32 | .with(SimpleProgressProperties.PROGRESS_FRACTION, 0f) |
| 33 | .with(SimpleProgressProperties.FOREGROUND_COLOR, Color.RED) |
| 34 | .build(); |
| 35 | |
| 36 | // This view can come from multiple places, in this case we find it in the existing |
| 37 | // view hierarchy. |
| 38 | View view = tabProgressBarIsFor.getActivity().findViewById( |
| 39 | R.id.my_simple_progress_bar); |
| 40 | |
| 41 | PropertyModelChangeProcessor.create(model, view, SimpleProgressViewBinder::bind); |
| 42 | |
| 43 | mMediator = new SimpleProgressMediator(model, tabProgressBarIsFor); |
| 44 | } |
| 45 | |
| 46 | public void destroy() { |
| 47 | mMediator.destroy(); |
| 48 | } |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | #### SimpleProgressMediator |
| 53 | The class that handles all of the signals coming from the outside world. External classes should never interact with this class directly. |
| 54 | |
| 55 | ```java |
| 56 | class SimpleProgressMediator extends EmptyTabObserver { |
| 57 | |
| 58 | private PropertyModel mModel; |
| 59 | |
| 60 | private Tab mObservedTab; |
| 61 | |
| 62 | public SimpleProgressMediator(PropertyModel model, Tab tabProgressBarIsFor) { |
| 63 | mModel = model; |
| 64 | mObservedTab = tabProgressBarIsFor; |
| 65 | mObservedTab.addObserver(this); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void onLoadProgressChanged(Tab tab, int progress) { |
| 70 | mModel.set(SimpleProgressProperties.PROGRESS_FRACTION, progress / 100f); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onDidChangeThemeColor(Tab tab, int color) { |
| 75 | mModel.set(SimpleProgressProperties.FOREGROUND_COLOR, color); |
| 76 | } |
| 77 | |
| 78 | void destroy() { |
| 79 | // Be sure to clean up anything that needs to be (in this case, detach the tab |
| 80 | // observer). |
| 81 | mObservedTab.removeObserver(this); |
| 82 | } |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | #### SimpleProgressViewBinder |
| 87 | The class responsible for applying a model to a specific view. In general there is a 1:1 relationship between a type of view and its binder. Multiple binders can know how to take a single type of model and apply it to a view. The binder's method should be stateless; this is implied by the 'static' identifier. |
| 88 | |
| 89 | ```java |
| 90 | class SimpleProgressViewBinder { |
| 91 | |
| 92 | public static void bind(PropertyModel model, View view, PropertyKey propertyKey) { |
| 93 | if (SimpleProgressProperties.PROGRESS_FRACTION == propertyKey) { |
| 94 | |
| 95 | // Apply width modification to 'view' here. |
| 96 | |
| 97 | } else if (SimpleProgressProperties.FOREGROUND_COLOR == propertyKey) { |
| 98 | |
| 99 | // Apply color modification to 'view' here. |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | #### SimpleProgressProperties |
| 107 | These are properties associated with the view the model will be applied to. |
| 108 | |
| 109 | ```java |
| 110 | class SimpleProgressProperties { |
| 111 | |
| 112 | public static final WritableFloatPropertyKey PROGRESS_FRACTION = |
| 113 | new WritableFloatPropertyKey(); |
| 114 | |
| 115 | public static final WritableIntPropertyKey FOREGROUND_COLOR = |
| 116 | new WritableIntPropertyKey(); |
| 117 | |
| 118 | public static final PropertyKey[] ALL_KEYS = {PROGRESS_FRACTION, FOREGROUND_COLOR}; |
| 119 | } |
| 120 | ``` |
| 121 | |