-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArtistsView.m
executable file
·204 lines (173 loc) · 7.05 KB
/
ArtistsView.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
* iPhone Project : iMPDclient *
* Copyright (C) 2008 Boris Nagels <[email protected]> *
*******************************************************************************
* $LastChangedDate:: 2008-01-29 22:02:23 +0100 (Tue, 29 Jan 2008) $ *
* $LastChangedBy:: boris $ *
* $LastChangedRevision:: 140 $ *
* $Id:: application.m 140 2008-01-29 21:02:23Z boris $ *
*******************************************************************************
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************/
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import "ArtistsView.h"
#import "impdclientApp.h"
//////////////////////////////////////////////////////////////////////////
// ArtistsView: implementation.
//////////////////////////////////////////////////////////////////////////
@implementation ArtistsView
- (void)dealloc
{
// Release all objects.
[_table release];
[_artists release];
[_tableHeaders release];
[_sectionList release];
[_navBar release];
// Call the base class.
[super dealloc];
}
- (void)initialize:(MPDClientApplication *)app mpd:(MpdObj *)mpdServer
{
_app = app;
_mpdServer = mpdServer;
}
- (id)initWithFrame:(struct CGRect)frame
{
self = [super initWithFrame:frame];
_app = NULL;
_mpdServer = NULL;
// Create the storage arrays.
_artists = [[NSMutableArray alloc] init];
_tableHeaders = [[NSMutableArray alloc] init];
CGRect aRect = CGRectMake(0, NAVBARHEIGHT, 320, MAXHEIGHT);
// Create the section list.
_sectionList = [[UISectionList alloc] initWithFrame:aRect showSectionIndex:YES];
[_sectionList setDataSource:self];
[_sectionList reloadData];
[self addSubview:_sectionList];
// Get the real table.
_table = [_sectionList table];
UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"iMPDclient" identifier: @"column1" width: 320];
[_table addTableColumn: col];
[_table setDelegate: self];
[_table setSeparatorStyle:1];
[_table setRowHeight:42.0f];
// Create the navigation bar.
_navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, 320, NAVBARHEIGHT)];
[_navBar showLeftButton:@"Playlist" withStyle:2 rightButton:@"Search" withStyle:3]; // 2 = arrow left, 3 = blue.
[_navBar setBarStyle: 1]; // Dark style.
[_navBar setDelegate:self];
[_navBar enableAnimation];
_title = [[UINavigationItem alloc] initWithTitle:@"--:--"];
[_navBar pushNavigationItem: _title];
[self addSubview: _navBar];
return self;
}
// --- OTHER METHODS -----------------------------------------------
- (void)showArtists
{
if (!_mpdServer)
return;
// Clear the arrays.
[_artists removeAllObjects];
[_tableHeaders removeAllObjects];
// Get the list of artists.
mpd_database_search_field_start(_mpdServer, MPD_TAG_ITEM_ARTIST);
MpdData *data = mpd_database_search_commit(_mpdServer);
if (data) {
NSString* prevSection = @"";
int count = 0;
do {
if (data->type == MPD_DATA_TYPE_TAG) {
// Convert the name.
NSString* artistName = [NSString stringWithCString: data->tag];
// Determine the first letters for the section list.
NSString* firstLetter = [artistName substringWithRange:NSMakeRange(0,1)];
if (![firstLetter isEqual:prevSection]) {
prevSection = [firstLetter copy];
// NSLog(@"%@ - %d", firstletter, count);
NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
[cellDict setObject:firstLetter forKey:@"title"];
[cellDict setObject:[[NSNumber alloc] initWithInt:count] forKey:@"beginRow"];
[_tableHeaders addObject: cellDict];
[cellDict release];
}
// Create artist object and add it to the array.
UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
[cell setTitle:artistName];
[_artists addObject:cell];
[cell release];
count++;
}
// Go to the next entry.
data = mpd_data_get_next(data);
} while(data);
} else
NSLog(@"No data found");
// Update the table contents.
[_sectionList reloadData];
}
- (void)updateTitle
{
int totalTime = mpd_status_get_total_song_time(_mpdServer);
int elapsedTime = mpd_status_get_elapsed_song_time(_mpdServer);
NSString* str = [NSString stringWithFormat:@"%d:%02d - %d:%02d", elapsedTime / 60, elapsedTime % 60, totalTime / 60, totalTime % 60];
[_title setTitle: str];
}
// --- DELEGATE METHODS -----------------------------------------------
- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
{
NSLog(@"ArtistView: button %d", button);
if (button == 0)
[_app showSearchViewWithTransition:1];
else if (button == 1)
[_app showPlaylistViewWithTransition:2];
}
- (void)tableRowSelected:(NSNotification*)notification
{
// Get selected cell and artist name.
UIImageAndTextTableCell* cell = [[notification object] cellAtRow:[[notification object] selectedRow] column:0];
NSLog(@"Selected artist: %@", [cell title]);
// Show the albums of the selected artist.
[cell setSelected:FALSE withFade:TRUE];
[_app showAlbumsViewWithTransition:1 artist:[cell title]];
}
- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
return [_tableHeaders count];
}
- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)section {
return [[_tableHeaders objectAtIndex:section] objectForKey:@"title"];
}
- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)section {
return [[[_tableHeaders objectAtIndex:section] valueForKey:@"beginRow"] intValue];
}
- (int) numberOfRowsInTable: (UITable *)table
{
return [_artists count];
}
- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{
return [_artists objectAtIndex:row];
}
- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col reusing: (BOOL) reusing
{
return [self table: table cellForRow: row column: col];
}
@end