espagram/Espagram/EspagramTestViewController.m
2013-02-21 17:08:51 +01:00

244 lines
9.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"
#import "TestResult+Create.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 verbTestTenseLabel = _verbTestTenseLabel;
@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;
@synthesize testProgressIndicator = _testProgressIndicator;
@synthesize testStatusLabel = _testStatusLabel;
- (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;
};
- (void) updateTestProgress
{
if ( self.testSet.count == 0 ) {
self.testProgressIndicator.progress = 0;
} else {
self.testProgressIndicator.progress = self.correctAnswersInCurrentSet * 1.0 / (self.testSet.count + self.correctAnswersInCurrentSet);
}
self.testStatusLabel.text = [NSString stringWithFormat:@"%@: %d %@: %d %@: %d",NSLocalizedString(@"Correct",@"Number of correct answers"),self.correctAnswersInCurrentSet,
NSLocalizedString(@"Error",@"Number of incorrect answers"),self.wrongAnswersInCurrentSet,
NSLocalizedString(@"Total",@"Number of total conjugations in test"),self.testSet.count+self.correctAnswersInCurrentSet];
}
- (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;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"user pressed Continue");
}
else {
NSLog(@"user pressed Cancel");
}
}
- (IBAction)nextButtonPressed:(id)sender {
if ( self.testSet.count == 1 &&
self.correctAnswersInCurrentSet > 0 &&
self.currentVerb.failed == false) {
//Create UIAlertView alert
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Congratulations",@"Congratulations title")
message:NSLocalizedString(@"You succesfully completed this lesson!",@"Lesson completed succesfully message")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Continue",@"Quit lesson completed completed alert message") otherButtonTitles: nil];
// Show alert
[alert show];
}
[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;
// Add test result
if ( self.currentVerb) {
[TestResult addTestableVerbResult:self.currentVerb withTestType:@"Multiple Choice"];
if ( self.currentVerb.failed) {
// Last test failed.. so we won't remove it
// instead we shuffle the test set.
self.currentVerb.failed = false;
[self.testSet shuffle];
// increase wrongly answered counter
self.wrongAnswersInCurrentSet += 1;
} else {
// Last verb answered correctly
[self.testSet removeLastObject];
// increase correct answered counter
self.correctAnswersInCurrentSet += 1;
}
}
// Update progress indicator
[self updateTestProgress];
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.title = NSLocalizedString(@"Multiple Choice",@"Multiple Choice test");
self.verbTestTenseLabel.text = [self.lesson getLocalizedTense];
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];
if ( [self.testSet count] > 0) {
[self nextVerb];
}
}
@end