OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_WUG_VIEW_MODEL_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_WUG_VIEW_MODEL_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "components/login/screens/screen_context.h" |
| 12 #include "components/wug/export.h" |
| 13 |
| 14 namespace base { |
| 15 class DictionaryValue; |
| 16 } |
| 17 |
| 18 namespace wug { |
| 19 |
| 20 using Context = login::ScreenContext; |
| 21 class View; |
| 22 |
| 23 // Base class for view-model. Usually View is responsible for creating and |
| 24 // initializing instances of this class. |
| 25 class WUG_EXPORT ViewModel { |
| 26 public: |
| 27 ViewModel(); |
| 28 virtual ~ViewModel(); |
| 29 |
| 30 void SetView(View* view); |
| 31 |
| 32 // Called by view to notify that children are ready. |
| 33 void OnChildrenReady(); |
| 34 |
| 35 // Called by view when it became bound (meaning that all changes in context |
| 36 // will be reflected in view right after they are made). |
| 37 virtual void OnViewBound() = 0; |
| 38 |
| 39 // Notifies view-model about context changes happended on view side. |
| 40 // View-model corrects its copy of context according to changes in |diff|. |
| 41 void UpdateContext(const base::DictionaryValue& diff); |
| 42 |
| 43 // Notifies view-model about |event| sent from view. |
| 44 virtual void OnEvent(const std::string& event); |
| 45 |
| 46 protected: |
| 47 // Scoped context editor, which automatically commits all pending |
| 48 // context changes on destruction. |
| 49 class ContextEditor { |
| 50 public: |
| 51 using KeyType = ::login::ScreenContext::KeyType; |
| 52 using String16List = ::login::String16List; |
| 53 using StringList = ::login::StringList; |
| 54 |
| 55 explicit ContextEditor(ViewModel& screen); |
| 56 ~ContextEditor(); |
| 57 |
| 58 const ContextEditor& SetBoolean(const KeyType& key, bool value) const; |
| 59 const ContextEditor& SetInteger(const KeyType& key, int value) const; |
| 60 const ContextEditor& SetDouble(const KeyType& key, double value) const; |
| 61 const ContextEditor& SetString(const KeyType& key, |
| 62 const std::string& value) const; |
| 63 const ContextEditor& SetString(const KeyType& key, |
| 64 const base::string16& value) const; |
| 65 const ContextEditor& SetStringList(const KeyType& key, |
| 66 const StringList& value) const; |
| 67 const ContextEditor& SetString16List(const KeyType& key, |
| 68 const String16List& value) const; |
| 69 |
| 70 private: |
| 71 ViewModel& view_model_; |
| 72 Context& context_; |
| 73 }; |
| 74 |
| 75 // This method is called in a time appropriate for initialization. At this |
| 76 // point it is allowed to make changes in the context, but it is not allowed |
| 77 // to access child view-models. |
| 78 virtual void Initialize() = 0; |
| 79 |
| 80 // This method is called when all children are ready. |
| 81 virtual void OnAfterChildrenReady() = 0; |
| 82 |
| 83 // This method is called after context was changed on view side. |
| 84 // |chanaged_keys| contains a list of keys of changed fields. |
| 85 virtual void OnContextChanged(const std::vector<std::string>& changed_keys) {} |
| 86 |
| 87 Context& context() { return context_; } |
| 88 const Context& context() const { return context_; } |
| 89 |
| 90 View* view() const { return view_; } |
| 91 |
| 92 // Returns scoped context editor. The editor or it's copies should not outlive |
| 93 // current BaseScreen instance. |
| 94 ContextEditor GetContextEditor(); |
| 95 |
| 96 // Notifies view about changes made in context. |
| 97 void CommitContextChanges(); |
| 98 |
| 99 private: |
| 100 Context context_; |
| 101 View* view_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ViewModel); |
| 104 }; |
| 105 |
| 106 } // namespace wug |
| 107 |
| 108 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_MODEL_H_ |
OLD | NEW |