mirror of
https://github.com/zhigang1992/transshift.git
synced 2026-01-12 22:52:56 +08:00
first try to realize a background fetch for selected server
This commit is contained in:
@@ -491,6 +491,11 @@
|
||||
2C5810D51B39A5E50069020D = {
|
||||
CreatedOnToolsVersion = 6.3.2;
|
||||
DevelopmentTeam = 72C28QMYS6;
|
||||
SystemCapabilities = {
|
||||
com.apple.BackgroundModes = {
|
||||
enabled = 1;
|
||||
};
|
||||
};
|
||||
};
|
||||
2C5810E81B39A5E50069020D = {
|
||||
CreatedOnToolsVersion = 6.3.2;
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
@interface AppDelegate() <RPCConnectorDelegate>
|
||||
|
||||
@property(atomic) TRInfos* bgTRInfos;
|
||||
|
||||
@end
|
||||
|
||||
@implementation AppDelegate
|
||||
@@ -30,6 +32,12 @@
|
||||
RPCServerConfig *_selectedConfig;
|
||||
|
||||
NSString *_magnetURLString;
|
||||
|
||||
// flag showing - that we use background fetching
|
||||
BOOL _isBackgroundFetching;
|
||||
|
||||
// background fetch complition handler
|
||||
void (^_bgComplitionHandler)(UIBackgroundFetchResult);
|
||||
}
|
||||
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
||||
@@ -62,6 +70,9 @@
|
||||
}
|
||||
|
||||
self.window.rootViewController = rootController;
|
||||
// set background fetch interval
|
||||
_isBackgroundFetching = NO;
|
||||
[application setMinimumBackgroundFetchInterval:20]; // 20 seconds fetching
|
||||
|
||||
// show main window
|
||||
self.window.backgroundColor = [UIColor whiteColor];
|
||||
@@ -155,12 +166,20 @@
|
||||
|
||||
- (void)connector:(RPCConnector *)cn complitedRequestName:(NSString *)requestName withError:(NSString *)errorMessage
|
||||
{
|
||||
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Can't add torrent"
|
||||
message:[NSString stringWithFormat:@"%@", errorMessage]
|
||||
delegate:nil
|
||||
cancelButtonTitle:@"OK"
|
||||
otherButtonTitles:nil, nil];
|
||||
[alert show];
|
||||
if( !_isBackgroundFetching )
|
||||
{
|
||||
|
||||
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Can't add torrent"
|
||||
message:[NSString stringWithFormat:@"%@", errorMessage]
|
||||
delegate:nil
|
||||
cancelButtonTitle:@"OK"
|
||||
otherButtonTitles:nil, nil];
|
||||
[alert show];
|
||||
}
|
||||
else
|
||||
{
|
||||
_bgComplitionHandler(UIBackgroundFetchResultFailed);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addTorrentToSelectedServer
|
||||
@@ -182,4 +201,109 @@
|
||||
[[RPCServerConfigDB sharedDB] saveDB];
|
||||
}
|
||||
|
||||
- (void)gotAllTorrents:(TRInfos *)trInfos
|
||||
{
|
||||
if( _isBackgroundFetching )
|
||||
{
|
||||
// fetch is complite
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
NSArray *downIds = [defaults arrayForKey:USERDEFAULTS_BGFETCH_KEY_DOWNTORRENTIDS];
|
||||
|
||||
|
||||
// update current downloading torrents ids
|
||||
// in NSUserDefaults
|
||||
NSArray *curDownIds = trInfos.downloadingTorrents;
|
||||
if( curDownIds && curDownIds.count > 0 )
|
||||
{
|
||||
NSMutableArray *downIds = [NSMutableArray array];
|
||||
for (TRInfo* t in downIds)
|
||||
[downIds addObject:@(t.trId)];
|
||||
|
||||
[defaults setObject:downIds forKey:USERDEFAULTS_BGFETCH_KEY_DOWNTORRENTIDS];
|
||||
[defaults synchronize];
|
||||
}
|
||||
else // there is no ids, remove key
|
||||
{
|
||||
[defaults removeObjectForKey:USERDEFAULTS_BGFETCH_KEY_DOWNTORRENTIDS];
|
||||
[defaults synchronize];
|
||||
}
|
||||
|
||||
|
||||
if( !downIds )
|
||||
{
|
||||
// there is downIds - try to create new and return
|
||||
_bgComplitionHandler(UIBackgroundFetchResultNoData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// info string
|
||||
NSMutableString *infoStr = [NSMutableString string];
|
||||
|
||||
// searching finished torrents
|
||||
NSArray* seedTrs = trInfos.seedingTorrents;
|
||||
for ( NSNumber* trId in downIds )
|
||||
{
|
||||
int torrentId = [trId intValue];
|
||||
|
||||
for( TRInfo* info in seedTrs )
|
||||
{
|
||||
if( torrentId == info.trId )
|
||||
{
|
||||
// we have found torrent that is finished
|
||||
[infoStr appendString: [NSString stringWithFormat:@"Torrent: %@, has finished downloading\n", info.name] ];
|
||||
}
|
||||
}
|
||||
} // end for searching
|
||||
|
||||
if( infoStr.length > 0 )
|
||||
{
|
||||
// we should show
|
||||
// show local notification
|
||||
UILocalNotification *notification = [[UILocalNotification alloc] init];
|
||||
notification.fireDate = nil;
|
||||
notification.alertTitle = @"Torrent(s) downloaded";
|
||||
notification.alertBody = infoStr;
|
||||
notification.soundName = UILocalNotificationDefaultSoundName;
|
||||
|
||||
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
|
||||
|
||||
_bgComplitionHandler(UIBackgroundFetchResultNewData);
|
||||
}
|
||||
else
|
||||
{
|
||||
_bgComplitionHandler(UIBackgroundFetchResultNoData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
|
||||
{
|
||||
// peform fetch in background
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
NSDictionary *plist = [defaults dictionaryForKey: USERDEFAULTS_BGFETCH_KEY_RPCCONFG];
|
||||
|
||||
if( plist )
|
||||
{
|
||||
NSLog(@"BackgroundFetch: - GETTING DATA .... ");
|
||||
|
||||
RPCServerConfig *config = [[RPCServerConfig alloc] initFromPList:plist];
|
||||
// try to get update on this torrents
|
||||
|
||||
self.bgTRInfos = nil;
|
||||
// set request timeout max to 5 seconds
|
||||
config.requestTimeout = 5;
|
||||
RPCConnector *connector = [[RPCConnector alloc] initWithConfig:config andDelegate:self];
|
||||
_isBackgroundFetching = YES;
|
||||
_bgComplitionHandler = completionHandler;
|
||||
// try to get all torrents
|
||||
[connector getAllTorrents];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"BackgroundFetch: - NO DATA FETCHED");
|
||||
completionHandler( UIBackgroundFetchResultNoData );
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -56,9 +56,13 @@
|
||||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>027</string>
|
||||
<string>031</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>fetch</string>
|
||||
</array>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
@interface RPCServerConfig : NSObject <NSCoding>
|
||||
|
||||
- (instancetype)initFromPList:(NSDictionary*)plist;
|
||||
|
||||
@property(nonatomic) NSString *name; // common server name
|
||||
@property(nonatomic) NSString *host; // ip address of domain name of server
|
||||
@property(nonatomic) int port; // RPC port to connect to (default 8090)
|
||||
@@ -26,4 +28,6 @@
|
||||
@property(nonatomic) int requestTimeout; // request timeout to server in seconds
|
||||
@property(nonatomic,readonly) NSString* urlString; // return short descriptions of class
|
||||
|
||||
@property(nonatomic,readonly) NSDictionary* plist; // return property list object
|
||||
|
||||
@end
|
||||
|
||||
@@ -87,4 +87,40 @@ static NSString *CODER_REQUEST_TIMEOUT = @"reqtimeout";
|
||||
[coder encodeBool:self.useSSL forKey:CODER_USE_SSL];
|
||||
}
|
||||
|
||||
- (NSDictionary *)plist
|
||||
{
|
||||
NSDictionary *pList = @{
|
||||
CODER_NAME : _name,
|
||||
CODER_RPC_PATH : _rpcPath,
|
||||
CODER_HOST : _host,
|
||||
CODER_PORT : @(_port),
|
||||
CODER_USE_SSL : @(_useSSL),
|
||||
CODER_USER_NAME : _userName,
|
||||
CODER_USER_PASSWORD : _userPassword,
|
||||
CODER_REFRESH_TIMEOUT : @(_refreshTimeout),
|
||||
CODER_REQUEST_TIMEOUT : @(_refreshTimeout)
|
||||
};
|
||||
|
||||
return pList;
|
||||
}
|
||||
|
||||
- (instancetype)initFromPList:(NSDictionary *)plist
|
||||
{
|
||||
self = [super init];
|
||||
if( self )
|
||||
{
|
||||
_name = plist[CODER_NAME];
|
||||
_rpcPath = plist[CODER_RPC_PATH];
|
||||
_host = plist[CODER_HOST];
|
||||
_port = [(NSNumber*)plist[CODER_PORT] intValue];
|
||||
_useSSL = [(NSNumber*)plist[CODER_USE_SSL] boolValue];
|
||||
_userName = plist[CODER_USER_NAME];
|
||||
_userPassword = plist[CODER_USER_PASSWORD];
|
||||
_refreshTimeout = [(NSNumber*)plist[CODER_REFRESH_TIMEOUT] intValue];
|
||||
_requestTimeout = [(NSNumber*)plist[CODER_REQUEST_TIMEOUT] intValue];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
#define CONTROLLER_ID_SERVERLIST @"ServerListController"
|
||||
#define SERVERLIST_CONTROLLER_TITLE @"Servers"
|
||||
|
||||
#define USERDEFAULTS_BGFETCH_KEY_RPCCONFG @"bgCurrentRPCConfig"
|
||||
#define USERDEFAULTS_BGFETCH_KEY_DOWNTORRENTIDS @"bgDownloadingTorrentIds"
|
||||
|
||||
@interface ServerListController : CommonTableController
|
||||
|
||||
@end
|
||||
|
||||
@@ -69,6 +69,11 @@
|
||||
|
||||
[nc popToRootViewControllerAnimated:YES];
|
||||
}
|
||||
|
||||
// remove keys for background fetching
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
[defaults removeObjectForKey:USERDEFAULTS_BGFETCH_KEY_RPCCONFG];
|
||||
[defaults synchronize];
|
||||
}
|
||||
|
||||
- (RPCServerConfigController *)rpcConfigController
|
||||
@@ -163,6 +168,12 @@
|
||||
|
||||
RPCServerConfig *selectedConfig = [RPCServerConfigDB sharedDB].db[indexPath.row];
|
||||
|
||||
// register config for background fetchig
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
[defaults setObject:selectedConfig.plist forKey:USERDEFAULTS_BGFETCH_KEY_RPCCONFG];
|
||||
[defaults synchronize];
|
||||
// ---
|
||||
|
||||
_statusListController.config = selectedConfig ;
|
||||
_statusListController.title = selectedConfig.name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user