/* * Copyright (c) 2021 BWI GmbH * * 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 "RecentsBannerViewController.h" #import "GeneratedInterface-Swift.h" @interface RecentsBannerViewController () { TopBannerViewController *topBannerViewController; bool bannersInitialized; bool isBannerVisible; bool shouldHideBanner; } - (void)setTopBannerViewConstraints; - (void)showTopBanner; - (void)hideTopBanner; @end @implementation RecentsBannerViewController + (bool)isBannerHidden { NSDictionary *info = [[NSBundle mainBundle] infoDictionary]; NSString *version = [info objectForKey:@"CFBundleShortVersionString"]; if( [version isEqual:[[NSUserDefaults standardUserDefaults] objectForKey:@"de.bwi.messenger.top_banner_confirmation"]] ) return TRUE; else return FALSE; } - (void)viewDidLoad { [super viewDidLoad]; bannersInitialized = FALSE; isBannerVisible = FALSE; shouldHideBanner = FALSE; } - (void)onMatrixSessionChange { [super onMatrixSessionChange]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if( [BWIBuildSettings.shared showTopBanner] ) { [self showTopBanner]; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if( bannersInitialized && [BWIBuildSettings.shared showTopBanner] ) { [self hideTopBanner]; } } - (void)setTopBannerViewConstraints { // get the offset in the y-coordiante so that the banner appears below the navigation bar UIWindow *window = UIApplication.sharedApplication.windows.firstObject; CGFloat statusBarHeight = window.safeAreaInsets.top; CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height; NSLayoutConstraint *alignTopConstraint = [NSLayoutConstraint constraintWithItem:topBannerViewController.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:statusBarHeight + navigationBarHeight]; [[self view] addConstraint:alignTopConstraint]; NSLayoutConstraint *alignLeadingConstraint = [NSLayoutConstraint constraintWithItem:topBannerViewController.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; [[self view] addConstraint:alignLeadingConstraint]; NSLayoutConstraint *alignTrailingConstraint = [NSLayoutConstraint constraintWithItem:topBannerViewController.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; [[self view] addConstraint:alignTrailingConstraint]; NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:topBannerViewController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:NULL attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:130]; [[self view] addConstraint:heightConstraint]; } - (void)setupTopBanner { NSMutableArray *bannerControllers = [NSMutableArray array]; MXSession* session = [self mainSession]; if( !session || bannersInitialized) return; NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; FeatureBannerVisibilityService *service = [[FeatureBannerVisibilityService alloc] initWithMxSession:session]; [service isUnreadWithVersion:version completion:^(BOOL unread) { if (unread) { //[bannerControllers addObject:[[FeatureBannerViewController alloc] initWithSession:session version:version]]; self->topBannerViewController = [[TopBannerViewController alloc] initWithControllers: bannerControllers]; // this notification will be called either if the user clicked on the banner or wants to hide it using a swipe gesture [[NSNotificationCenter defaultCenter] addObserverForName:@"de.bwi.messenger.hide_top_banner" object:NULL queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { [self hideTopBanner]; [self->topBannerViewController removeAdvertiseViewControllers]; }]; // this notification will be called either if the user clicked on the banner or wants to hide it using a swipe gesture [[NSNotificationCenter defaultCenter] addObserverForName:@"de.bwi.messenger.mark_top_banner_as_read" object:NULL queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { shouldHideBanner = TRUE; }]; self->bannersInitialized = TRUE; if( [BWIBuildSettings.shared showTopBanner] ) { [self showTopBanner]; } } }]; } - (void)showTopBanner { if( isBannerVisible || shouldHideBanner) { return; // nothing to do because the banner is already visible for the user } if( [topBannerViewController.advertiseViewControllers count] > 0 ) { // add child view controller [self addChildViewController:topBannerViewController]; [[topBannerViewController view] setTranslatesAutoresizingMaskIntoConstraints:FALSE]; [[topBannerViewController view] setBounds:CGRectMake(0, 0, self.view.bounds.size.width, 100)]; [[self view] addSubview:[topBannerViewController view]]; [topBannerViewController didMoveToParentViewController:self]; [self setTopBannerViewConstraints]; isBannerVisible = TRUE; } } - (void)hideTopBanner { if( [[topBannerViewController advertiseViewControllers] count] > 0 ) { // remove child view controller [topBannerViewController willMoveToParentViewController:nil]; [topBannerViewController.view removeFromSuperview]; [topBannerViewController removeFromParentViewController]; } isBannerVisible = FALSE; } @end