-
Notifications
You must be signed in to change notification settings - Fork 3
/
NewWordsViewController.m
executable file
·549 lines (442 loc) · 20.4 KB
/
NewWordsViewController.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
//
// NewWordsViewController.m
// Iwordsv2
//
// Created by yaonphy on 12-11-20.
// Copyright (c) 2012年 yang. All rights reserved.
//
#import "NewWordsViewController.h"
#import "WordInfo.h"
#import "TableDelegate.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "AVFoundation/AVFoundation.h"
#ifndef WORD_FT
#define WORD_FT [UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0f]
#endif
#ifndef PRON_FT
#define PRON_FT [UIFont fontWithName:@"HelveticaNeue" size:14.0f]
#endif
#ifndef MEAN_FT
#define MEAN_FT [UIFont fontWithName:@"HelveticaNeue" size:16.0f]
#endif
NSString *const BaseURLString = @"http://dict-co.iciba.com/api/dictionary.php?w=";
@interface NewWordsViewController ()
{
}
@end
@implementation NewWordsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)loadInitial
{
UIView * selfView = [self view];
_wordInfoArr = [[NSMutableArray alloc]init];
_wordsListArr = [[NSMutableArray alloc]init];
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
_numPerDay = [userDefault integerForKey:@"NumPerDay"];
_learnedNum = [userDefault integerForKey:@"LearnedNum"];
_maxItemNum = [userDefault integerForKey:@"MaxItemNum"];
_maxMeanNum = [userDefault integerForKey:@"MaxMeanNum"];
userDefault = nil;
[self loadWordsList];
_prefix = [[NSString alloc]initWithString:@"| "];
_suffix = [[NSString alloc] initWithString:@" |"];
_scroIndex = 0;
_addToReViewButton = [[NOIMGButton alloc]initWithFrame:CGRectMake(20, 5, 120, 30) style:NVUIGradientButtonStyleBlackTranslucent];
[_addToReViewButton setText:@"加入复习计划"];
[_addToReViewButton setTintColor:NAV_TINT_COLOR forState:UIControlStateHighlighted];
[_addToReViewButton setBorderColor:MAIN_BG_COLOR forState:UIControlStateNormal];
[_addToReViewButton setBorderColor:MAIN_BG_COLOR forState:UIControlStateHighlighted];
[_addToReViewButton addTarget:self action:@selector(addToReViewButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[selfView addSubview:_addToReViewButton];
_playAudioButton = [[NOIMGButton alloc]initWithFrame:CGRectMake(180, 5, 120, 30) style:NVUIGradientButtonStyleBlackTranslucent];
[_playAudioButton setText:@"播放语音"];
[_playAudioButton setTintColor:NAV_TINT_COLOR forState:UIControlStateHighlighted];
[_playAudioButton setBorderColor:MAIN_BG_COLOR forState:UIControlStateNormal];
[_playAudioButton setBorderColor:MAIN_BG_COLOR forState:UIControlStateHighlighted];
[_playAudioButton addTarget:self action:@selector(playAudioButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[selfView addSubview:_playAudioButton];
_mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, 320, 340)];
[_mainScrollView setDelegate:self];
[_mainScrollView setBackgroundColor:[UIColor whiteColor]];
[_mainScrollView setPagingEnabled:YES];
int i = 0;
for (i = 0; i < [[self wordsListArr] count]; i++) {
@autoreleasepool {
NSString * theURLStr = [BaseURLString stringByAppendingString:[[self wordsListArr] objectAtIndex:i]];
NSURL * theURL = [[NSURL alloc]initWithString:theURLStr];
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc]initWithURL:theURL];
NSData * downLoadData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
theURLStr = nil;
[theURL release];
theURL = nil;
[theRequest release];
theRequest = nil;
if (downLoadData) {
TBXML * theTBXML = [[TBXML alloc]initWithXMLData:downLoadData error:nil];
[self initMemo];
[self traverseElement:theTBXML.rootXMLElement];
[theTBXML release];
theTBXML = nil;
UIScrollView * subScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(_curIndex*320, 0,320, 340)];
/*加载解析好的数据....*/
WordInfo * curInfo = [[self wordInfoArr] objectAtIndex:_curIndex];
/*单词和音标....固定*/
UILabel * wordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 320, 30)];
[wordLabel setFont:WORD_FT];
if ([curInfo theWord]) {
[wordLabel setText:[curInfo theWord]];
}
[subScrollView addSubview:wordLabel];
[wordLabel release];
wordLabel = nil;
UILabel *pronouncelabel = [[UILabel alloc]initWithFrame:CGRectMake(22, 50, 320, 20)];
[pronouncelabel setFont:PRON_FT];
if ([[curInfo thePSArr]count]) {
NSString * tmpStr = [[self prefix] stringByAppendingString:[[curInfo thePSArr] objectAtIndex:0]];
NSString * tmp2Strs = [tmpStr stringByAppendingString:[self suffix]];
tmpStr = nil;
[pronouncelabel setText:tmp2Strs];
tmp2Strs = nil;
}
[subScrollView addSubview:pronouncelabel];
[pronouncelabel release];
pronouncelabel = nil;
/*单词和音标....*/
/*词性和对应的释意....*/
NSMutableArray * curPosArr = [curInfo thePosArr];
NSMutableArray * curAccArr = [curInfo theAcceptationArr];
UILabel * meanLabel = [[UILabel alloc]init];
if ([curPosArr count] && [curAccArr count]) {
NSInteger curItemNum = 0;
if (([self maxItemNum] > [curPosArr count])) {
curItemNum = [curAccArr count];
}else
{
curItemNum = [self maxItemNum];
}
NSString * begainStr = [[NSString alloc]initWithString:@""];
NSString * finalPoint = nil;
for (int i = 0; i < curItemNum; i++) {
finalPoint = [begainStr stringByAppendingString:[curPosArr objectAtIndex:i]];
finalPoint = [finalPoint stringByAppendingString:@" "];
finalPoint = [finalPoint stringByAppendingString:[curAccArr objectAtIndex:i]];
finalPoint = [finalPoint stringByAppendingString:@"\n"];
[begainStr release];
begainStr = [[NSString alloc]initWithString:finalPoint];
finalPoint = nil;
}
_meanLabelSize = [begainStr sizeWithFont:MEAN_FT constrainedToSize:CGSizeMake(280,960) lineBreakMode:NSLineBreakByWordWrapping];
[meanLabel setNumberOfLines:0];
[meanLabel setFont:MEAN_FT];
[meanLabel setFrame:CGRectMake(20,80,280,_meanLabelSize.height)];
[meanLabel setText:begainStr];
[begainStr release];
begainStr = nil;
}
[subScrollView addSubview:meanLabel];
[meanLabel release];
/*词性和对应的示意....*/
/*例句和对应的示意....*/
NSMutableArray * curOriArr = [curInfo theOrigArr];
NSMutableArray * curTraArr = [curInfo theTransArr];
UILabel * originLabel = [[UILabel alloc]init];
if ([curPosArr count] && [curAccArr count]) {
NSInteger curMeanNum = 0;
if (([self maxMeanNum] > [curOriArr count])) {
curMeanNum = [curOriArr count];
}else
{
curMeanNum = [self maxMeanNum];
}
NSString * begainStr22 = [[NSString alloc]initWithString:@"例句:\n"];
NSString * finalPoint22 = nil;
for (int i = 0; i < curMeanNum; i++) {
finalPoint22 = [begainStr22 stringByAppendingString:[curOriArr objectAtIndex:i]];
finalPoint22 = [finalPoint22 stringByAppendingString:@"\n"];
finalPoint22 = [finalPoint22 stringByAppendingString:[curTraArr objectAtIndex:i]];
finalPoint22 = [finalPoint22 stringByAppendingString:@"\n"];
[begainStr22 release];
begainStr22 = [[NSString alloc]initWithString:finalPoint22];
finalPoint22 = nil;
}
_originLabelSize = [begainStr22 sizeWithFont:MEAN_FT constrainedToSize:CGSizeMake(280,960) lineBreakMode:NSLineBreakByWordWrapping];
[originLabel setNumberOfLines:0];
[originLabel setFont:MEAN_FT];
[originLabel setFrame:CGRectMake(20,90 + _meanLabelSize.height,280,_originLabelSize.height)];
[originLabel setText:begainStr22];
[begainStr22 release];
begainStr22 = nil;
}
[subScrollView addSubview:originLabel];
[originLabel release];
/*例句和对应的示意....*/
[subScrollView setShowsVerticalScrollIndicator:YES];
[subScrollView setShowsHorizontalScrollIndicator:NO];
_finalContentHeight = 100 + _meanLabelSize.height + _originLabelSize.height;
[subScrollView setContentSize:CGSizeMake(320, _finalContentHeight)];
[subScrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[[self mainScrollView] addSubview:subScrollView];
[subScrollView release];
subScrollView = nil;
}
downLoadData = nil;
}
}
[_mainScrollView setContentSize:CGSizeMake(320*[[self wordsListArr]count], 340)];
[selfView addSubview:_mainScrollView];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationItem] setTitle:@"学习中"];
[[self view]setBackgroundColor:MAIN_BG_COLOR];
NSUserDefaults * preUserDefault = [NSUserDefaults standardUserDefaults];
_numPerDay = [preUserDefault integerForKey:@"NumPerDay"];
_learnedNum = [preUserDefault integerForKey:@"LearnedNum"];
preUserDefault = nil;
[self loadInitial];
}
-(void)learnedButtonTouched:(id) sender
{
[[self navigationController] popToRootViewControllerAnimated:YES];
[[NSUserDefaults standardUserDefaults] setInteger:[[self wordsListArr] count] + [self numPerDay] forKey:@"LearnedNum"];
}
-(void)initMemo
{
WordInfo *theInfo = [[WordInfo alloc]init];
NSMutableArray * thePSArr = [[NSMutableArray alloc]init];
NSMutableArray * thePronArr = [[NSMutableArray alloc]init];
NSMutableArray * thePosArr = [[NSMutableArray alloc]init];
NSMutableArray * theAcceptationArr = [[NSMutableArray alloc]init];
NSMutableArray * theOrigArr = [[NSMutableArray alloc]init];
NSMutableArray * theTransArr = [[NSMutableArray alloc]init];
[theInfo setThePSArr:thePSArr];
[thePSArr release];
thePSArr = nil;
[theInfo setThePronArr:thePronArr];
[thePronArr release];
thePronArr = nil;
[theInfo setThePosArr:thePosArr];
[thePosArr release];
thePosArr = nil;
[theInfo setTheAcceptationArr:theAcceptationArr];
[theAcceptationArr release];
theAcceptationArr = nil;
[theInfo setTheOrigArr:theOrigArr];
[theOrigArr release];
theOrigArr = nil;
[theInfo setTheTransArr:theTransArr];
[theTransArr release];
theTransArr = nil;
[[self wordInfoArr]addObject:theInfo];
[theInfo release];
theInfo = nil;
NSLog(@"%d\n",[[self wordInfoArr]count]);
_curIndex = [[self wordInfoArr] count]-1;
}
/* 解析XML */
- (void) traverseElement:(TBXMLElement *)element {
WordInfo *theInfo = [[self wordInfoArr] objectAtIndex:[self curIndex]];
do {
// Display the name of the element
// NSLog(@"%@",[TBXML elementName:element]);
NSString * elementName = [TBXML elementName:element];
if ([elementName isEqualToString:@"key"]) {
NSString * theWord = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!theWord) {
theWord = @"";
}
[theInfo setTheWord:theWord];
[theWord release];
theWord = nil;
}
if ([elementName isEqualToString:@"ps"]) {
NSString * thePS = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!thePS) {
thePS = @"";
}
[[theInfo thePSArr] addObject:thePS];
[thePS release];
thePS = nil;
}
if ([elementName isEqualToString:@"pron"]) {
NSString * thePron = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!thePron) {
thePron = @"";
}
[[theInfo thePronArr] addObject:thePron];
[thePron release];
thePron = nil;
}
if ([elementName isEqualToString:@"pos"]) {
NSString * thePos = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!thePos) {
thePos = @"";
}
[[theInfo thePosArr] addObject:thePos];
[thePos release];
thePos = nil;
}
if ([elementName isEqualToString:@"acceptation"]) {
NSString * theAcception = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!theAcception) {
theAcception = @"";
}
[[theInfo theAcceptationArr] addObject:theAcception];
[theAcception release];
theAcception = nil;
}
if ([elementName isEqualToString:@"orig"]) {
NSString * theOrig = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!theOrig) {
theOrig = @"";
}
[[theInfo theOrigArr] addObject:theOrig];
[theOrig release];
theOrig = nil;
}
if ([elementName isEqualToString:@"trans"]) {
if (element) {
NSString * theTrans = [[NSString alloc]initWithCString:element->text encoding:NSUTF8StringEncoding];
if (!theTrans) {
theTrans = @"";
}
[[theInfo theTransArr] addObject:theTrans];
[theTrans release];
theTrans = nil;
}
}
// NSLog(@"%s\n",element->text);
elementName = nil;
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
theInfo = nil;
}
/* 解析XML */
-(void)loadWordsList
{
@autoreleasepool {
//对词库分文件的选择没有实现
NSInteger fileNum = [self learnedNum] / 200;
NSString * curLibName = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:@"curLibName"];
NSString * fileName = [[NSString alloc]initWithFormat:@"%@_%i",curLibName,fileNum];
NSString * sourcePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
NSDictionary * listDic = [[NSDictionary alloc]initWithContentsOfFile:sourcePath];
int i = [self learnedNum] + 1;
int total = [self numPerDay] + [self learnedNum];
for (; i <= total; i++) {
NSString * wordsID = [[NSString alloc]initWithFormat:@"%d",i];
[[self wordsListArr] addObject:[listDic objectForKey:wordsID]];
[wordsID release];
wordsID = nil;
}
[fileName release];
fileName = nil;
[listDic release];
listDic = nil;
}
}
#pragma UIScrollView Delegate method
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat curOffSetX = self.mainScrollView.contentOffset.x;
NSInteger nextIndex = (NSInteger)curOffSetX/320;
[self setScroIndex:nextIndex];
NSLog(@"%i",nextIndex);
BOOL isAuto = [[NSUserDefaults standardUserDefaults] boolForKey:@"IsAutoPlay"];
if (isAuto) {
[self playWordMP3];
}
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
CGFloat curOffSetX = self.mainScrollView.contentOffset.x;
if (curOffSetX > ([[self wordsListArr]count] - 1)*320) {
UIView * doneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];
[doneView setBackgroundColor:[UIColor whiteColor]];
_learnedButton = [[NOIMGButton alloc]initWithFrame:CGRectMake(20, 160, 260, 40)style:NVUIGradientButtonStyleDefault];
[_learnedButton addTarget:self action:@selector(learnedButtonTouched:)forControlEvents:UIControlEventTouchUpInside];
[_learnedButton setText:@"学习完成"];
[doneView addSubview:_learnedButton];
[[self view] addSubview:doneView];
[doneView release];
doneView = nil;
}
}
#pragma end
-(void)addToReViewButtonTouched:(id)sender
{
@autoreleasepool {
NSString * theWord = [[[self wordInfoArr]objectAtIndex:[self scroIndex]] theWord];
NSAssert(theWord, @"无法插入空的单词");
NSString * docStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * dbPath = [docStr stringByAppendingPathComponent:@"review.sqlite"];
FMDatabase * theReviewDB = [FMDatabase databaseWithPath:dbPath];
if ([theReviewDB open]) {
NSString * sql = @"insert into ReviewTable (word) values(?) ";
BOOL res = [theReviewDB executeUpdate:sql,theWord];
if (!res) {
NSLog(@"error to insert data");
} else {
NSLog(@"succ to insert data");
}
[theReviewDB close];
}
docStr = nil;
dbPath = nil;
theWord = nil;
NSInteger reviewNum = [[NSUserDefaults standardUserDefaults] integerForKey:@"reviewTotalNumber"];
[[NSUserDefaults standardUserDefaults] setInteger:(reviewNum + 1) forKey:@"reviewTotalNumber"];
}
}
-(void)playWordMP3
{
if (([[self wordInfoArr]count] - 1) < [self scroIndex]) {
return;
}
NSString * mp3Str = [[[[self wordInfoArr]objectAtIndex:[self scroIndex]] thePronArr]objectAtIndex:0];
NSLog(@"%@\n",mp3Str);
NSURL * mp3Url = [[NSURL alloc]initWithString:mp3Str];
mp3Str = nil;
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:mp3Url error:nil];
if (avPlayer) {
[avPlayer play];
}
[mp3Url release];
[avPlayer release];
}
-(void)playAudioButtonTouched:(id)sender
{
[self playWordMP3];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[_prefix release];
[_suffix release];
[_wordInfoArr release];
[_wordsListArr release];
[_addToReViewButton release];
[_playAudioButton release];
[_mainScrollView release];
[_learnedButton release];
[super dealloc];
}
@end