Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2010, Frank Blumenberg |
||
3 | // |
||
4 | // See License.txt for complete licensing and attribution information. |
||
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
||
6 | // of this software and associated documentation files (the "Software"), to deal |
||
7 | // in the Software without restriction, including without limitation the rights |
||
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
9 | // copies of the Software, and to permit persons to whom the Software is |
||
10 | // furnished to do so, subject to the following conditions: |
||
11 | // |
||
12 | // The above copyright notice and this permission notice shall be included in all |
||
13 | // copies or substantial portions of the Software. |
||
14 | // |
||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
21 | // THE SOFTWARE. |
||
22 | // |
||
23 | // /////////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | #import "SettingViewController.h" |
||
26 | #import "InAppSettings.h" |
||
27 | #import "MKConnectionController.h" |
||
28 | #import "NSData+MKCommandEncode.h" |
||
29 | #import "NSData+MKPayloadEncode.h" |
||
30 | #import "MKDatatypes.h" |
||
31 | #import "MKDataConstants.h" |
||
32 | |||
33 | @implementation SettingViewController |
||
34 | |||
35 | @synthesize setting=_setting; |
||
36 | |||
37 | #pragma mark - |
||
38 | |||
39 | - (id)initWithSettingDatasource:(NSMutableDictionary*)aSetting { |
||
40 | |||
41 | if ((self = [super initWithFile:@"Setting"])) { |
||
42 | self.setting = aSetting; |
||
43 | super.dataSource = self; |
||
44 | } |
||
45 | |||
46 | return self; |
||
47 | } |
||
48 | |||
49 | - (void)dealloc { |
||
50 | |||
51 | self.setting=nil; |
||
52 | [_setting release]; |
||
53 | [super dealloc]; |
||
54 | } |
||
55 | |||
56 | #pragma mark - |
||
57 | #pragma mark View lifecycle |
||
58 | |||
59 | - (void)viewDidLoad { |
||
60 | [super viewDidLoad]; |
||
61 | |||
62 | UIBarButtonItem* renameButton; |
||
63 | renameButton = [[[UIBarButtonItem alloc] |
||
64 | initWithTitle:NSLocalizedString(@"Save",@"Save toolbar item") |
||
65 | style:UIBarButtonItemStyleDone |
||
66 | target:self |
||
67 | action:@selector(saveSetting:)] autorelease]; |
||
68 | |||
69 | |||
70 | UIBarButtonItem* spacer; |
||
71 | spacer = [[[UIBarButtonItem alloc] |
||
72 | initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace |
||
73 | target:nil |
||
74 | action:nil] autorelease]; |
||
75 | |||
76 | UIBarButtonItem* reloadButton; |
||
77 | reloadButton = [[[UIBarButtonItem alloc] |
||
78 | initWithTitle:NSLocalizedString(@"Reload", @"Reload toolbar item") |
||
79 | style:UIBarButtonItemStyleBordered |
||
80 | target:self |
||
81 | action:@selector(reloadSetting:)] autorelease]; |
||
82 | |||
83 | [self setToolbarItems:[NSArray arrayWithObjects:renameButton,spacer,reloadButton,nil]]; |
||
84 | } |
||
85 | |||
86 | - (void)viewDidUnload { |
||
87 | [super viewDidUnload]; |
||
88 | |||
89 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
||
90 | [nc removeObserver:self]; |
||
91 | DLog(@"unload"); |
||
92 | } |
||
93 | |||
94 | #pragma mark - |
||
95 | |||
96 | // called after this controller's view will appear |
||
97 | - (void)viewWillAppear:(BOOL)animated |
||
98 | { |
||
99 | [self.navigationController setToolbarHidden:NO animated:NO]; |
||
100 | |||
101 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
||
102 | [nc addObserver:self |
||
103 | selector:@selector(readSettingNotification:) |
||
104 | name:MKReadSettingNotification |
||
105 | object:nil]; |
||
106 | |||
107 | [nc addObserver:self |
||
108 | selector:@selector(writeSettingNotification:) |
||
109 | name:MKWriteSettingNotification |
||
110 | object:nil]; |
||
111 | } |
||
112 | |||
113 | // called after this controller's view will appear |
||
114 | - (void)viewWillDisappear:(BOOL)animated { |
||
115 | |||
116 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
||
117 | [nc removeObserver:self]; |
||
118 | } |
||
119 | |||
120 | |||
121 | #pragma mark - |
||
122 | #pragma mark Save Setting |
||
123 | |||
124 | - (void)saveSetting:(id)sender { |
||
125 | |||
126 | DLog(@"save setting"); |
||
127 | |||
128 | MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController]; |
||
129 | |||
130 | NSData * payload = [NSData payloadForWriteSettingRequest:self.setting]; |
||
131 | |||
132 | NSData * data = [payload dataWithCommand:MKCommandWriteSettingsRequest |
||
133 | forAddress:MKAddressFC]; |
||
134 | |||
135 | [cCtrl sendRequest:data]; |
||
136 | } |
||
137 | |||
138 | - (void) writeSettingNotification:(NSNotification *)aNotification { |
||
139 | |||
140 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Setting" message:@"Setting saved" |
||
141 | delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
||
142 | [alert show]; |
||
143 | [alert release]; |
||
144 | } |
||
145 | |||
146 | #pragma mark - |
||
147 | #pragma mark Reload Setting |
||
148 | |||
149 | - (void)reloadSetting:(id)sender { |
||
150 | |||
151 | MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController]; |
||
152 | |||
153 | NSNumber* theIndex = [self.setting objectForKey:kMKDataKeyIndex]; |
||
154 | uint8_t index = [theIndex unsignedCharValue]; |
||
155 | |||
156 | NSData * data = [NSData dataWithCommand:MKCommandReadSettingsRequest |
||
157 | forAddress:MKAddressFC |
||
158 | payloadWithBytes:&index |
||
159 | length:1]; |
||
160 | |||
161 | [cCtrl sendRequest:data]; |
||
162 | } |
||
163 | |||
164 | - (void) readSettingNotification:(NSNotification *)aNotification { |
||
165 | |||
166 | NSDictionary* d=[aNotification userInfo]; |
||
167 | self.setting = [d mutableCopy]; |
||
168 | [super.settingsTableView reloadData]; |
||
169 | } |
||
170 | |||
171 | #pragma mark - |
||
172 | #pragma mark InAppSettingsDatasource |
||
173 | |||
174 | - (id) objectForKey:(id)aKey { |
||
175 | return [self.setting objectForKey:aKey]; |
||
176 | } |
||
177 | |||
178 | - (void) setObject:(id)anObject forKey:(id)aKey { |
||
179 | [self.setting setObject:anObject forKey:aKey]; |
||
180 | } |
||
181 | |||
182 | |||
183 | @end |