AccountSettingsVC.mm 7.48 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2018-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

//LRC
#import <api/lrc.h>
#import <api/newaccountmodel.h>

#import "views/NSColor+RingTheme.h"
#import "AccountSettingsVC.h"
#import "AccRingGeneralVC.h"
#import "AccSipGeneralVC.h"
#import "AccAdvancedRingVC.h"
#import "AccAdvancedSipVC.h"

@interface AccountSettingsVC ()

@property (unsafe_unretained) IBOutlet NSScrollView *containerView;
@property (unsafe_unretained) IBOutlet NSView *settingsView;

@end

@implementation AccountSettingsVC

std::string selectedAccountID;
NSViewController <AccountGeneralProtocol>* accountGeneralVC;
NSViewController <AccountAdvancedProtocol>* accountAdvancedVC;
AccRingGeneralVC* ringGeneralVC;
AccSipGeneralVC* sipGeneralVC;
AccAdvancedRingVC* ringAdvancedVC;
AccAdvancedSipVC* sipAdvancedVC;

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
48
CGFloat const VIEW_INSET = 40;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

@synthesize accountModel;

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
{
    if (self =  [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
    {
        self.accountModel= accountModel;
    }
   ringGeneralVC =  [[AccRingGeneralVC alloc] initWithNibName:@"AccRingGeneral" bundle:nil accountmodel: accountModel];
   sipGeneralVC =  [[AccSipGeneralVC alloc] initWithNibName:@"AccSipGeneral" bundle:nil accountmodel: accountModel];
   ringAdvancedVC = [[AccAdvancedRingVC alloc] initWithNibName:@"AccAdvancedRing" bundle:nil accountmodel: accountModel];
   sipAdvancedVC = [[AccAdvancedSipVC alloc] initWithNibName:@"AccAdvancedSip" bundle:nil accountmodel: accountModel];
   return self;
}

- (void) initFrame
{
    [self.view setFrame:self.view.superview.bounds];
    [self.view setHidden:YES];
}

- (void) setSelectedAccount:(std::string) account {
    selectedAccountID = account;
    const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID);
    if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) {
        accountGeneralVC = ringGeneralVC;
        accountGeneralVC.delegate = self;
        accountAdvancedVC = ringAdvancedVC;
    } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){
        accountGeneralVC = sipGeneralVC;
        accountGeneralVC.delegate = self;
        accountAdvancedVC = sipAdvancedVC;
    } else {
        [self hide];
        return;
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
86
    [self.view.window makeFirstResponder:self.view];
87 88 89 90 91 92 93 94
    [accountGeneralVC setSelectedAccount: selectedAccountID];
    [accountAdvancedVC setSelectedAccount: selectedAccountID];
    [self displayGeneralSettings];
}

- (void) show {
    [self.view setHidden:NO];
    [self displayGeneralSettings];
95 96 97 98 99 100 101 102 103
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInset) name:NSWindowDidResizeNotification object:nil];
}

- (void)updateInset {
    if(self.containerView.documentView.frame.size.height > (self.containerView.frame.size.height + VIEW_INSET) &&  self.containerView.contentInsets.bottom <= VIEW_INSET) {
        return;
    }
    int bottomInset = self.containerView.frame.size.height - self.containerView.documentView.frame.size.height - VIEW_INSET;
    self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0);
104 105 106 107
}

-(void)displayGeneralSettings {
    self.containerView.documentView = accountGeneralVC.view;
108 109
    [self updateInset];

110 111
}

112 113
-(void)displayAllSettingAndScrollToAdvanced: (BOOL) shouldScroll {
    CGRect visibleRect = self.containerView.visibleRect;
114 115 116 117 118 119 120 121 122
    CGRect settingsFrame = accountGeneralVC.view.frame;
    settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height;
    NSView* container = [[NSView alloc] initWithFrame:settingsFrame];
    [container addSubview:accountAdvancedVC.view];
    CGRect generalSettingsFrame = accountGeneralVC.view.frame;
    generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height;
    accountGeneralVC.view.frame = generalSettingsFrame;
    [container addSubview:accountGeneralVC.view];
    self.containerView.documentView = container;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    //return scroll to position it was before changing scroll document view
    NSPoint oldOrigin = NSMakePoint(0.0, [[self.containerView documentView] frame].size.height
                                    - NSHeight([[self.containerView contentView] bounds])
                                    - (NSHeight([[accountGeneralVC view] bounds])
                                       - visibleRect.size.height));
    [[self.containerView documentView] scrollPoint: oldOrigin];
    if(!shouldScroll) {
        [self updateInset];
        return;
    }
    //animte scroll to advanced option
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0.5];
    NSClipView* clipView = [self.containerView contentView];
    NSPoint newOrigin = NSMakePoint(0.0, accountAdvancedVC.view.frame.size.height
                                    - visibleRect.size.height
                                    + VIEW_INSET);
    NSPoint clipViewOrigin = [clipView bounds].origin;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
141
    clipViewOrigin.y = clipViewOrigin.y - NSHeight([[accountGeneralVC view] bounds]) - VIEW_INSET * 0.5;
142 143 144 145 146 147 148 149 150 151 152 153
    CGFloat accountHeight = NSHeight([[accountGeneralVC view] bounds]);
    CGFloat visibleHeight = visibleRect.size.height;
    if(accountGeneralVC.view.frame.size.height > visibleRect.size.height) {
        clipViewOrigin.y = clipViewOrigin.y
        + accountGeneralVC.view.frame.size.height
        - visibleRect.size.height
        + VIEW_INSET;
    }
    [[clipView animator] setBoundsOrigin:clipViewOrigin];
    [NSAnimationContext endGrouping];
    [[self.containerView documentView] scrollPoint: newOrigin];
    [self updateInset];
154 155 156 157 158 159 160
}

-(void) scrollToTopScrollView: (NSScrollView *) scrollView {
    NSPoint newScrollOrigin;
    if ([[scrollView documentView] isFlipped]) {
        newScrollOrigin=NSMakePoint(0.0,0.0);
    } else {
161
        newScrollOrigin=NSMakePoint(0.0, NSMaxY([[ self.containerView documentView] frame])
162 163 164 165 166 167 168 169 170 171
                                    -NSHeight([[scrollView contentView] bounds]));
    }
    [[scrollView documentView] scrollPoint:newScrollOrigin];
}


#pragma mark - AccountGeneralDelegate methods

-(void) updateFrame {
    if (accountAdvancedVC.view.superview == self.containerView.documentView) {
172
        [self displayAllSettingAndScrollToAdvanced: NO];
173 174 175 176 177 178 179 180 181 182
        return;
    }
    [self displayGeneralSettings];
}

-(void) triggerAdvancedOptions {
    if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) {
        [self displayGeneralSettings];
        return;
    }
183
    [self displayAllSettingAndScrollToAdvanced: YES];
184 185 186 187
}

- (void) hide {
    [self.view setHidden:YES];
188
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil];
189 190
}

191

192
@end