espagram/Espagram/EspagramTestViewController.m
2012-12-20 18:09:11 +01:00

198 lines
7.1 KiB
Objective-C

//
// EspagramTestViewController.m
// Espagram
//
// Created by Abel Fokkinga on 11/8/12.
// Copyright (c) 2012 Abel Fokkinga. All rights reserved.
//
#import "EspagramTestViewController.h"
#import "NSMutableArray_Shuffling.h"
@interface EspagramTestViewController ()
@property (nonatomic, strong) NSMutableArray * testSet;
@property (nonatomic, strong) TestableVerb * currentVerb;
@property (nonatomic) int correctAnswersInCurrentSet;
@property (nonatomic) int wrongAnswersInCurrentSet;
@end
@implementation EspagramTestViewController
@synthesize lesson = _lesson;
@synthesize testedVerbLabel = _testedVerbLabel;
@synthesize verbMeaningLable = _testedVerbLable;
@synthesize verbPersonLabel = _verbPersonLabel;
@synthesize answer1Button = _answer1Button;
@synthesize answer2Button = _answer2Button;
@synthesize answer3Button = _answer3Button;
@synthesize answer4Button = _answer4Button;
@synthesize nextButton = _nextButton;
@synthesize testSet = _testSet;
@synthesize currentVerb = _currentVerb;
@synthesize correctAnswersInCurrentSet = _correctAnswersInCurrentSet;
@synthesize wrongAnswersInCurrentSet = _wrongAnswersInCurrentSet;
- (NSMutableArray *)testSet{
// Check if there are any verbs in the set
if ( _testSet.count == 0) {
_testSet = [self.lesson testableVerbs];
self.correctAnswersInCurrentSet = 0;
self.wrongAnswersInCurrentSet = 0;
}
return _testSet;
};
- (IBAction)EditingDidEnd:(UITextField *)sender {
[self resignFirstResponder];
}
- (IBAction)answered:(UIButton *)sender {
NSString * correctAnswer = [self.lesson.getConjugationEngine conjugateVerb:self.currentVerb.verb.verb inPerson:self.currentVerb.person andTense:[self.lesson getTenseAsTense]];
if ( [sender.titleLabel.text isEqualToString:correctAnswer]) {
[self setUILabelTitle:self.verbPersonLabel withText:[[[[[[self.lesson getConjugationEngine] persons] objectForKey:self.currentVerb.person] stringByAppendingString:@" "] stringByAppendingString:correctAnswer] stringByAppendingString:@" \u2705"] inColour:[UIColor greenColor] andSize:16];
} else {
[self setUILabelTitle:self.verbPersonLabel withText:[[[[[[self.lesson getConjugationEngine] persons] objectForKey:self.currentVerb.person] stringByAppendingString:@" "] stringByAppendingString:sender.titleLabel.text] stringByAppendingString:@" \u274C"] inColour:[UIColor redColor] andSize:16];
// Set current test verb to failed
self.currentVerb.failed = true;
}
}
- (id) popLastFromArray:(NSMutableArray *)a
{
id lastObject = nil;
if ( a && a.count > 0 ) {
lastObject = [a lastObject];
NSLog(@"Popping: %@", lastObject);
[a removeLastObject];
}
return lastObject;
}
- (IBAction)nextButtonPressed:(id)sender {
[self.testSet removeLastObject];
[self nextVerb];
}
- (void)setAnswerButton:(UIButton *)button withAnswer:(NSString *)answer andCorrect:(NSString *)correct
{
[self setUIButtonTitle:button withText:answer inColour:[UIColor blackColor] forState:UIControlStateNormal];
if ( [answer isEqualToString:correct])
[self setUIButtonTitle:button withText:answer inColour:[UIColor greenColor] forState:UIControlStateHighlighted];
else
[self setUIButtonTitle:button withText:answer inColour:[UIColor greenColor] forState:UIControlStateHighlighted];
}
- (void) setUIButtonTitle:(UIButton *)button withText:(NSString *) titleString inColour:(UIColor *) colour forState:(UIControlState)controlState
{
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:titleString attributes:@{ NSFontAttributeName :
[UIFont fontWithName:@"Noteworthy-Bold" size:16],
// NSUnderlineStyleAttributeName : @1,
NSStrokeColorAttributeName : colour}];
[button setAttributedTitle:title forState:controlState];
}
- (void) setUILabelTitle:(UILabel *)label withText:(NSString *) titleString inColour:(UIColor *) colour andSize:(int) fontSize;
{
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:titleString attributes:@{ NSFontAttributeName :
[UIFont fontWithName:@"Noteworthy-Bold" size:fontSize],
// NSUnderlineStyleAttributeName : @1,
NSStrokeColorAttributeName : colour
}];
label.attributedText = title;
}
- (void) nextVerb {
NSMutableArray * answers;
if ( self.currentVerb && self.currentVerb.failed) {
// Last test failed.. so we won't remove it
// instead we shuffle the test set.
[self.testSet shuffle];
// increase wrongly answered counter
self.wrongAnswersInCurrentSet += 1;
} {
// Last verb answered correctly
[self.testSet lastObject];
// increase wrongly answered counter
self.correctAnswersInCurrentSet += 1;
}
self.currentVerb = self.testSet.lastObject;
if ( self.currentVerb) {
NSLog(@"Testing verb %@ in %@", self.currentVerb.verb.verb, self.currentVerb.person);
NSString * correctAnswer = [self.lesson.getConjugationEngine conjugateVerb:self.currentVerb.verb.verb inPerson:self.currentVerb.person andTense:[self.lesson getTenseAsTense]];
NSLog(@"Correct answer: %@", correctAnswer);
// Set display label to verb to test
[self setUILabelTitle:self.testedVerbLabel withText:self.currentVerb.verb.verb inColour:[UIColor blackColor] andSize:30];
[self setUILabelTitle:self.verbMeaningLable withText:self.currentVerb.verb.meaning inColour:[UIColor blackColor] andSize:12];
[self setUILabelTitle:self.verbPersonLabel withText:[[[self.lesson getConjugationEngine] persons] objectForKey:self.currentVerb.person] inColour:[UIColor blackColor] andSize:16];
// Get the shuffles answers;
answers = [self.lesson.getConjugationEngine getTestConjugationsForVerb:self.currentVerb.verb.verb inPerson:self.currentVerb.person andTense:[self.lesson getTenseAsTense]];
[self setAnswerButton:self.answer1Button withAnswer:[self popLastFromArray:answers] andCorrect:correctAnswer];
[self setAnswerButton:self.answer2Button withAnswer:[self popLastFromArray:answers] andCorrect:correctAnswer];
[self setAnswerButton:self.answer3Button withAnswer:[self popLastFromArray:answers] andCorrect:correctAnswer];
[self setAnswerButton:self.answer4Button withAnswer:[self popLastFromArray:answers] andCorrect:correctAnswer];
} else {
NSLog(@"Que horror, no lesson!");
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.nextButton.titleLabel.text = NSLocalizedString(@"Next", @"Next button in the testing screen to continue to the next verb");
[self.nextButton setTitle:NSLocalizedString(@"Next", @"Next button in the testing screen to continue to the next verb") forState:UIControlStateNormal];
self.title = [self.lesson getTenseName];
if ( [self.testSet count] > 0) {
[self nextVerb];
}
}
@end