forked from cubewang/NewsReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCardViewController_iPad.m
582 lines (461 loc) · 18.5 KB
/
WordCardViewController_iPad.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//
// WordCardViewController_iPad.m
// EnglishFun
//
// Created by cg on 12-8-3.
// Copyright (c) 2012年 iKnow Team. All rights reserved.
//
#import "WordCardViewController_iPad.h"
static const int ddLogLevel = LOG_FLAG_ERROR;
@interface WordCardViewController_iPad ()
@end
@implementation WordCardViewController_iPad
@synthesize word = _word;
@synthesize downloaderList;
@synthesize keyLabel, pronLabel, acceptationTableView, sentenceTableView, activityIndicator;
@synthesize favoriteButton, pronunciationButton;
@synthesize descriptionTextField;
@synthesize keyboardRect;
@synthesize context;
@synthesize fetchedResultsController;
@synthesize translator, listen, remarks, favorites, close;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.sentenceTableView.delegate = self;
self.sentenceTableView.dataSource = self;
self.acceptationTableView.delegate = self;
self.acceptationTableView.dataSource = self;
self.translator.text = NSLocalizedString(@"翻译",@"");
self.listen.text = NSLocalizedString(@"例句",@"");
self.remarks.text = NSLocalizedString(@"备注",@"");
self.close.text = NSLocalizedString(@"关闭",@"");
self.favorites.text = NSLocalizedString(@"收藏",@"");
self.descriptionTextField.placeholder = NSLocalizedString(@"这里输入备注", @"");
NSMutableString *phoneticSymbolString;
if (_word.PhoneticSymbol)
{
phoneticSymbolString = [[[NSMutableString alloc] initWithString:@"/"] autorelease];
[phoneticSymbolString appendFormat:@"%@/", _word.PhoneticSymbol];
}
else {
phoneticSymbolString = @"";
}
if ([_word.Pronunciation length] < 5) {
pronunciationButton.enabled = NO;
}
self.keyLabel.text = _word.Key;
self.pronLabel.text = phoneticSymbolString;
self.downloaderList = [[NSMutableArray alloc] init];
[self.downloaderList release];
self.context = [[[EnglishFunAppDelegate sharedAppDelegate] getClient] getContext];
[self getWords:_word.Key];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
isWordFavorite = FALSE;
if ([sectionInfo numberOfObjects] >= 1)
{
NSString *key = [[[sectionInfo objects] objectAtIndex:0] valueForKey:@"key"];
if ([key isEqualToString:_word.Key]) {
self.descriptionTextField.text = [[[sectionInfo objects] objectAtIndex:0] valueForKey:@"remark"];
[favoriteButton setImage:[UIImage imageNamed:@"favourite_on.png"] forState:UIControlStateNormal];
self.favorites.text = NSLocalizedString(@"已收藏",@"");
isWordFavorite = TRUE;
}
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void) keyboardWillShow:(NSNotification *)notification {
// get keyboard size and loctaion
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
keyboardRect = keyboardBounds;
CGRect containerFrame = self.view.frame;
containerFrame.origin.y = self.view.bounds.size.height + 20 - (keyboardBounds.size.height + containerFrame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
self.view.frame = containerFrame;
[UIView commitAnimations];
}
-(void) keyboardWillHide:(NSNotification *)notification{
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
CGRect containerFrame = self.view.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
self.view.frame = containerFrame;
// commit animations
[UIView commitAnimations];
}
#pragma mark touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches
withEvent:event];
[descriptionTextField resignFirstResponder];
}
#pragma mark -
#pragma mark DownloaderDelegate
- (void)downloader:(Downloader *)downloader didDownloadData:(NSData *)data {
if (data == nil)
return;
if (player != nil)
{
[player stop];
RELEASE_SAFELY(player);
}
NSError *error;
player = [[AVAudioPlayer alloc] initWithData:data error:&error];
if (!player)
{
DDLogError(@"Error: %@", [error localizedDescription]);
return;
}
else {
[player prepareToPlay];
[player play];
}
}
- (void)downloader:(Downloader *)downloader didFailWithError:(NSString *)error{
DDLogError(@"download audio error");
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (tableView == acceptationTableView)
{
NSArray *keys = [_word.AcceptationList allKeys];
if (keys.count == 0)
return 1;
return keys.count;
}
else if (tableView == sentenceTableView)
{
return _word.SentenceList.count;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == acceptationTableView)
{
static NSString *CellIdentifier = @"CellAcceptation";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *keys = [_word.AcceptationList allKeys];
NSString *str;
if ([keys count] == 0)
{
str = NSLocalizedString(@"暂无翻译", @"");
}
else
{
NSString *key = [keys objectAtIndex:indexPath.row];
NSString *value = [_word.AcceptationList objectForKey:key];
str = [[NSString alloc] initWithFormat:@"%@ %@", key, value];
}
// Set
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = str;
[str release];
}
else if (tableView == sentenceTableView)
{
// Get cell
static NSString *CellIdentifier = @"CellSentence";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Sentence *sentence = [_word.SentenceList count] == 0 ? nil : [_word.SentenceList objectAtIndex:indexPath.row];
// Configure the cell.
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = sentence.Orig ? sentence.Orig : @"";
cell.detailTextLabel.font = [UIFont systemFontOfSize:17];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = sentence.Trans;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == acceptationTableView)
{
NSArray *keys = [_word.AcceptationList allKeys];
NSString *str;
if ([keys count] == 0)
{
str = NSLocalizedString(@"暂无翻译", @"");
}
else
{
NSString *key = [keys objectAtIndex:indexPath.row];
NSString *value = [_word.AcceptationList objectForKey:key];
str = [[NSString alloc] initWithFormat:@"%@ %@", key, value];
}
UIFont *cellFont = [UIFont systemFontOfSize:15];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [str sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 10;
}
else if (tableView == sentenceTableView)
{
Sentence *sentence = [_word.SentenceList count] == 0 ? nil : [_word.SentenceList objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont systemFontOfSize:15];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize origLabelSize = [sentence.Orig sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGSize transLabelSize = [sentence.Trans sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return origLabelSize.height + transLabelSize.height + 10;
}
return 40.0f;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Downloader *downloader = nil;
if (tableView == sentenceTableView && _word != nil && _word.SentenceList != nil)
{
Sentence *sentence = [_word.SentenceList count] == 0 ? nil : [_word.SentenceList objectAtIndex:indexPath.row];
if (sentence.Pron)
{
// Download sound data
Downloader *downloader = [[Downloader alloc] initWithString:sentence.Pron
delegate:self
connectionType:ConnectionTypeSynchronously
downloaderType:DownloaderTypeData];
[downloader download];
[downloaderList addObject:downloader];
[downloader release];
}
}
}
- (IBAction)favoriteButtonClicked:(UIButton *)sender
{
if (!isWordFavorite)
{
[favoriteButton setImage:[UIImage imageNamed:@"favourite_on.png"] forState:UIControlStateNormal];
self.favorites.text = NSLocalizedString(@"已收藏",@"");
if (_word)
{
//添加备注
_word.Description = descriptionTextField.text;
[self addWord:_word];
}
}
else
{
[favoriteButton setImage:[UIImage imageNamed:@"favourite_off.png"] forState:UIControlStateNormal];
self.favorites.text = NSLocalizedString(@"收藏",@"");
[self deleteWord];
}
}
- (IBAction)closeCard:(id)sender
{
if (player)
[player stop];
BOOL bAnimation = sender != nil;
if ([self respondsToSelector:@selector(presentingViewController)]) {
if (self.presentedViewController) {
[[self presentedViewController] dismissModalViewControllerAnimated:bAnimation];
}
else {
[self dismissModalViewControllerAnimated:bAnimation];
}
}
else {
[[self parentViewController] dismissModalViewControllerAnimated:bAnimation];
}
}
- (IBAction)pronunce:(id)sender
{
if (_word == nil || _word.Pronunciation == nil)
return;
// Download sound data
Downloader *downloader = [[Downloader alloc] initWithString:_word.Pronunciation
delegate:self
connectionType:ConnectionTypeSynchronously
downloaderType:DownloaderTypeData];
downloader.useCacheFirst = YES;
[downloader download];
[downloaderList addObject:downloader];
[downloader release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
//[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark CoreData
- (void) getWords:(NSString *)query
{
// Init a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"iWord" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
// Apply an ascending sort for items
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES selector:nil];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];
// Recover query
if (query && query.length)
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"key ==[cd] %@", query];
// Init the fetched results controller
NSError *error;
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController.delegate = self;
[self.fetchedResultsController release];
if (![[self fetchedResultsController] performFetch:&error])
DDLogError(@"Error: %@", [error localizedDescription]);
[fetchRequest release];
[sortDescriptor release];
}
- (BOOL) addWord:(Word *)newWord
{
if (newWord == nil)
return FALSE;
// build a new iWord and set its field
iWord *word = (iWord *)[NSEntityDescription
insertNewObjectForEntityForName:@"iWord"
inManagedObjectContext:self.context];
word.key = newWord.Key;
word.remark = newWord.Description;
if ( newWord.AcceptationList != nil && [newWord.AcceptationList count] > 0)
{
NSString *key = [[newWord.AcceptationList allKeys] objectAtIndex:0];
word.acceptation = [newWord.AcceptationList valueForKey:key];
}
else {
word.acceptation = @"";
}
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString* dateString = [dateFormat stringFromDate:date];
word.createDate = dateString;
word.section = [[newWord.Key substringToIndex:1] uppercaseString];
[dateFormat release];
//同步单词
BOOL result = [iKnowAPI addWord:newWord];
if (!result) {
DDLogError(@"addWord failed.");
}
// save the new item
NSError *error;
if (![self.context save:&error])
{
DDLogError(@"Error: %@", [error localizedDescription]);
[self getWords:_word.Key];
return NO;
}
else {
[self getWords:_word.Key];
isWordFavorite = TRUE;
return YES;
}
}
- (BOOL) deleteWord
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
if ([sectionInfo numberOfObjects] == 0) {
[self getWords:_word.Key];
return TRUE;
}
//从服务器删除单词
BOOL result = [iKnowAPI deleteWord:_word.Key];
if (!result) {
DDLogError(@"addWord failed.");
}
NSError *error = nil;
[self.context deleteObject:[[sectionInfo objects] objectAtIndex:0]];
if (![self.context save:&error])
{
DDLogError(@"Error: %@", [error localizedDescription]);
[self getWords:_word.Key];
return FALSE;
}
else {
[self getWords:_word.Key];
isWordFavorite = FALSE;
return TRUE;
}
}
- (void)dealloc {
[_word release];
self.keyLabel = nil;
self.pronLabel = nil;
self.acceptationTableView = nil;
self.sentenceTableView = nil;
self.activityIndicator = nil;
self.favoriteButton = nil;
self.pronunciationButton = nil;
self.descriptionTextField = nil;
self.context = nil;
self.fetchedResultsController = nil;
self.translator = nil;
self.listen = nil;
self.remarks = nil;
self.favorites =nil;
self.close = nil;
for (Downloader *downloader in downloaderList) {
[downloader cancel];
}
self.downloaderList = nil;
[super dealloc];
}
@end