//
// DownMusicOne.h
// SunshineMusic
//
// Created by dllo on 16/1/18.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "BaseModel.h"
#import <UIKit/UIKit.h>
@interface DownMusicOne : BaseModel
- (void)downMusic:(NSString *)str;
@property (nonatomic, assign) CGFloat yixiazai;
@property (nonatomic, assign) CGFloat bencixiazai;
@property (nonatomic, assign) CGFloat zong;
@property (nonatomic, retain) NSURLSessionDownloadTask *downT;
@property (nonatomic,copy) void(^loadingBlock)(float); //下载回调
@property (nonatomic,copy) void(^complet)();
@end
//
// DownMusicOne.m
// SunshineMusic
//
// Created by dllo on 16/1/18.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "DownMusicOne.h"
@interface DownMusicOne () <NSURLSessionDownloadDelegate>
@end
@implementation DownMusicOne
- (void)downMusic:(NSString *)str
{
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue currentQueue]];
self.downT = [session downloadTaskWithRequest:request];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
NSString *cach = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [cach stringByAppendingPathComponent:@"music"];
NSString *file = [filePath stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
NSFileManager *fileM = [NSFileManager defaultManager];
[fileM moveItemAtPath:location.path toPath:file error:nil];
self.complet();
}
/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"本次下载大小:%fKB, 已经下载大小:%fKB, 总大小:%fKB", bytesWritten / 1024.0, totalBytesWritten / 1024.0, totalBytesExpectedToWrite / 1024.0);
self.zong = totalBytesExpectedToWrite / 1024.0;
self.yixiazai = totalBytesWritten / 1024.0;
self.bencixiazai = bytesWritten;
self.loadingBlock(self.yixiazai / self.zong);
}
@end
//
// DownMusicViewController.h
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DownMusicViewController : UIViewController
+ (instancetype)shareVC;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UICollectionView *cv;
@end
//
// DownMusicViewController.m
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "DownMusicViewController.h"
#import "DowningMusic.h"
#import <Masonry.h>
#import "BaseButton.h"
#import "LocalMusicview.h"
#import "MusicPlayerViewController.h"
@interface DownMusicViewController () <UICollectionViewDataSource, UICollectionViewDelegate, LocalMusicDelegate>
@property (nonatomic, retain) NSMutableArray *arrButton;
@end
@implementation DownMusicViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createSubViews];
[self createButtons];
}
+ (instancetype)shareVC
{
static DownMusicViewController *downMusicVC = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
downMusicVC = [[DownMusicViewController alloc] init];
downMusicVC.arr = [NSMutableArray array];
});
return downMusicVC;
}
- (NSMutableArray *)findMusicList
{
NSMutableArray *arr = [NSMutableArray array];
NSFileManager *fileM = [NSFileManager defaultManager];
NSString *cach = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [cach stringByAppendingPathComponent:@"music"];
[fileM createDirectoryAtPath:filePath withIntermediateDirectories:YES
attributes:nil error:nil];
NSLog(@"_____%@", cach);
NSString *str = [cach stringByAppendingPathComponent:@"music"];
for (NSString *str1 in [fileM enumeratorAtPath:str]) {
if (![str1 isEqualToString:@".DS_Store"]) {
[arr addObject:str1];
}
}
return arr;
}
- (void)createSubViews
{
self.view.backgroundColor = [UIColor whiteColor];
UICollectionViewFlowLayout *flo = [[UICollectionViewFlowLayout alloc] init];
flo.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flo.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.cv = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flo];
self.cv.backgroundColor = [UIColor redColor];
self.cv.delegate = self;
self.cv.dataSource = self;
[self.cv registerClass:[DowningMusic class] forCellWithReuseIdentifier:@"down"];
[self.cv registerClass:[LocalMusicview class] forCellWithReuseIdentifier:@"local"];
[self.view addSubview:self.cv];
[self.cv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.mas_equalTo(self.view);
make.top.mas_equalTo(self.view.mas_top).offset(40);
}];
[_cv release];
}
- (void)createButtons
{
self.arrButton = [NSMutableArray array];
for (NSInteger i = 0; i < 2; i++) {
BaseButton *button = [BaseButton new];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:button];
[self.arrButton addObject:button];
}
[[self.arrButton objectAtIndex:0] setTitle:@"已经下载" forState:UIControlStateNormal];
[[self.arrButton objectAtIndex:1] setTitle:@"正在下载" forState:UIControlStateNormal];
[[self.arrButton objectAtIndex:0] mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view).offset(-100);
make.top.equalTo(self.view).offset(5);
make.height.mas_equalTo(30);
make.width.equalTo(@80);
}];
[[self.arrButton objectAtIndex:1] mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo([self.arrButton objectAtIndex:0]).offset(200);
make.top.height.width.equalTo([self.arrButton objectAtIndex:1]);
}];
}
- (void)turnToPlayerVC:(NSString *)str
{
MusicPlayerViewController *m = [[MusicPlayerViewController alloc] init];
m.str = str;
[self presentViewController:m animated:YES completion:^{
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 2;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {
DowningMusic *down = [collectionView dequeueReusableCellWithReuseIdentifier:@"down" forIndexPath:indexPath];
down.arr = self.arr;
[down.tableV reloadData];
return down;
} else {
LocalMusicview *local = [collectionView dequeueReusableCellWithReuseIdentifier:@"local" forIndexPath:indexPath];
local.delegate = self;
local.arr = [self findMusicList];
return local;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// DownMusicingForOneView.h
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "BaseTableViewCell.h"
#import "BaseLabel.h"
#import "BaseImageView.h"
#import "BaseButton.h"
@interface DownMusicingForOneView : BaseTableViewCell
@property (nonatomic, retain) BaseLabel *labelName;
@property (nonatomic, retain) BaseImageView *imageStyle;
@property (nonatomic, retain) BaseLabel *labelBig;
@property (nonatomic, retain) UIProgressView *progress;
@property (nonatomic, retain) BaseButton *button;
@end
//
// DownMusicingForOneView.m
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "DownMusicingForOneView.h"
#import <Masonry.h>
@implementation DownMusicingForOneView
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createSubViews];
}
return self;
}
- (CGSize)widthWithStr:(NSString *)str
{
CGRect frame = [str boundingRectWithSize:CGSizeMake(400, 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];
CGFloat width = frame.size.width;
return CGSizeMake(width, 0);
}
- (void)createSubViews
{
self.progress = [UIProgressView new];
self.progress.backgroundColor = [UIColor greenColor];
[self addSubview:self.progress];
[self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.left.mas_equalTo(self).offset(30);
make.width.mas_equalTo(300);
make.height.mas_equalTo(5);
}];
self.labelName = [BaseLabel new];
self.labelName.text = @"aaaaaaaaaaaaEEEEEEEEE";
[self addSubview:self.labelName];
[self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.progress.mas_top).offset(-5);
make.left.equalTo(self.progress);
CGSize size = [self widthWithStr:self.labelName.text];
make.height.mas_equalTo(20);
make.width.mas_equalTo(size.width);
}];
self.imageStyle = [BaseImageView new];
self.imageStyle.backgroundColor = [UIColor greenColor];
[self addSubview:self.imageStyle];
[self.imageStyle mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.labelName);
make.left.equalTo(self.labelName.mas_right);
make.height.equalTo(@10);
make.width.equalTo(@15);
}];
self.labelBig = [BaseLabel new];
self.labelBig.text = @"90/ 90 / 90";
self.labelBig.font = [UIFont systemFontOfSize:17.0];
[self addSubview:self.labelBig];
[self.labelBig mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.imageStyle.mas_right).offset(5);
make.top.equalTo(self.imageStyle);
make.height.equalTo(self.labelName);
make.width.equalTo(@80);
}];
}
@end
//
// DowningMusic.h
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "BaseCollectionViewCell.h"
@interface DowningMusic : BaseCollectionViewCell
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableV;
@end
//
// DowningMusic.m
// SunshineMusic
//
// Created by dllo on 16/1/13.
// Copyright © 2016年 G.Group. All rights reserved.
//
#import "DowningMusic.h"
#import "BaseButton.h"
#import <Masonry.h>
#import "DownMusicingForOneView.h"
#import "DownMusicOne.h"
@interface DowningMusic () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *arrButton;
@property (nonatomic, retain) DownMusicingForOneView *downing;
//@property (nonatomic, retain ) DownMusicOne *one;
@property (nonatomic, retain) NSMutableArray *down;
@end
@implementation DowningMusic
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createSubViews];
}
return self;
}
- (void)createSubViews
{
[self createTableV];
}
- (void)setArr:(NSMutableArray *)arr
{
if (_arr != arr) {
[_arr release];
_arr = [arr retain];
}
[self createDownNum];
}
- (void)createDownNum
{
NSLog(@"MMMMMMMMMMMMMMM");
NSLog(@"%ld", [self.arr count]);
if ([self.arr count] != 0) {
self.down = [NSMutableArray array];
for (NSInteger i = 0; i < [self.arr count]; i++) {
DownMusicOne *one = [[DownMusicOne alloc] init];
[one downMusic:[self.arr objectAtIndex:i]];
[one.downT resume];
[self.down addObject:one];
NSLog(@"%ld", [self.down count]);
[self.tableV reloadData];
}
}
}
- (void)createTableV
{
self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 52, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
self.tableV.rowHeight = 60;
[self.tableV registerClass:[DownMusicingForOneView class] forCellReuseIdentifier:@"downing"];
self.tableV.delegate = self;
self.tableV.dataSource = self;
[self addSubview:self.tableV];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.downing = [tableView dequeueReusableCellWithIdentifier:@"downing"];
self.downing.layer.cornerRadius = 10;
self.downing.layer.borderColor = [UIColor purpleColor].CGColor;
self.downing.layer.borderWidth = 1;
// DownMusicOne *one = [[DownMusicOne alloc] init];
// [one downMusic:[self.arr objectAtIndex:indexPath.row]];
// __unsafe_unretained DowningMusic *music = self;
// [one senderZongAndYiXiaZai:^(CGFloat zong, CGFloat yixiazi) {
// music.downing.progress.progress = one.yixiazai / one.zong;
// }];
// [one JIanshaoone:^{
//
// [self.arr lastObject];
// [self.tableV reloadData];
// }];
// [one.downT resume];
__unsafe_unretained DowningMusic *music = self;
if ([self.down count] != 0) {
DownMusicOne *one = [self.down objectAtIndex:indexPath.row];
one.loadingBlock = ^(float progress) {
music.downing.progress.progress = progress;
};
one.complet = ^(){
[self.arr removeLastObject];
[self.tableV reloadData];
};
}
return _downing;
}
@end