-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLBookmarkCellViewModel.m
185 lines (146 loc) · 5.24 KB
/
PLBookmarkCellViewModel.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
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "PLBookmarkCellViewModel.h"
#import "PLDataAccess.h"
#import "PLUtils.h"
#import "NSObject+PLExtensions.h"
#import "PLPlayer.h"
#import "PLBookmarkSong.h"
#import "PLKVOObserver.h"
#import "PLNotificationObserver.h"
#import "PLColors.h"
@interface PLBookmarkCellViewModel() {
PLBookmarkSong *_bookmarkSong;
RACDisposable *_imageArtworkSubscription;
RACDisposable *_progressTimerSubscription;
PLKVOObserver *_playerObserver;
PLNotificationObserver *_notificationObserver;
}
@property (strong, nonatomic, readwrite) UIImage *imageArtwork;
@property (strong, nonatomic, readwrite) NSString *titleText;
@property (strong, nonatomic, readwrite) NSString *artistText;
@property (assign, nonatomic, readwrite) CGFloat alpha;
@end
@implementation PLBookmarkCellViewModel
- (instancetype)initWithBookmarkSong:(PLBookmarkSong *)bookmarkSong
{
self = [super init];
if (self) {
_bookmarkSong = bookmarkSong;
[self updateTrackMetadata];
[self setupUpdatingProgress];
_notificationObserver = [PLNotificationObserver observer];
_playerObserver = [PLKVOObserver observerWithTarget:[PLPlayer sharedPlayer]];
@weakify(self);
[_playerObserver addKeyPath:@instanceKey(PLPlayer, currentSong) handler:^(id _) { @strongify(self);
[self pl_notifyKvoForKey:@"backgroundColor"];
[self setupUpdatingProgress];
}];
[_playerObserver addKeyPath:@instanceKey(PLPlayer, isPlaying) handler:^(id _) { @strongify(self);
[self setupUpdatingProgress];
}];
[_notificationObserver addNotification:UIApplicationWillEnterForegroundNotification handler:^(id _) { @strongify(self);
[self setupUpdatingProgress];
}];
[_notificationObserver addNotification:UIApplicationDidEnterBackgroundNotification handler:^(id _) { @strongify(self);
[self stopUpdatingProgress];
}];
}
return self;
}
- (void)updateTrackMetadata
{
self.artistText = _bookmarkSong.track.artist;
self.titleText = _bookmarkSong.track.title;
self.alpha = _bookmarkSong.track.assetURL ? 1.f : 0.5f;
[self pl_notifyKvoForKey:@"durationText"];
[_imageArtworkSubscription dispose];
@weakify(self);
_imageArtworkSubscription = [_bookmarkSong.track.smallArtwork subscribeNext:^(UIImage *image) { @strongify(self);
self.imageArtwork = image;
}];
}
- (void)setupUpdatingProgress
{
[self pl_notifyKvoForKeys:@[ @"playbackProgress", @"durationText" ]];
PLPlayer *player = [PLPlayer sharedPlayer];
BOOL isPlayingThisSong = NO;
if (player.isPlaying && [player.currentSong isKindOfClass:[PLBookmarkSong class]]) {
PLBookmarkSong *currentBookmarkSong = (PLBookmarkSong *)player.currentSong;
isPlayingThisSong = currentBookmarkSong.bookmark == _bookmarkSong.bookmark;
}
if (!isPlayingThisSong) {
[self stopUpdatingProgress];
return;
}
// already checking
if (_progressTimerSubscription)
return;
_progressTimerSubscription = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _) {
[self pl_notifyKvoForKeys:@[ @"playbackProgress", @"durationText" ]];
}];
}
- (void)stopUpdatingProgress
{
if (_progressTimerSubscription) {
[_progressTimerSubscription dispose];
_progressTimerSubscription = nil;
}
}
- (BOOL)isSongCurrent
{
PLPlayer *player = [PLPlayer sharedPlayer];
if (![player.currentSong isKindOfClass:[PLBookmarkSong class]])
return NO;
PLBookmarkSong *currentBookmarkSong = (PLBookmarkSong *)player.currentSong;
return currentBookmarkSong.bookmark == _bookmarkSong.bookmark;
}
- (NSTimeInterval)position
{
PLPlayer *player = [PLPlayer sharedPlayer];
return [self isSongCurrent] ? player.currentPosition : [_bookmarkSong.position doubleValue];
}
- (UIColor *)backgroundColor
{
return [self isSongCurrent] ? [PLColors shadeOfGrey:242] : [UIColor whiteColor];
}
- (double)playbackProgress
{
NSTimeInterval position = [self position];
NSTimeInterval duration = _bookmarkSong.track.duration;
if (duration > 0)
return MAX(0.0, MIN(1.0, position / duration));
return 0.0;
}
- (double)bookmarkPosition
{
NSTimeInterval position = [_bookmarkSong.bookmark.position doubleValue];
NSTimeInterval duration = _bookmarkSong.track.duration;
if (duration > 0)
return MAX(0.0, MIN(1.0, position / duration));
return 0.0;
}
- (NSString *)durationText
{
NSTimeInterval position = [self position];
NSTimeInterval duration = _bookmarkSong.track.duration;
NSString *positionText = [PLUtils formatDuration:position];
NSString *durationText = [PLUtils formatDuration:duration];
if (duration == 0)
return @"";
else if (position == 0)
return durationText;
else
return [NSString stringWithFormat:@"%@ / %@", positionText, durationText];
}
- (void)selectCell
{
PLPlayer *player = [PLPlayer sharedPlayer];
[player setCurrentSong:_bookmarkSong];
[player play];
}
- (void)dealloc
{
_playerObserver = nil;
[self stopUpdatingProgress];
}
@end