Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
VideoLAN
VLC-iOS
Commits
a702fdf0
Commit
a702fdf0
authored
Jun 16, 2013
by
Felix Paul Kühne
Browse files
HTTPFileDownloader: clean-up code and implement a proper delegation system
parent
fdd1e3e8
Changes
2
Hide whitespace changes
Inline
Side-by-side
AspenProject/VLCHTTPFileDownloader.h
View file @
a702fdf0
...
...
@@ -8,13 +8,25 @@
// Refer to the COPYING file of the official project for license.
//
@class
VLCMenuViewController
;
@protocol
VLCHTTPFileDownloader
<
NSObject
>
@required
-
(
void
)
downloadStarted
;
-
(
void
)
downloadEnded
;
@optional
-
(
void
)
downloadFailedWithError
:(
NSError
*
)
error
;
-
(
void
)
progressUpdatedTo
:(
CGFloat
)
percentage
;
@end
@interface
VLCHTTPFileDownloader
:
NSObject
@property
(
nonatomic
,
retain
)
VLCMenuViewController
*
mediaViewController
;
@property
(
readonly
,
nonatomic
)
NSString
*
userReadableDownloadName
;
@property
(
nonatomic
,
readonly
)
BOOL
downloadInProgress
;
@property
(
nonatomic
,
retain
)
id
delegate
;
-
(
void
)
cancelDownload
;
-
(
void
)
downloadFileFromURL
:(
NSURL
*
)
url
;
@end
AspenProject/VLCHTTPFileDownloader.m
View file @
a702fdf0
...
...
@@ -15,31 +15,35 @@
@interface
VLCHTTPFileDownloader
()
{
VLCCircularProgressIndicator
*
_progressIndicator
;
NSString
*
_filePath
;
NSUInteger
_expectedDownloadSize
;
NSUInteger
_receivedDataSize
;
NSString
*
_fileName
;
NSURLConnection
*
_urlConnection
;
}
@end
@implementation
VLCHTTPFileDownloader
-
(
NSString
*
)
userReadableDownloadName
{
return
_fileName
;
}
-
(
void
)
downloadFileFromURL
:(
NSURL
*
)
url
{
_progressIndicator
=
self
.
mediaViewController
.
httpDownloadProgressIndicator
;
_progressIndicator
.
progress
=
0
.;
NSArray
*
searchPaths
=
NSSearchPathForDirectoriesInDomains
(
NSDocumentDirectory
,
NSUserDomainMask
,
YES
);
_filePath
=
[
searchPaths
[
0
]
stringByAppendingPathComponent
:
url
.
lastPathComponent
];
_fileName
=
url
.
lastPathComponent
;
_filePath
=
[
searchPaths
[
0
]
stringByAppendingPathComponent
:
_fileName
];
_expectedDownloadSize
=
_receivedDataSize
=
0
;
NSMutableURLRequest
*
theRequest
=
[
NSMutableURLRequest
requestWithURL
:
url
];
NSURLConnection
*
the
Connection
=
[[
NSURLConnection
alloc
]
initWithRequest
:
theRequest
delegate
:
self
];
if
(
!
the
Connection
)
{
_url
Connection
=
[[
NSURLConnection
alloc
]
initWithRequest
:
theRequest
delegate
:
self
];
if
(
!
_url
Connection
)
{
APLog
(
@"failed to establish connection"
);
_downloadInProgress
=
NO
;
}
else
{
_downloadInProgress
=
YES
;
_progressIndicator
.
hidden
=
NO
;
[
UIApplication
sharedApplication
].
networkActivityIndicatorVisible
=
YES
;
}
}
...
...
@@ -49,6 +53,7 @@
NSUInteger
statusCode
=
[
response
statusCode
];
if
(
statusCode
==
200
)
{
_expectedDownloadSize
=
[
response
expectedContentLength
];
[
self
.
delegate
downloadStarted
];
APLog
(
@"expected download size: %i"
,
_expectedDownloadSize
);
}
}
...
...
@@ -63,6 +68,8 @@
if
(
!
fileHandle
)
{
APLog
(
@"file creation failed, no data was saved"
);
if
([
self
.
delegate
respondsToSelector
:
@selector
(
downloadFailedWithError
:)])
[
self
.
delegate
downloadFailedWithError
:
nil
];
return
;
}
}
...
...
@@ -72,7 +79,8 @@
[
fileHandle
writeData
:
data
];
_receivedDataSize
=
_receivedDataSize
+
data
.
length
;
_progressIndicator
.
progress
=
(
float
)
_receivedDataSize
/
(
float
)
_expectedDownloadSize
;
if
([
self
.
delegate
respondsToSelector
:
@selector
(
progressUpdatedTo
:)])
[
self
.
delegate
progressUpdatedTo
:
(
float
)
_receivedDataSize
/
(
float
)
_expectedDownloadSize
];
}
@catch
(
NSException
*
e
)
{
APLog
(
@"exception when writing to file %@"
,
_filePath
);
...
...
@@ -84,22 +92,28 @@
-
(
void
)
connectionDidFinishLoading
:(
NSURLConnection
*
)
connection
{
APLog
(
@"http file download complete"
);
_downloadInProgress
=
NO
;
_progressIndicator
.
hidden
=
YES
;
[
UIApplication
sharedApplication
].
networkActivityIndicatorVisible
=
NO
;
VLCAppDelegate
*
appDelegate
=
[
UIApplication
sharedApplication
].
delegate
;
[
appDelegate
updateMediaList
];
[
_mediaViewController
dismiss
:
nil
];
[
self
.
delegate
downloadEnded
];
}
-
(
void
)
connection
:(
NSURLConnection
*
)
connection
didFailWithError
:(
NSError
*
)
error
{
APLog
(
@"http file download failed (%i)"
,
error
.
code
);
_downloadInProgress
=
NO
;
_progressIndicator
.
hidden
=
YES
;
[
UIApplication
sharedApplication
].
networkActivityIndicatorVisible
=
NO
;
[
_mediaViewController
dismiss
:
nil
];
if
([
self
.
delegate
respondsToSelector
:
@selector
(
downloadFailedWithError
:)])
[
self
.
delegate
downloadFailedWithError
:
error
];
[
self
.
delegate
downloadEnded
];
}
-
(
void
)
cancelDownload
{
[
_urlConnection
cancel
];
}
@end
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment