252 lines
9.4 KiB
Objective-C
252 lines
9.4 KiB
Objective-C
//
|
|
// EspagramTypingTestViewController.m
|
|
// Espagram
|
|
//
|
|
// Created by Abel Fokkinga on 1/9/13.
|
|
// Copyright (c) 2013 Abel Fokkinga. All rights reserved.
|
|
//
|
|
|
|
#import "EspagramTypingTestViewController.h"
|
|
#import "TestResult+Create.h"
|
|
#import "NSMutableArray_Shuffling.h"
|
|
|
|
@interface EspagramTypingTestViewController ()
|
|
|
|
@property (nonatomic, strong) NSMutableArray * testSet;
|
|
@property (nonatomic, strong) TestableVerb * currentVerb;
|
|
@property (nonatomic) int correctAnswersInCurrentSet;
|
|
@property (nonatomic) int wrongAnswersInCurrentSet;
|
|
|
|
@end
|
|
|
|
@implementation EspagramTypingTestViewController
|
|
|
|
@synthesize lesson = _lesson;
|
|
@synthesize testedVerbLabel = _testedVerbLabel;
|
|
@synthesize verbMeaningLable = _testedVerbLable;
|
|
@synthesize verbPersonLabel = _verbPersonLabel;
|
|
@synthesize verbAnswerTextField = _verbAnswerTextField;
|
|
@synthesize nextButton = _nextButton;
|
|
@synthesize showAnswerButton = _showAnswerButton;
|
|
@synthesize checkAnswerButton = _checkAnswerButton;
|
|
@synthesize testSet = _testSet;
|
|
@synthesize currentVerb = _currentVerb;
|
|
@synthesize correctAnswersInCurrentSet = _correctAnswersInCurrentSet;
|
|
@synthesize wrongAnswersInCurrentSet = _wrongAnswersInCurrentSet;
|
|
@synthesize verbTestTenseLabel = _verbTestTenseLabel;
|
|
@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];
|
|
}
|
|
|
|
- (void)setLesson:(Lesson *)lesson{
|
|
if ( _lesson != lesson){
|
|
// Clear current test set
|
|
self.testSet = nil;
|
|
// set Tense label
|
|
self.verbTestTenseLabel.text = [self.lesson getLocalizedTense];
|
|
_lesson = lesson;
|
|
}
|
|
}
|
|
|
|
- (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) checkAnswer:(NSString *) answer {
|
|
|
|
NSString * correctAnswer = [self.lesson.getConjugationEngine conjugateVerb:self.currentVerb.verb.verb inPerson:self.currentVerb.person andTense:[self.lesson getTenseAsTense]];
|
|
|
|
|
|
if ( [answer 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:answer] stringByAppendingString:@" \u274C"] inColour:[UIColor redColor] andSize:16];
|
|
|
|
// Set current test verb to failed
|
|
self.currentVerb.failed = true;
|
|
}
|
|
}
|
|
|
|
|
|
- (void) nextVerb {
|
|
|
|
// Add test result
|
|
if ( self.currentVerb) {
|
|
[TestResult addTestableVerbResult:self.currentVerb withTestType:@"Typing"];
|
|
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
self.currentVerb = self.testSet.lastObject;
|
|
|
|
// Update progress indicator
|
|
[self updateTestProgress];
|
|
|
|
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];
|
|
|
|
|
|
} else {
|
|
NSLog(@"Que horror, no lesson!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
- (IBAction)checkAnswerButtonPressed {
|
|
[self.verbAnswerTextField resignFirstResponder];
|
|
[self checkAnswer:self.verbAnswerTextField.text];
|
|
}
|
|
|
|
|
|
- (IBAction)showCorrectAnswerButtonPressed:(id)sender {
|
|
[self.verbAnswerTextField resignFirstResponder];
|
|
|
|
NSString * correctAnswer = [self.lesson.getConjugationEngine conjugateVerb:self.currentVerb.verb.verb inPerson:self.currentVerb.person andTense:[self.lesson getTenseAsTense]];
|
|
|
|
[self setUILabelTitle:self.verbPersonLabel withText:[[[[[[self.lesson getConjugationEngine] persons] objectForKey:self.currentVerb.person] stringByAppendingString:@" "] stringByAppendingString:correctAnswer] stringByAppendingString:@" \u2705"] inColour:[UIColor greenColor] andSize:16];
|
|
|
|
}
|
|
|
|
- (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];
|
|
self.verbAnswerTextField.text = @"";
|
|
}
|
|
|
|
- (IBAction)answerEntered:(id)sender {
|
|
[self resignFirstResponder];
|
|
[self checkAnswer:self.verbAnswerTextField.text];
|
|
|
|
}
|
|
|
|
- (IBAction)addAccentedCharacter:(UIButton *)sender {
|
|
self.verbAnswerTextField.text = [self.verbAnswerTextField.text stringByAppendingString:sender.titleLabel.text];
|
|
}
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
{
|
|
[super viewWillAppear:animated];
|
|
|
|
self.title = NSLocalizedString(@"Typing",@"Typing 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];
|
|
|
|
self.checkAnswerButton.titleLabel.text = NSLocalizedString(@"Check", @"Check answer");
|
|
|
|
[self.checkAnswerButton setTitle:NSLocalizedString(@"Check", @"Check answer button") forState:UIControlStateNormal];
|
|
|
|
self.showAnswerButton.titleLabel.text = NSLocalizedString(@"Answer", @"Show correct answer button");
|
|
|
|
[self.showAnswerButton setTitle:NSLocalizedString(@"Answer", @"Show correct answer button") forState:UIControlStateNormal];
|
|
|
|
|
|
if ( [self.testSet count] > 0) {
|
|
[self nextVerb];
|
|
}
|
|
|
|
|
|
}
|
|
|
|
- (void)didReceiveMemoryWarning
|
|
{
|
|
[super didReceiveMemoryWarning];
|
|
// Dispose of any resources that can be recreated.
|
|
}
|
|
|
|
@end
|