-
Notifications
You must be signed in to change notification settings - Fork 3
/
ReviewViewController.m
executable file
·153 lines (120 loc) · 4.94 KB
/
ReviewViewController.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
//
// ReviewViewController.m
// Iwordsv2
//
// Created by yaonphy on 12-11-20.
// Copyright (c) 2012年 yang. All rights reserved.
//
#import "ReviewViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
@interface ReviewViewController ()
@end
@implementation ReviewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)loadWordsList
{
NSUserDefaults *thisDef = [NSUserDefaults standardUserDefaults];
[self setNumPerDay:[thisDef integerForKey:@"ReNumPreDay"]];
[self setLearnedNum:[thisDef integerForKey:@"ReviewedNum"]];
@autoreleasepool {
NSString * docStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * dbPath = [docStr stringByAppendingPathComponent:@"review.sqlite"];
FMDatabase * theReviewDB = [FMDatabase databaseWithPath:dbPath];
NSInteger reviewSum = [[NSUserDefaults standardUserDefaults] integerForKey:@"reviewTotalNumber"];
int fromNum = [self learnedNum] + 1;
int toNum = 0;
if ([self learnedNum] + [self numPerDay] > reviewSum) {
toNum = reviewSum;
}else
{
toNum = [self learnedNum] + [self numPerDay];
}
if ([theReviewDB open]) {
NSString * sqlMent = [[NSString alloc]initWithFormat:@" select * from ReviewTable where id between %i and %i",fromNum,toNum];
FMResultSet * rs = [theReviewDB executeQuery:sqlMent];
NSLog(@"%p\n",rs);
while ([rs next]) {
int userId = [rs intForColumn:@"id"];
NSString * thisWord = [rs stringForColumn:@"word"];
[[self wordsListArr] addObject:thisWord];
NSLog(@"user id = %d, word = %@\n", userId,thisWord);
}
[theReviewDB close];
[sqlMent release];
sqlMent = nil;
}
docStr = nil;
dbPath = nil;
}
}
- (void)viewDidLoad
{
// [super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationItem] setTitle:@"复习中"];
[[self view]setBackgroundColor:MAIN_BG_COLOR];
[self loadInitial];
[[self addToReViewButton]setText:@"加入单词本"];
[[self learnedButton]setText:@"完成复习"];
}
-(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]];
NOIMGButton * learnedButton = [[NOIMGButton alloc]initWithFrame:CGRectMake(20, 160, 260, 40)style:NVUIGradientButtonStyleDefault];
[learnedButton addTarget:self action:@selector(learnedButtonTouched:)forControlEvents:UIControlEventTouchUpInside];
[learnedButton setText:@"复习完成"];
[doneView addSubview:learnedButton];
[learnedButton release];
learnedButton = nil;
[[self view] addSubview:doneView];
[doneView release];
doneView = nil;
}
}
-(void)learnedButtonTouched:(id) sender
{
[[self navigationController] popToRootViewControllerAnimated:YES];
[[NSUserDefaults standardUserDefaults] setInteger:[[self wordsListArr] count] + [self learnedNum] forKey:@"ReviewedNum"];
}
-(void)addToReViewButtonTouched:(id)sender
{
@autoreleasepool {
NSString * theWord = [[self wordsListArr]objectAtIndex:[self scroIndex]];
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 VocabTable (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:@"vocabTotalNumber"];
[[NSUserDefaults standardUserDefaults] setInteger:(reviewNum + 1) forKey:@"vocabTotalNumber"];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end