Skip to content

Commit

Permalink
Merge pull request #505 in ADGUARD-IOS/adguard-ios from feature/1089_…
Browse files Browse the repository at this point in the history
…2 to v3.2

* commit 'a36b5bc3a6e26b422bd8ad287f894081bab94e2d':
  fixes
  build fix
  - fix tests - add confirmation allert
  move reset settings to appDelegate
  reset login
  reset vpn and remove all files
  reset filters
  move database iniitialization to AntibannerController
  fixes
  fixes
  refactor
  fixed tests
  refactor AEService
  • Loading branch information
IvanIin committed Oct 22, 2019
2 parents c3ead39 + a36b5bc commit bb5cf5c
Show file tree
Hide file tree
Showing 61 changed files with 710 additions and 1,302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ActionViewController: UIViewController {
private let safariService: SafariService
private let contentBlockerService: ContentBlockerService
private let networking = ACNNetworking()
private let aeService: AEServiceProtocol
private let antibannerController: AntibannerControllerProtocol
private let support: AESSupport
private var theme: ThemeServiceProtocol?
private let asDataBase = ASDatabase()
Expand All @@ -74,9 +74,10 @@ class ActionViewController: UIViewController {

required init?(coder: NSCoder) {
safariService = SafariService(resources: sharedResources)
contentBlockerService = ContentBlockerService(resources: sharedResources, safariService: safariService)
aeService = AEService(contentBlocker: contentBlockerService, resources: sharedResources, networking: networking, asDataBase: asDataBase)
support = AESSupport(resources: sharedResources, safariSevice: safariService, aeService: aeService)
let antibanner = AESAntibanner()
self.antibannerController = AntibannerController(antibanner: antibanner)
contentBlockerService = ContentBlockerService(resources: sharedResources, safariService: safariService, antibanner: antibanner)
support = AESSupport(resources: sharedResources, safariSevice: safariService, antibanner: antibanner)

super.init(coder: coder)
}
Expand Down Expand Up @@ -127,7 +128,7 @@ class ActionViewController: UIViewController {
errorMessage = error?.localizedDescription ?? ""
}
} else {
sSelf.aeService.onReady {
sSelf.antibannerController.onReady { (antibanner) in
// Add observers for application notifications
sSelf.addObservers()

Expand Down Expand Up @@ -305,13 +306,13 @@ class ActionViewController: UIViewController {
} else if !sSelf.asDataBase.ready {
sSelf.dbObserver = sSelf.asDataBase.observe(\.ready, options: .new, changeHandler: { (db, change) in
if sSelf.asDataBase.ready{
sSelf.aeService.start()
sSelf.antibannerController.start()
}
})
}
//--------------------- Start Services ---------------------------
else {
sSelf.aeService.start()
sSelf.antibannerController.start()
}
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions AdguardExtension/Adguard-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
#import "ASDFilterObjects.h"
#import "AESAntibanner.h"
#import "AEUIPlayerViewController.h"
#import "AEService.h"
#import "AESharedResources.h"
#import "ABECRequest.h"
#import "ACNNetworking.h"
#import "AEService.h"
#import "ACSSystemUtils.h"
#import "AEInvertedWhitelistDomainsObject.h"
#import "AEInvertedWhitelistDomainsObject.h"
Expand All @@ -31,3 +29,4 @@
#import "ASDatabase.h"
#import "Reachability.h"
#import "AppDelegate.h"
#import "AESProductSchemaManager.h"
71 changes: 12 additions & 59 deletions AdguardExtension/Adguard.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions AdguardExtension/AdguardApp/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern NSString *ShowCommonAlertNotification;
*/
- (BOOL)invalidateAntibanner:(BOOL)fromUI interactive:(BOOL)interactive;

- (void) resetAllSettings;


@end

135 changes: 36 additions & 99 deletions AdguardExtension/AdguardApp/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#import "ACommons/ACNetwork.h"
#import "ADomain/ADomain.h"
#import "AppDelegate.h"
#import "ASDatabase/ASDatabase.h"
#import "AEService.h"
#import "AESAntibanner.h"
#import "AESFilterConverter.h"

Expand Down Expand Up @@ -70,10 +68,10 @@ @interface AppDelegate (){
AEDownloadsCompletionBlock _downloadCompletion;
NSArray *_updatedFilters;
AESharedResources *_resources;
AEService * _aeService;
id<AntibannerControllerProtocol> _antibannerController;
id<AESAntibannerProtocol> _antibanner;
ContentBlockerService* _contentBlockerService;
PurchaseService* _purchaseService;
ASDatabase* _asDataBase;

BOOL _activateWithOpenUrl;

Expand All @@ -93,6 +91,14 @@ @implementation AppDelegate

- (instancetype)init {
self = [super init];

[StartupService start];
_resources = [ServiceLocator.shared getSetviceWithTypeName:@"AESharedResourcesProtocol"];
_antibannerController = [ServiceLocator.shared getSetviceWithTypeName:@"AntibannerControllerProtocol"];
_contentBlockerService = [ServiceLocator.shared getSetviceWithTypeName:@"ContentBlockerService"];
_purchaseService = [ServiceLocator.shared getSetviceWithTypeName:@"PurchaseServiceProtocol"];
_antibanner = [ServiceLocator.shared getSetviceWithTypeName:@"AESAntibannerProtocol"];

helper = [[AppDelegateHelper alloc] initWithAppDelegate:self];

return self;
Expand All @@ -104,14 +110,6 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:

//------------- Preparing for start application. Stage 1. -----------------

[StartupService start];
_resources = [ServiceLocator.shared getSetviceWithTypeName:@"AESharedResourcesProtocol"];
_aeService = [ServiceLocator.shared getSetviceWithTypeName:@"AEServiceProtocol"];
_contentBlockerService = [ServiceLocator.shared getSetviceWithTypeName:@"ContentBlockerService"];
_purchaseService = [ServiceLocator.shared getSetviceWithTypeName:@"PurchaseService"];
_asDataBase = [ServiceLocator.shared getSetviceWithTypeName:@"ASDatabase"];


BOOL succeeded = [helper application:application willFinishLaunchingWithOptions:launchOptions];

// Init Logger
Expand All @@ -130,31 +128,11 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:
_activateWithOpenUrl = NO;
self.userDefaultsInitialized = NO;

// Init database
[_asDataBase initDbWithURL:[[AESharedResources sharedResuorcesURL] URLByAppendingPathComponent:AE_PRODUCTION_DB] upgradeDefaultDb:YES];

//------------ Interface Tuning -----------------------------------
self.window.backgroundColor = [UIColor whiteColor];

if (application.applicationState != UIApplicationStateBackground) {
[_aeService onReady:^{
[_purchaseService checkPremiumStatusChanged];
}];
}

if ([_aeService firstRunInProgress]) {

[_aeService onReady:^{
[AESProductSchemaManager install];

[_purchaseService checkLicenseStatus];
}];
}
else{

[_aeService onReady:^{
[AESProductSchemaManager upgradeWithAntibanner: _aeService.antibanner];
}];
[_purchaseService checkPremiumStatusChanged];
}

return succeeded;
Expand All @@ -178,27 +156,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(antibannerNotify:) name:ASAntibannerUpdatePartCompletedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAlertNotification:) name:ShowCommonAlertNotification object:nil];

//------------ Checking DB status -----------------------------
if (_asDataBase.error) {

DDLogWarn(@"(AppDelegate) Stage 2. DB Error. Panic!");
// [self dbFailure];
}
else if (!_asDataBase.ready){

DDLogWarn(@"(AppDelegate) Stage 2. DB not ready.");
[_asDataBase addObserver:self forKeyPath:@"ready" options:NSKeyValueObservingOptionNew context:nil];
}
//--------------------- Start Services ---------------------------
else{

[_aeService start];
DDLogInfo(@"(AppDelegate) Stage 2. Main service started.");
}

//---------------------- Set period for checking filters ---------------------
[self setPeriodForCheckingFilters];
DDLogInfo(@"(AppDelegate) Stage 2 completed.");

return YES;
}
Expand Down Expand Up @@ -254,17 +213,16 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
// If theme mode is System Default gets current style
[self setAppInterfaceStyle];

[_aeService onReady:^{

[[_aeService antibanner] repairUpdateStateWithCompletionBlock:^{
[_antibannerController onReady:^(id<AESAntibannerProtocol> _Nonnull antibanner) {
[antibanner repairUpdateStateWithCompletionBlock:^{

if (_activateWithOpenUrl) {
_activateWithOpenUrl = NO;
DDLogInfo(@"(AppDelegate - applicationDidBecomeActive) Update process did not start because app activated with open URL.");
return;
}

if (_aeService.antibanner.updatesRightNow) {
if (antibanner.updatesRightNow) {
DDLogInfo(@"(AppDelegate - applicationDidBecomeActive) Update process did not start because it is performed right now.");
return;
}
Expand Down Expand Up @@ -308,11 +266,11 @@ - (void)application:(UIApplication *)application performFetchWithCompletionHandl
//Entry point for updating of the filters
_fetchCompletion = completionHandler;

[_aeService onReady:^{
[_antibannerController onReady:^(id<AESAntibannerProtocol> _Nonnull antibanner) {

[[_aeService antibanner] repairUpdateStateWithCompletionBlock:^{
[antibanner repairUpdateStateWithCompletionBlock:^{

if (_aeService.antibanner.updatesRightNow) {
if (antibanner.updatesRightNow) {
DDLogInfo(@"(AppDelegate) Update process did not start because it is performed right now.");
return;
}
Expand Down Expand Up @@ -342,10 +300,9 @@ - (void)application:(UIApplication *)application handleEventsForBackgroundURLSes

if ([identifier isEqualToString:AE_FILTER_UPDATES_ID]) {

[_aeService onReady:^{

[_antibannerController onReady:^(id<AESAntibannerProtocol> _Nonnull antibanner) {
_downloadCompletion = completionHandler;
[[_aeService antibanner] repairUpdateStateForBackground];
[antibanner repairUpdateStateForBackground];
}];
}
else{
Expand All @@ -359,7 +316,7 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
DDLogError(@"(AppDelegate) application Open URL.");

_activateWithOpenUrl = YES;

return [helper application:app open:url options:options];
}

Expand All @@ -385,17 +342,19 @@ - (BOOL)invalidateAntibanner:(BOOL)fromUI interactive:(BOOL)interactive {
DDLogInfo(@"(AppDelegate) Update process started by timer.");
}

[[_aeService antibanner] beginTransaction];
__block BOOL result = NO;

[_antibanner beginTransaction];
DDLogInfo(@"(AppDelegate) Begin of the Update Transaction from - invalidateAntibanner.");

BOOL result = [[_aeService antibanner] startUpdatingForced:fromUI interactive:interactive];
result = [_antibanner startUpdatingForced:fromUI interactive:interactive];

if (! result) {
DDLogInfo(@"(AppDelegate) Update process did not start because [antibanner startUpdatingForced] return NO.");
[[_aeService antibanner] rollbackTransaction];
[_antibanner rollbackTransaction];
DDLogInfo(@"(AppDelegate) Rollback of the Update Transaction from ASAntibannerDidntStartUpdateNotification.");
}

return result;
}

Expand All @@ -406,28 +365,8 @@ - (BOOL)invalidateAntibanner:(BOOL)fromUI interactive:(BOOL)interactive {
}
}

/////////////////////////////////////////////////////////////////////
#pragma mark Observing notifications
/////////////////////////////////////////////////////////////////////

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

// DB DELAYED READY
if ([object isEqual:_asDataBase]
&& [keyPath isEqualToString:@"ready"]
&& _asDataBase.ready) {

[_asDataBase removeObserver:self forKeyPath:@"ready"];

//--------------------- Start Services ---------------------------
[_aeService start];
DDLogInfo(@"(AppDelegate) DB service ready. Main service started.");

return;
}

// Default processing
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- (void)resetAllSettings {
[helper resetAllSettings];
}

/////////////////////////////////////////////////////////////////////
Expand All @@ -443,8 +382,7 @@ - (void)antibannerNotify:(NSNotification *)notification {
[_contentBlockerService reloadJsonsWithBackgroundUpdate:background completion:^(NSError *error) {

if (error) {

[[_aeService antibanner] rollbackTransaction];
[_antibanner rollbackTransaction];
DDLogInfo(@"(AppDelegate) Rollback of the Update Transaction from ASAntibannerUpdateFilterRulesNotification.");

[self updateFailuredNotify];
Expand All @@ -463,8 +401,7 @@ - (void)antibannerNotify:(NSNotification *)notification {
// Success antibanner updated from backend

[_resources.sharedDefaults setObject:[NSDate date] forKey:AEDefaultsCheckFiltersLastDate];

[[_aeService antibanner] endTransaction];
[_antibanner endTransaction];
DDLogInfo(@"(AppDelegate) End of the Update Transaction from ASAntibannerUpdateFilterRulesNotification.");

[self updateFinishedNotify];
Expand All @@ -483,9 +420,9 @@ - (void)antibannerNotify:(NSNotification *)notification {
else if ([notification.name
isEqualToString:ASAntibannerDidntStartUpdateNotification]) {

if ([[_aeService antibanner] inTransaction]) {
if ([_antibanner inTransaction]) {

[[_aeService antibanner] rollbackTransaction];
[_antibanner rollbackTransaction];
DDLogInfo(@"(AppDelegate) Rollback of the Update Transaction from ASAntibannerDidntStartUpdateNotification.");
}

Expand All @@ -500,10 +437,10 @@ - (void)antibannerNotify:(NSNotification *)notification {

[_contentBlockerService reloadJsonsWithBackgroundUpdate:YES completion:^(NSError * _Nullable error) {

if ([[_aeService antibanner] inTransaction]) {
if ([_antibanner inTransaction]) {
// Success antibanner updated from backend
[_resources.sharedDefaults setObject:[NSDate date] forKey:AEDefaultsCheckFiltersLastDate];
[[_aeService antibanner] endTransaction];
[_antibanner endTransaction];
DDLogInfo(@"(AppDelegate) End of the Update Transaction from ASAntibannerFinishedUpdateNotification.");

[self updateFinishedNotify];
Expand All @@ -521,9 +458,9 @@ - (void)antibannerNotify:(NSNotification *)notification {
else if ([notification.name
isEqualToString:ASAntibannerFailuredUpdateNotification]) {

if ([[_aeService antibanner] inTransaction]) {
if ([_antibanner inTransaction]) {

[[_aeService antibanner] rollbackTransaction];
[_antibanner rollbackTransaction];
DDLogInfo(@"(AppDelegate) Rollback of the Update Transaction from ASAntibannerFailuredUpdateNotification.");
}

Expand Down
Loading

0 comments on commit bb5cf5c

Please sign in to comment.