Skip to content

Commit

Permalink
Merge pull request #26 from albertschulz/master
Browse files Browse the repository at this point in the history
Downloader State, Progress Property, Writable Filename & Multiple Downloads Example
  • Loading branch information
thibaultcha committed Apr 29, 2014
2 parents 7b007af + e056ff7 commit b9e94ab
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 30 deletions.
10 changes: 10 additions & 0 deletions Example/TCBlobDownloadExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
21D06E13181847F400CAADCD /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 21D06E0A181847F400CAADCD /* ViewController.m */; };
21D06E14181847F400CAADCD /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 21D06E0B181847F400CAADCD /* ViewController.xib */; };
C317CBD218CB668500160ED6 /* libTCBlobDownload.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C33448CA18C000850050C1ED /* libTCBlobDownload.a */; };
E17B6757190CEBE100B81CD8 /* MultipleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E17B6755190CEBE100B81CD8 /* MultipleViewController.m */; };
E17B6758190CEBE100B81CD8 /* MultipleViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -63,6 +65,9 @@
21D06E0A181847F400CAADCD /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
21D06E0B181847F400CAADCD /* ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ViewController.xib; sourceTree = "<group>"; };
C33448C318C000850050C1ED /* TCBlobDownload.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = TCBlobDownload.xcodeproj; path = ../TCBlobDownload/TCBlobDownload.xcodeproj; sourceTree = "<group>"; };
E17B6754190CEBE100B81CD8 /* MultipleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MultipleViewController.h; sourceTree = "<group>"; };
E17B6755190CEBE100B81CD8 /* MultipleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MultipleViewController.m; sourceTree = "<group>"; };
E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MultipleViewController.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -116,6 +121,9 @@
21D06E09181847F400CAADCD /* ViewController.h */,
21D06E0A181847F400CAADCD /* ViewController.m */,
21D06E0B181847F400CAADCD /* ViewController.xib */,
E17B6754190CEBE100B81CD8 /* MultipleViewController.h */,
E17B6755190CEBE100B81CD8 /* MultipleViewController.m */,
E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */,
21D06E15181847FF00CAADCD /* Ressources */,
21D06E161818480A00CAADCD /* Supporting Files */,
);
Expand Down Expand Up @@ -236,6 +244,7 @@
21D06E10181847F400CAADCD /* InfoPlist.strings in Resources */,
21D06E0D181847F400CAADCD /* [email protected] in Resources */,
21D06E0E181847F400CAADCD /* Default.png in Resources */,
E17B6758190CEBE100B81CD8 /* MultipleViewController.xib in Resources */,
21D06E14181847F400CAADCD /* ViewController.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -249,6 +258,7 @@
files = (
21D06E13181847F400CAADCD /* ViewController.m in Sources */,
21D06E11181847F400CAADCD /* main.m in Sources */,
E17B6757190CEBE100B81CD8 /* MultipleViewController.m in Sources */,
21D06E0C181847F400CAADCD /* AppDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>58B4167E-AFE9-44B9-8C89-85EDD0E61CDB</string>
<string>F39411CF-86E8-472C-BB23-59E6E7BADCEC</string>
<key>IDESourceControlProjectName</key>
<string>TCBlobDownloadExample</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>6B3F9DA0-0435-4817-BF82-396C64B119CB</key>
<string>https://github.com/thibaultCha/TCBlobDownload.git</string>
<key>AAF2C7DA-26ED-4FFF-9AAA-45212BB1FAE3</key>
<string>https://github.com/albertschulz/TCBlobDownload</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>TCBlobDownloadExample.xcodeproj/project.xcworkspace</string>
<string>Example/TCBlobDownloadExample.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>6B3F9DA0-0435-4817-BF82-396C64B119CB</key>
<string>../..</string>
<key>AAF2C7DA-26ED-4FFF-9AAA-45212BB1FAE3</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/thibaultCha/TCBlobDownload.git</string>
<string>https://github.com/albertschulz/TCBlobDownload</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>6B3F9DA0-0435-4817-BF82-396C64B119CB</string>
<string>AAF2C7DA-26ED-4FFF-9AAA-45212BB1FAE3</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>6B3F9DA0-0435-4817-BF82-396C64B119CB</string>
<string>AAF2C7DA-26ED-4FFF-9AAA-45212BB1FAE3</string>
<key>IDESourceControlWCCName</key>
<string>TC_BlobDownload</string>
<string>TCBlobDownload</string>
</dict>
</array>
</dict>
Expand Down
14 changes: 14 additions & 0 deletions Example/TCBlobDownloadExample/MultipleViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// MultipleViewController.h
// TCBlobDownloadExample
//
// Created by Albert on 27.04.14.
// Copyright (c) 2014 thibaultCha. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <TCBlobDownload/TCBlobDownload.h>

@interface MultipleViewController : UITableViewController <UIAlertViewDelegate, TCBlobDownloaderDelegate>

@end
204 changes: 204 additions & 0 deletions Example/TCBlobDownloadExample/MultipleViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//
// MultipleViewController.m
// TCBlobDownloadExample
//
// Created by Albert on 27.04.14.
// Copyright (c) 2014 thibaultCha. All rights reserved.
//

#import "MultipleViewController.h"

#define kDownloadPath [NSString pathWithComponents:@[NSTemporaryDirectory(), @"multipleExample"]]

@interface MultipleViewController () {
NSMutableArray *downloads;
}

@end

@implementation MultipleViewController

- (void)viewDidLoad
{
[super viewDidLoad];

downloads = [NSMutableArray new];

self.title = @"Multiple Downloads Table";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(dismiss)];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddDownloadAlert)];
}

- (void)dismiss
{
[[TCBlobDownloadManager sharedInstance] cancelAllDownloadsAndRemoveFiles:YES];

[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showAddDownloadAlert
{
UIAlertView *addAlertView = [[UIAlertView alloc] initWithTitle:@"Add Download" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add this URL", @"~ Multiple Test Downloads ~", nil];

addAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

UITextField *textField = [addAlertView textFieldAtIndex:0];
textField.placeholder = @"http://";

[addAlertView show];
}

- (void)addDownload:(NSURL *)url
{
TCBlobDownloader *download = [[TCBlobDownloader alloc] initWithURL:url downloadPath:kDownloadPath delegate:self];

download.fileName = [url.absoluteString lastPathComponent];

[[TCBlobDownloadManager sharedInstance] startDownload:download];

[downloads addObject:download];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Helper Methods

- (NSString *)subtitleForDownload:(TCBlobDownloader *)download
{
NSString *stateString;

switch (download.state) {
case TCBlobDownloadStateReady:
stateString = @"Ready";
break;
case TCBlobDownloadStateDownloading:
stateString = @"Downloading";
break;
case TCBlobDownloadStateDone:
stateString = @"Done";
break;
case TCBlobDownloadStateCancelled:
stateString = @"Cancelled";
break;
case TCBlobDownloadStateFailed:
stateString = @"Failed";
break;
default:
break;
}

return [NSString stringWithFormat:@"%i%%%lis left • State: %@", (int)(download.progress * 100), (long)download.remainingTime, stateString];
}

#pragma mark - Alert view delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {

if (buttonIndex == 0) {
NSString *urlString = [alertView textFieldAtIndex:0].text;

[self addDownload:[NSURL URLWithString:urlString]];
}
else {
[self addDownload:[NSURL URLWithString:@"http://api.soundcloud.com/tracks/136369443/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]];
[self addDownload:[NSURL URLWithString:@"http://api.soundcloud.com/tracks/130355303/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]];
[self addDownload:[NSURL URLWithString:@"https://api.soundcloud.com/tracks/126240832/download?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]];
}

}
}

#pragma mark - Blob downloader delegate

- (void)download:(TCBlobDownloader *)blobDownload didFinishWithSuccess:(BOOL)downloadFinished atPath:(NSString *)pathToFile
{
NSInteger index = [downloads indexOfObject:blobDownload];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload];
}

- (void)download:(TCBlobDownloader *)blobDownload didReceiveData:(uint64_t)receivedLength onTotal:(uint64_t)totalLength progress:(float)progress
{
NSInteger index = [downloads indexOfObject:blobDownload];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload];
}

- (void)download:(TCBlobDownloader *)blobDownload didReceiveFirstResponse:(NSURLResponse *)response
{
NSInteger index = [downloads indexOfObject:blobDownload];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload];
}

- (void)download:(TCBlobDownloader *)blobDownload didStopWithError:(NSError *)error
{
NSInteger index = [downloads indexOfObject:blobDownload];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return downloads.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}

TCBlobDownloader *download = downloads[indexPath.row];

cell.textLabel.text = download.fileName;

cell.detailTextLabel.text = [self subtitleForDownload:download];

return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

TCBlobDownloader *download = downloads[indexPath.row];
[download cancelDownloadAndRemoveFile:YES];

NSInteger index = [downloads indexOfObject:download];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
cell.detailTextLabel.text = [self subtitleForDownload:download];

[cell setEditing:NO animated:YES];
}
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Cancel";
}

@end
27 changes: 27 additions & 0 deletions Example/TCBlobDownloadExample/MultipleViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5053" systemVersion="13C1021" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MultipleViewController">
<connections>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableView opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" bouncesZoom="NO" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
<connections>
<outlet property="dataSource" destination="-1" id="Tng-2m-Rnh"/>
<outlet property="delegate" destination="-1" id="9aC-8N-iBw"/>
</connections>
</tableView>
</objects>
</document>
1 change: 1 addition & 0 deletions Example/TCBlobDownloadExample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

- (IBAction)download:(id)sender;
- (IBAction)cancelAll:(id)sender;
- (IBAction)switchToMultipleDownloads:(id)sender;

@end
9 changes: 9 additions & 0 deletions Example/TCBlobDownloadExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "ViewController.h"
#import "MultipleViewController.h"

@implementation ViewController

Expand Down Expand Up @@ -66,6 +67,14 @@ - (void)cancelAll:(id)sender
[self.sharedDownloadManager cancelAllDownloadsAndRemoveFiles:YES];
}

- (IBAction)switchToMultipleDownloads:(id)sender
{
MultipleViewController *multipleViewController = [MultipleViewController new];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:multipleViewController];

[self presentViewController:navController animated:YES completion:nil];
}

#pragma mark - TCBlobDownloaderDelegate

Expand Down
Loading

0 comments on commit b9e94ab

Please sign in to comment.