OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #import "DrawerPresenter.h" | |
6 | |
7 @interface PanePresenter : NSObject <ViewPresenter, NodePresenter> | |
8 | |
9 @property DynamicPresenter presenter; | |
10 @property bool empty; | |
11 @property UIViewController* emptyViewController; | |
12 | |
13 - (id)initWithPresenter:(DynamicPresenter)presenter NS_DESIGNATED_INITIALIZER; | |
14 | |
15 @end | |
16 | |
17 @interface DrawerPresenter () | |
18 | |
19 @property DrawerNode* root; | |
20 | |
21 @end | |
22 | |
23 @implementation DrawerPresenter { | |
24 PanePresenter* _leftPresenter; | |
25 PanePresenter* _rightPresenter; | |
26 } | |
27 | |
28 - (id)initWithCenterPresenter:(DynamicPresenter)centerPresenter | |
29 leftPresenter:(DynamicPresenter)leftPresenter | |
30 rightPresenter:(DynamicPresenter)rightPresenter { | |
31 _centerPresenter = centerPresenter; | |
32 _leftPresenter = [[PanePresenter alloc] initWithPresenter:leftPresenter]; | |
33 _rightPresenter = [[PanePresenter alloc] initWithPresenter:rightPresenter]; | |
34 return self; | |
35 } | |
36 | |
37 - (id)initWithCenterPresenter:(DynamicPresenter)centerPresenter { | |
38 return [self initWithCenterPresenter:centerPresenter | |
39 leftPresenter:nil | |
40 rightPresenter:nil]; | |
41 } | |
42 | |
43 - (id)initWithCenterPresenter:(DynamicPresenter)centerPresenter | |
44 leftPresenter:(DynamicPresenter)leftPresenter { | |
45 return [self initWithCenterPresenter:centerPresenter | |
46 leftPresenter:leftPresenter | |
47 rightPresenter:nil]; | |
48 } | |
49 | |
50 - (id)initWithCenterPresenter:(DynamicPresenter)centerPresenter | |
51 rightPresenter:(DynamicPresenter)rightPresenter { | |
52 return [self initWithCenterPresenter:centerPresenter | |
53 leftPresenter:nil | |
54 rightPresenter:rightPresenter]; | |
55 } | |
56 | |
57 - (void)presentDrawer:(DrawerNode*)node { | |
58 self.root = node; | |
59 [_centerPresenter presentNode:node.center]; | |
60 [_leftPresenter presentNode:node.left]; | |
61 [_rightPresenter presentNode:node.right]; | |
62 } | |
63 | |
64 - (void)patchDrawer:(DrawerPatch*)patch { | |
65 self.root = patch.current; | |
66 [patch.center applyTo:_centerPresenter]; | |
67 [patch.left applyTo:_leftPresenter]; | |
68 [patch.right applyTo:_rightPresenter]; | |
69 } | |
70 | |
71 - (DynamicPresenter)leftPresenter { | |
72 return _leftPresenter; | |
73 } | |
74 - (void)setLeftPresenter:(DynamicPresenter)leftPresenter { | |
75 _leftPresenter.presenter = leftPresenter; | |
76 } | |
77 | |
78 - (DynamicPresenter)rightPresenter { | |
79 return _rightPresenter; | |
80 } | |
81 - (void)setRightPresenter:(DynamicPresenter)rightPresenter { | |
82 _rightPresenter.presenter = rightPresenter; | |
83 } | |
84 | |
85 - (bool)leftVisible { | |
86 return self.root.leftVisible; | |
87 } | |
88 - (void)setLeftVisible:(bool)leftVisible { | |
89 if (self.leftVisible != leftVisible) [self toggleLeft]; | |
90 } | |
91 | |
92 - (bool)rightVisible { | |
93 return self.root.rightVisible; | |
94 } | |
95 - (void)setRightVisible:(bool)rightVisible { | |
96 if (self.rightVisible != rightVisible) [self toggleRight]; | |
97 } | |
98 | |
99 - (void)toggleLeft { | |
100 self.root.toggleLeft(); | |
101 } | |
102 | |
103 - (void)toggleRight { | |
104 self.root.toggleRight(); | |
105 } | |
106 | |
107 // TODO(zerny): This is not the right place or way to add buttons. | |
108 - (void)addDrawerButtons:(UIViewController*)controller { | |
109 [self addDrawerButtons:controller leftTitle:@"Show" rightTitle:@"Show"]; | |
110 } | |
111 | |
112 - (void)addDrawerButtons:(UIViewController *)controller | |
113 leftTitle:(NSString*)leftTitle { | |
114 [self addDrawerButtons:controller leftTitle:leftTitle rightTitle:@"Show"]; | |
115 } | |
116 | |
117 - (void)addDrawerButtons:(UIViewController *)controller | |
118 rightTitle:(NSString*)rightTitle { | |
119 [self addDrawerButtons:controller leftTitle:@"Show" rightTitle:rightTitle]; | |
120 } | |
121 | |
122 - (void)addDrawerButtons:(UIViewController*)controller | |
123 leftTitle:(NSString*)leftTitle | |
124 rightTitle:(NSString*)rightTitle { | |
125 if (![controller isKindOfClass:UINavigationController.class]) return; | |
126 UINavigationController* nav = (UINavigationController*)controller; | |
127 if (_leftPresenter.presenter != nil) { | |
128 UIBarButtonItem* leftToggle = | |
129 [[UIBarButtonItem alloc] initWithTitle:leftTitle | |
130 style:UIBarButtonItemStylePlain | |
131 target:self | |
132 action:@selector(toggleLeft)]; | |
133 nav.topViewController.navigationItem.leftBarButtonItem = leftToggle; | |
134 } | |
135 if (_rightPresenter.presenter != nil) { | |
136 UIBarButtonItem* rightToggle = | |
137 [[UIBarButtonItem alloc] initWithTitle:rightTitle | |
138 style:UIBarButtonItemStylePlain | |
139 target:self | |
140 action:@selector(toggleRight)]; | |
141 nav.topViewController.navigationItem.rightBarButtonItem = rightToggle; | |
142 } | |
143 } | |
144 | |
145 @end | |
146 | |
147 @implementation PanePresenter | |
148 | |
149 - (id)initWithPresenter:(DynamicPresenter)presenter { | |
150 self = [super init]; | |
151 self.empty = true; | |
152 self.presenter = presenter; | |
153 self.emptyViewController = [[UIViewController alloc] init]; | |
154 return self; | |
155 } | |
156 | |
157 - (void)presentNode:(Node*)node { | |
158 self.empty = [node is:EmptyPaneNode.class]; | |
159 if (!self.empty) [self.presenter presentNode:node]; | |
160 } | |
161 | |
162 - (void)patchNode:(NodePatch*)patch { | |
163 assert(!self.empty); | |
164 [self.presenter patchNode:patch]; | |
165 } | |
166 | |
167 - (UIViewController*)viewController { | |
168 if (self.presenter == nil) return nil; | |
169 return self.empty ? self.emptyViewController | |
170 : [self.presenter viewController]; | |
171 } | |
172 | |
173 @end | |
OLD | NEW |