-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSViewController.m
132 lines (114 loc) · 4.3 KB
/
PSViewController.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
//
// PSViewController.m
// PianoScribe
//
// Created by Wenley Tong on 11/4/13.
// Copyright (c) 2013 WenleyTong. All rights reserved.
//
#import "PSViewController.h"
#import "PSInstrumentViewController.h"
#import "PSLearningViewController.h"
@interface PSViewController ()
@end
@implementation PSViewController
@synthesize picker, instrumentName;
@synthesize instrumentDirectories, instrumentsDirectory, currentInstrumentPicked;
- (void)viewDidLoad
{
[super viewDidLoad];
// Load instrument directories into instrumentDirectories
self.instrumentsDirectory = [[NSBundle mainBundle] pathForResource:@"Instruments" ofType:nil];
[self loadInstruments];
// Select the last instrument used
NSString * lastInstrumentPath = [[NSBundle mainBundle] pathForResource:@"LastInstrumentUsed" ofType:@"txt"];
NSString * lastInstrument = [NSString stringWithContentsOfFile:lastInstrumentPath
encoding:NSASCIIStringEncoding
error:nil];
if (lastInstrument) {
NSInteger lastRow = [instrumentDirectories indexOfObject:lastInstrument];
[self.picker selectRow:lastRow inComponent:0 animated:NO];
}
else {
[self.picker selectRow:0 inComponent:0 animated:NO];
}
}
- (IBAction) initiateLearning:(UIButton *)sender
{
if ([self.instrumentDirectories containsObject:self.instrumentName.text] ||
[self.instrumentName.text isEqualToString:@""]) {
NSLog(@"Invalid instrument name"); // Display error message
}
else {
PSLearningViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Learning"];
vc.parent = self;
vc.directory = self.instrumentsDirectory;
vc.name = self.instrumentName.text;
[self presentViewController:vc animated:YES completion:nil];
}
}
- (IBAction) useExistingInstrument:(UIButton *)sender
{
[self useInstrument:self.instrumentName.text];
}
- (void) useInstrument:(NSString *)pathToInstrumentDirectory
{
PSInstrumentViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Instrument"];
vc.parent = self;
[self presentViewController:vc animated:YES completion:nil];
}
- (void) loadInstruments
{
self.instrumentDirectories = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:instrumentsDirectory error:nil];
}
/* ---------------------------------------------------------------- */
/* UIPickerViewDatasource Methods */
/* ---------------------------------------------------------------- */
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1; // Only column of instrument names
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.instrumentDirectories.count;
}
/* ---------------------------------------------------------------- */
/* UIPickerViewDelegate Methods */
/* ---------------------------------------------------------------- */
- (CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30.0;
}
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 300.0;
}
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.instrumentDirectories objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSAssert(component == 0, @"More than one component?");
self.currentInstrumentPicked = [instrumentDirectories objectAtIndex:row];
}
/* ---------------------------------------------------------------- */
/* UITextField Methods */
/* ---------------------------------------------------------------- */
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end