Commit 52bd3a4e authored by Theresa's avatar Theresa Committed by Commit Bot

[EoC] Use new modelutil PropertyModelChangeProcessor

BUG=

Change-Id: I447814b1fb250062f4e204b31208c5b9b01f0fc3
Reviewed-on: https://chromium-review.googlesource.com/998738Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548614}
parent 821bce3b
......@@ -12,7 +12,17 @@ import org.chromium.chrome.browser.modelutil.PropertyObservable;
import java.util.Collections;
/** A model for the contextual suggestions UI component. */
class ContextualSuggestionsModel extends PropertyObservable<PropertyKey> {
class ContextualSuggestionsModel
extends PropertyObservable<ContextualSuggestionsModel.PropertyKey> {
/** Keys uniquely identifying model properties. */
static class PropertyKey {
static final PropertyKey CLOSE_BUTTON_ON_CLICK_LISTENER = new PropertyKey();
static final PropertyKey TITLE = new PropertyKey();
static final PropertyKey TOOLBAR_SHADOW_VISIBILITY = new PropertyKey();
private PropertyKey() {}
}
/** A {@link ListObservable} containing the current cluster list. */
class ClusterListObservable extends ListObservable {
ClusterList mClusterList = new ClusterList(Collections.emptyList());
......@@ -51,7 +61,7 @@ class ContextualSuggestionsModel extends PropertyObservable<PropertyKey> {
/** @param listener The {@link OnClickListener} for the close button. */
void setCloseButtonOnClickListener(OnClickListener listener) {
mCloseButtonOnClickListener = listener;
notifyPropertyChanged(new PropertyKey(PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER));
notifyPropertyChanged(PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER);
}
/** @return The {@link OnClickListener} for the close button. */
......@@ -62,7 +72,7 @@ class ContextualSuggestionsModel extends PropertyObservable<PropertyKey> {
/** @param title The title to display in the toolbar. */
void setTitle(String title) {
mTitle = title;
notifyPropertyChanged(new PropertyKey(PropertyKey.TITLE));
notifyPropertyChanged(PropertyKey.TITLE);
}
/** @return title The title to display in the toolbar. */
......@@ -80,7 +90,7 @@ class ContextualSuggestionsModel extends PropertyObservable<PropertyKey> {
/** @param visible Whether the toolbar shadow should be visible. */
void setToolbarShadowVisibility(boolean visible) {
mToolbarShadowVisibility = visible;
notifyPropertyChanged(new PropertyKey(PropertyKey.TOOLBAR_SHADOW_VISIBILITY));
notifyPropertyChanged(PropertyKey.TOOLBAR_SHADOW_VISIBILITY);
}
/** @return Whether the toolbar shadow should be visible. */
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.contextual_suggestions;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Contains a key uniquely identifying properties held in the {@link ContextualSuggestionsModel}.
*/
class PropertyKey {
/** The unique identifiers for properties held in the model. */
@IntDef({CLOSE_BUTTON_ON_CLICK_LISTENER, TITLE, TOOLBAR_SHADOW_VISIBILITY})
@Retention(RetentionPolicy.SOURCE)
@interface Key {}
static final int CLOSE_BUTTON_ON_CLICK_LISTENER = 0;
static final int TITLE = 1;
static final int TOOLBAR_SHADOW_VISIBILITY = 2;
@Key
Integer mKey;
PropertyKey(@Key Integer key) {
mKey = key;
}
}
......@@ -10,6 +10,8 @@ import android.view.View;
import android.view.ViewGroup;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.contextual_suggestions.ContextualSuggestionsModel.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModelChangeProcessor;
/**
* Coordinator for the toolbar sub-component. Responsible for communication with the parent
......@@ -18,7 +20,8 @@ import org.chromium.chrome.R;
class ToolbarCoordinator {
private final ContextualSuggestionsModel mModel;
private ToolbarView mToolbarView;
private ToolbarModelChangeProcessor mModelChangeProcessor;
private PropertyModelChangeProcessor<ContextualSuggestionsModel, ToolbarView, PropertyKey>
mModelChangeProcessor;
/**
* Construct a new {@link ToolbarCoordinator}.
......@@ -32,8 +35,14 @@ class ToolbarCoordinator {
mToolbarView = (ToolbarView) LayoutInflater.from(context).inflate(
R.layout.contextual_suggestions_toolbar, parentView, false);
mModelChangeProcessor = new ToolbarModelChangeProcessor(mToolbarView, mModel);
mModelChangeProcessor =
new PropertyModelChangeProcessor<>(mModel, mToolbarView, new ToolbarViewBinder());
mModel.addObserver(mModelChangeProcessor);
// The ToolbarCoordinator is created dynamically as needed, so the initial model state
// needs to be bound on creation.
mModelChangeProcessor.onPropertyChanged(mModel, PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER);
mModelChangeProcessor.onPropertyChanged(mModel, PropertyKey.TITLE);
}
/** @return The content {@link View}. */
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.contextual_suggestions;
import org.chromium.chrome.browser.modelutil.PropertyObservable;
import org.chromium.chrome.browser.modelutil.PropertyObservable.PropertyObserver;
/**
* A model change processor for use with a {@link ToolbarView}. The
* {@link ToolbarModelChangeProcessor} should be registered as a property observer of
* {@link ContextualSuggestionsModel}. Internally uses a view binder to bind model
* properties to the toolbar view.
*/
class ToolbarModelChangeProcessor implements PropertyObserver<PropertyKey> {
private static class ViewBinder {
/**
* Bind the changed property to the toolbar view.
* @param view The {@link ToolbarView} to which data will be bound.
* @param model The {@link ContextualSuggestionsModel} containing the data to be bound.
* @param propertyKey The {@link PropertyKey} of the property that has changed.
*/
private static void bindProperty(
ToolbarView view, ContextualSuggestionsModel model, PropertyKey propertyKey) {
switch (propertyKey.mKey) {
case PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER:
view.setCloseButtonOnClickListener(model.getCloseButtonOnClickListener());
break;
case PropertyKey.TITLE:
view.setTitle(model.getTitle());
break;
case PropertyKey.TOOLBAR_SHADOW_VISIBILITY:
view.setShadowVisibility(model.getToolbarShadowVisibility());
break;
default:
assert false;
}
}
}
private final ToolbarView mToolbarView;
private final ContextualSuggestionsModel mModel;
/**
* Construct a new ToolbarModelChangeProcessor.
* @param view The {@link ToolbarView} to which data will be bound.
* @param model The {@link ContextualSuggestionsModel} containing the data to be bound.
*/
ToolbarModelChangeProcessor(ToolbarView view, ContextualSuggestionsModel model) {
mToolbarView = view;
mModel = model;
// The ToolbarCoordinator is created dynamically as needed, so the initial model state
// needs to be bound on creation.
ViewBinder.bindProperty(
mToolbarView, mModel, new PropertyKey(PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER));
ViewBinder.bindProperty(mToolbarView, mModel, new PropertyKey(PropertyKey.TITLE));
}
@Override
public void onPropertyChanged(PropertyObservable<PropertyKey> source, PropertyKey propertyKey) {
ViewBinder.bindProperty(mToolbarView, mModel, propertyKey);
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.contextual_suggestions;
import org.chromium.chrome.browser.contextual_suggestions.ContextualSuggestionsModel.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModelChangeProcessor;
/**
* A view binder for use with a {@link ToolbarView}.
*/
class ToolbarViewBinder
implements PropertyModelChangeProcessor
.ViewBinder<ContextualSuggestionsModel, ToolbarView, PropertyKey> {
@Override
public void bind(ContextualSuggestionsModel model, ToolbarView view, PropertyKey propertyKey) {
if (propertyKey == PropertyKey.CLOSE_BUTTON_ON_CLICK_LISTENER) {
view.setCloseButtonOnClickListener(model.getCloseButtonOnClickListener());
} else if (propertyKey == PropertyKey.TITLE) {
view.setTitle(model.getTitle());
} else if (propertyKey == PropertyKey.TOOLBAR_SHADOW_VISIBILITY) {
view.setShadowVisibility(model.getToolbarShadowVisibility());
} else {
assert false : "Unhandled property detected.";
}
}
}
......@@ -305,10 +305,9 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/contextual_suggestions/DummyEventReporter.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/EnabledStateMonitor.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/FetchHelper.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/PropertyKey.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/ToolbarCoordinator.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/ToolbarModelChangeProcessor.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/ToolbarView.java",
"java/src/org/chromium/chrome/browser/contextual_suggestions/ToolbarViewBinder.java",
"java/src/org/chromium/chrome/browser/cookies/CanonicalCookie.java",
"java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java",
"java/src/org/chromium/chrome/browser/coordinator/CoordinatorLayoutForPointer.java",
......
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