element-ios/matrixConsole/ViewController/RageShakableTableViewController.m
giomfo 4b76c6b24d Console: Improve offline mode:
- remove loading wheel when network is unreachable.
- color in red the navigation bar when the app is offline.

Buf Fix SYIOS- 66 - Console: last outgoing message is stuck as local echo whereas the message has been delivered.
2015-02-12 11:16:28 +01:00

108 lines
3.8 KiB
Objective-C

/*
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "RageShakableTableViewController.h"
#import "RageShakableUIResponder.h"
@interface RageShakableTableViewController () {
id reachabilityObserver;
}
@end
@implementation RageShakableTableViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[RageShakableUIResponder cancel:self];
if (self.navigationController) {
// The navigation bar tintColor depends on reachability status - Register reachability observer
reachabilityObserver = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingReachabilityDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
[self onReachabilityStatusChange];
}];
// Force update
[self onReachabilityStatusChange];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:reachabilityObserver];
[RageShakableUIResponder cancel:self];
}
#pragma mark - Reachability monitoring
- (void)onReachabilityStatusChange {
// Retrieve the current reachability status
AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
AFNetworkReachabilityStatus status = reachabilityManager.networkReachabilityStatus;
// Retrieve the main navigation controller if the current view controller is embedded inside a split view controller.
UINavigationController *mainNavigationController = nil;
if (self.splitViewController) {
mainNavigationController = self.navigationController;
UIViewController *parentViewController = self.parentViewController;
while (parentViewController) {
if (parentViewController.navigationController) {
mainNavigationController = parentViewController.navigationController;
parentViewController = parentViewController.parentViewController;
} else {
break;
}
}
}
// Update navigationBar tintColor
if (status == AFNetworkReachabilityStatusNotReachable) {
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
if (mainNavigationController) {
mainNavigationController.navigationBar.barTintColor = [UIColor redColor];
}
} else if (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN) {
self.navigationController.navigationBar.barTintColor = nil;
if (mainNavigationController) {
mainNavigationController.navigationBar.barTintColor = nil;
}
}
}
#pragma mark - rageshake : screenshot
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
[RageShakableUIResponder startShaking:self];
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
[self motionEnded:motion withEvent:event];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
[RageShakableUIResponder stopShaking:self];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
@end