Skip to content

Commit

Permalink
Background Fetch #3
Browse files Browse the repository at this point in the history
  • Loading branch information
guruz committed Apr 17, 2020
1 parent 7d19c2a commit 954d1e7
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 11 deletions.
6 changes: 6 additions & 0 deletions quassel-for-ios/quassel-for-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
5B4156CE15DD3DC100C60EE3 /* AppState.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B4156CD15DD3DC100C60EE3 /* AppState.m */; };
5B4A8C3F242FE305006C5169 /* UITextField+QuasselInputField.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B4A8C3E242FE305006C5169 /* UITextField+QuasselInputField.m */; };
5B5E566716B7954A005D442B /* IPhoneStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5B5E566616B7954A005D442B /* IPhoneStoryboard.storyboard */; };
5B60C5F12449E6FD009F5144 /* QuasselBackgroundFetcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B60C5F02449E6FD009F5144 /* QuasselBackgroundFetcher.m */; };
5B6169BC15FF7A9700F85FDE /* woboq.png in Resources */ = {isa = PBXBuildFile; fileRef = 5B6169BB15FF7A9700F85FDE /* woboq.png */; };
5B6169C015FF7C8B00F85FDE /* quasselicon.png in Resources */ = {isa = PBXBuildFile; fileRef = 5B6169BF15FF7C8B00F85FDE /* quasselicon.png */; };
5B6169C315FF827E00F85FDE /* cooledinge.png in Resources */ = {isa = PBXBuildFile; fileRef = 5B6169C215FF827E00F85FDE /* cooledinge.png */; };
Expand Down Expand Up @@ -116,6 +117,8 @@
5B4A8C3D242FE305006C5169 /* UITextField+QuasselInputField.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UITextField+QuasselInputField.h"; sourceTree = "<group>"; };
5B4A8C3E242FE305006C5169 /* UITextField+QuasselInputField.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UITextField+QuasselInputField.m"; sourceTree = "<group>"; };
5B5E566616B7954A005D442B /* IPhoneStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = IPhoneStoryboard.storyboard; sourceTree = "<group>"; };
5B60C5EF2449E6FD009F5144 /* QuasselBackgroundFetcher.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuasselBackgroundFetcher.h; sourceTree = "<group>"; };
5B60C5F02449E6FD009F5144 /* QuasselBackgroundFetcher.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuasselBackgroundFetcher.m; sourceTree = "<group>"; };
5B6169BB15FF7A9700F85FDE /* woboq.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = woboq.png; sourceTree = "<group>"; };
5B6169BF15FF7C8B00F85FDE /* quasselicon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = quasselicon.png; sourceTree = "<group>"; };
5B6169C215FF827E00F85FDE /* cooledinge.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cooledinge.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -399,6 +402,8 @@
5B1B8A7515B4544300DA3B64 /* QuasselUtils.m */,
5BFA9C5E1591CC00006861D2 /* AppDelegate.h */,
5BFA9C5F1591CC00006861D2 /* AppDelegate.m */,
5B60C5EF2449E6FD009F5144 /* QuasselBackgroundFetcher.h */,
5B60C5F02449E6FD009F5144 /* QuasselBackgroundFetcher.m */,
5B4A8C3D242FE305006C5169 /* UITextField+QuasselInputField.h */,
5B4A8C3E242FE305006C5169 /* UITextField+QuasselInputField.m */,
5BFA9C611591CC00006861D2 /* MainStoryboard.storyboard */,
Expand Down Expand Up @@ -591,6 +596,7 @@
files = (
5BCA88FF223A6AD400D80E6E /* GCDAsyncSocket.m in Sources */,
5BFA9C5C1591CC00006861D2 /* main.m in Sources */,
5B60C5F12449E6FD009F5144 /* QuasselBackgroundFetcher.m in Sources */,
5BFA9C601591CC00006861D2 /* AppDelegate.m in Sources */,
5BBF347F1593490A00874C1F /* QuasselCoreConnection.m in Sources */,
5BBF34831593602100874C1F /* QVariant.m in Sources */,
Expand Down
57 changes: 47 additions & 10 deletions quassel-for-ios/quassel-for-ios/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "DDTTYLogger.h"
#import "ConnectingViewController.h"
#import "ErrorViewController.h"
#import "QuasselBackgroundFetcher.h"

#import "AppState.h"

Expand Down Expand Up @@ -45,15 +46,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[TestFlight takeOff:@"c7d61f3a-8a1c-4b0a-ad5c-02bf0987114b"];
// [TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#endif


// This is for getting backgrounded during running
bgTask = UIBackgroundTaskInvalid;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];

// This is for getting invoced newly in background
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1];

//NSSetUncaughtExceptionHandler (&myExceptionHandler);
Expand Down Expand Up @@ -127,10 +131,40 @@ - (void)applicationDidEnterBackground:(UIApplication *)application

}

- (UINavigationController*)navigationController {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
return [splitViewController.viewControllers lastObject];
} else {
return (UINavigationController*) self.window.rootViewController;
}
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// FIXME: Check if really invisible
NSLog(@"performFetchWithCompletionHandler");

// FIXME: Check if we have config
// Check if really in bg
if (application.applicationState != UIApplicationStateBackground) {
NSLog(@"Not in background");
completionHandler(UIBackgroundFetchResultFailed);
return;
}

NSLog(@"vc %@", self.window.rootViewController);
LoginViewController *loginVc = [[self navigationController].viewControllers objectAtIndex:0];
NSLog(@"loginVc %@", loginVc);

// Check if we have config
[loginVc loadViewIfNeeded];
if (loginVc.userNameField.text.length == 0) {
NSLog(@"no username %@",loginVc.userNameField.text);
completionHandler(UIBackgroundFetchResultFailed);
return;
}
NSLog(@"username %@",loginVc.userNameField.text);

QuasselBackgroundFetcher *backgroundFetcher = [[QuasselBackgroundFetcher alloc]initWithCompletionHandler:completionHandler];
[backgroundFetcher connectTo:loginVc.hostNameField.text port:[loginVc.portField.text intValue] userName:loginVc.userNameField.text passWord:loginVc.passWordField.text];

// FIXME: Make sure we don't init any User/Network structures but just fetch all backlogs

Expand All @@ -140,6 +174,8 @@ - (void)application:(UIApplication *)application performFetchWithCompletionHandl
// http://nsscreencast.com/episodes/92-background-fetch

// FIXME: idea: notifications for hilights, for message just unread count?

NSLog(@"Returning from fetch");
}


Expand Down Expand Up @@ -385,11 +421,13 @@ - (void) updateLastSeenOrBadge:(BufferId*)bufferId messageId:(MsgId*)msgId
NSLog(@"updateLastSeenOrBadge %@ %@", bufferId, msgId);
if ( [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
[quasselCoreConnection setLastSeenMsg:msgId forBuffer:bufferId];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1];
} else {
int unreadCount = [quasselCoreConnection computeUnreadCountForBuffer:bufferId];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:unreadCount];
}
[self updateAppBadge];
}

- (void) updateAppBadge {
int unreadCount = [quasselCoreConnection computeUnreadCountForAllBuffers]; // FIXME only relevant buffers
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:unreadCount];
}

- (void) quasselSocketFailedConnect:(NSString*)msg
Expand All @@ -411,8 +449,7 @@ - (void) quasselLastSeenMsgUpdated:(MsgId*)messageId forBuffer:(BufferId*)buffer
{
[self.bufferListViewController reloadRowForBufferId:bufferId];

int unreadCount = [quasselCoreConnection computeUnreadCountForBuffer:bufferId];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:unreadCount];
[self updateAppBadge];
}

- (void) quasselNetworkNameUpdated:(NetworkId*)networkId
Expand Down
20 changes: 20 additions & 0 deletions quassel-for-ios/quassel-for-ios/QuasselBackgroundFetcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// QuasselBackgroundFetcher.h
// quassel-for-ios
//
// Created by Markus Goetz on 17.04.20.
//

#import <Foundation/Foundation.h>
#import "QuasselCoreConnection.h"

NS_ASSUME_NONNULL_BEGIN

@interface QuasselBackgroundFetcher : NSObject <QuasselCoreConnectionDelegate>

- (id) initWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
- (void) connectTo:(NSString*)hostName port:(int)port userName:(NSString*)userName passWord:(NSString*)passWord;

@end

NS_ASSUME_NONNULL_END
110 changes: 110 additions & 0 deletions quassel-for-ios/quassel-for-ios/QuasselBackgroundFetcher.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// QuasselBackgroundFetcher.m
// quassel-for-ios
//
// Created by Markus Goetz on 17.04.20.
//

#import "QuasselBackgroundFetcher.h"

@interface QuasselBackgroundFetcher ()
@property (strong, nonatomic) QuasselCoreConnection *quasselCoreConnection;
@property (strong, nonatomic) void (^fetchCompletionHandler)(UIBackgroundFetchResult r);

@end

@implementation QuasselBackgroundFetcher
{
}

- (id) initWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
self = [super init];

self.fetchCompletionHandler = completionHandler;

self.quasselCoreConnection = [[QuasselCoreConnection alloc] init];
self.quasselCoreConnection.delegate = self;

return self;
}

- (void) connectTo:(NSString*)hostName port:(int)port userName:(NSString*)userName passWord:(NSString*)passWord
{
[self.quasselCoreConnection connectTo:hostName port:port userName:userName passWord:passWord];
}

- (void)quasselAllNetworkInitReceived {
NSLog(@"quasselAllNetworkInitReceived");
}

- (void)quasselAuthenticated {
NSLog(@"quasselAuthenticated");
}

- (void)quasselBufferListReceived {
NSLog(@"quasselBufferListReceived");
}

- (void)quasselBufferListUpdated {
NSLog(@"quasselBufferListUpdated");
}

- (void)quasselConnected {
NSLog(@"quasselConnected");
}

- (void)quasselEncrypted {
NSLog(@"quasselEncrypted");
}

- (void)quasselLastSeenMsgUpdated:(MsgId *)messageId forBuffer:(BufferId *)bufferId {

}

- (void)quasselMessageReceived:(Message *)msg received:(enum ReceiveStyle)style onIndex:(int)i {

}

- (void)quasselMessagesReceived:(NSArray *)messages received:(enum ReceiveStyle)style {

}

- (void)quasselNetworkInitReceived:(NSString *)networkName {

}

- (void)quasselNetworkNameUpdated:(NetworkId *)networkId {

}

- (void)quasselSocketDidDisconnect:(NSString *)msg {
if (self.fetchCompletionHandler) {
self.fetchCompletionHandler(UIBackgroundFetchResultFailed);
}
}

- (void)quasselSocketFailedConnect:(NSString *)msg {
self.fetchCompletionHandler(UIBackgroundFetchResultFailed);
self.fetchCompletionHandler = nil;
}

- (void)quasselSwitchToBuffer:(BufferId *)bufferId {

}

- (void) quasselFullyConnected {
NSLog(@"Fully connected");
int unreadCount = [self.quasselCoreConnection computeUnreadCountForAllBuffers];// computeRelevantUnreadCount
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:unreadCount]; // FIXME only relevant buffers
NSLog(@"%d", unreadCount);
if (unreadCount > 0) {
self.fetchCompletionHandler(UIBackgroundFetchResultNewData);
self.fetchCompletionHandler = nil;
} else {
self.fetchCompletionHandler(UIBackgroundFetchResultNoData);
self.fetchCompletionHandler = nil;
}

}
@end
2 changes: 2 additions & 0 deletions quassel-for-ios/quassel-for-ios/QuasselCoreConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
- (void) computeBufferActivityForBuffer:(BufferId*)bufferId;

- (int) computeUnreadCountForBuffer:(BufferId*)bufferId;
- (int) computeUnreadCountForAllBuffers;
- (int) computeRelevantUnreadCount;

- (void) sendMessage:(NSString*)msg toBuffer:(BufferId*)bufferId;
- (void) openQueryBufferForUser:(IrcUser*)user onNetwork:(NetworkId*)networkId;
Expand Down
25 changes: 24 additions & 1 deletion quassel-for-ios/quassel-for-ios/QuasselCoreConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,6 @@ - (void) backlogMessagesReceived:(NSMutableArray*)messagesReceived
// FIXME We assume that the third case cannot be
NSLog(@"backlogMessagesReceived THIRD CASE!");
}

}


Expand Down Expand Up @@ -1383,6 +1382,30 @@ - (enum BufferActivity) bufferActivityForBuffer:(BufferId*)bufferId
return (enum BufferActivity)[activity intValue];
}

-(int) computeUnreadCountForAllBuffers {
int count = 0;
for (BufferId *bufferId in self.visibleBufferIdsList) {
count += [self computeUnreadCountForBuffer:bufferId];
}
return count;
}

- (int) computeRelevantUnreadCount {
int count = 0;
for (BufferId *bufferId in self.visibleBufferIdsList) {
BufferInfo *bufferInfo = [self.bufferIdBufferInfoMap objectForKey:bufferId];
if (bufferInfo.bufferType == QueryBuffer) {
int thisBufferCount = [self computeUnreadCountForBuffer:bufferId];
NSLog(@"%@ -> %d", bufferInfo.bufferName, thisBufferCount);
count += thisBufferCount;
} else if (bufferInfo.bufferType == ChannelBuffer) {
// FIXME add if highlight
}
}
return count;
}


- (void) computeBufferActivityForBuffer:(BufferId*)bufferId
{
MsgId *lastSeenForBuffer = [self lastSeenMsgForBuffer:bufferId];
Expand Down

0 comments on commit 954d1e7

Please sign in to comment.