Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | //LRC |
| 21 | #import <api/lrc.h> |
| 22 | #import <api/account.h> |
| 23 | #import <api/newaccountmodel.h> |
| 24 | |
| 25 | #import "views/NSColor+RingTheme.h" |
| 26 | #import "AccountSettingsVC.h" |
| 27 | #import "AccRingGeneralVC.h" |
| 28 | #import "AccSipGeneralVC.h" |
| 29 | #import "AccAdvancedRingVC.h" |
| 30 | #import "AccAdvancedSipVC.h" |
| 31 | |
| 32 | @interface AccountSettingsVC () |
| 33 | |
| 34 | @property (unsafe_unretained) IBOutlet NSScrollView *containerView; |
| 35 | @property (unsafe_unretained) IBOutlet NSView *settingsView; |
| 36 | |
| 37 | @end |
| 38 | |
| 39 | @implementation AccountSettingsVC |
| 40 | |
| 41 | std::string selectedAccountID; |
| 42 | NSViewController <AccountGeneralProtocol>* accountGeneralVC; |
| 43 | NSViewController <AccountAdvancedProtocol>* accountAdvancedVC; |
| 44 | AccRingGeneralVC* ringGeneralVC; |
| 45 | AccSipGeneralVC* sipGeneralVC; |
| 46 | AccAdvancedRingVC* ringAdvancedVC; |
| 47 | AccAdvancedSipVC* sipAdvancedVC; |
| 48 | |
| 49 | CGFloat const VIEW_INSET = 20; |
| 50 | |
| 51 | @synthesize accountModel; |
| 52 | |
| 53 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel |
| 54 | { |
| 55 | if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil]) |
| 56 | { |
| 57 | self.accountModel= accountModel; |
| 58 | } |
| 59 | ringGeneralVC = [[AccRingGeneralVC alloc] initWithNibName:@"AccRingGeneral" bundle:nil accountmodel: accountModel]; |
| 60 | sipGeneralVC = [[AccSipGeneralVC alloc] initWithNibName:@"AccSipGeneral" bundle:nil accountmodel: accountModel]; |
| 61 | ringAdvancedVC = [[AccAdvancedRingVC alloc] initWithNibName:@"AccAdvancedRing" bundle:nil accountmodel: accountModel]; |
| 62 | sipAdvancedVC = [[AccAdvancedSipVC alloc] initWithNibName:@"AccAdvancedSip" bundle:nil accountmodel: accountModel]; |
| 63 | return self; |
| 64 | } |
| 65 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 66 | - (void) initFrame |
| 67 | { |
| 68 | [self.view setFrame:self.view.superview.bounds]; |
| 69 | [self.view setHidden:YES]; |
| 70 | } |
| 71 | |
| 72 | - (void) setSelectedAccount:(std::string) account { |
| 73 | selectedAccountID = account; |
| 74 | const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID); |
| 75 | if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) { |
| 76 | accountGeneralVC = ringGeneralVC; |
| 77 | accountGeneralVC.delegate = self; |
| 78 | accountAdvancedVC = ringAdvancedVC; |
| 79 | } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){ |
| 80 | accountGeneralVC = sipGeneralVC; |
| 81 | accountGeneralVC.delegate = self; |
| 82 | accountAdvancedVC = sipAdvancedVC; |
| 83 | } else { |
| 84 | [self hide]; |
| 85 | return; |
| 86 | } |
Kateryna Kostiuk | 39ce23d | 2018-10-02 14:33:37 -0400 | [diff] [blame] | 87 | [self.view.window makeFirstResponder:self.view]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 88 | [accountGeneralVC setSelectedAccount: selectedAccountID]; |
| 89 | [accountAdvancedVC setSelectedAccount: selectedAccountID]; |
| 90 | [self displayGeneralSettings]; |
| 91 | } |
| 92 | |
| 93 | - (void) show { |
| 94 | [self.view setHidden:NO]; |
| 95 | [self displayGeneralSettings]; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 96 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInset) name:NSWindowDidResizeNotification object:nil]; |
| 97 | } |
| 98 | |
| 99 | - (void)updateInset { |
| 100 | if(self.containerView.documentView.frame.size.height > (self.containerView.frame.size.height + VIEW_INSET) && self.containerView.contentInsets.bottom <= VIEW_INSET) { |
| 101 | return; |
| 102 | } |
| 103 | int bottomInset = self.containerView.frame.size.height - self.containerView.documentView.frame.size.height - VIEW_INSET; |
| 104 | self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | -(void)displayGeneralSettings { |
| 108 | self.containerView.documentView = accountGeneralVC.view; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 109 | [self updateInset]; |
| 110 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 113 | -(void)displayAllSettingAndScrollToAdvanced: (BOOL) shouldScroll { |
| 114 | CGRect visibleRect = self.containerView.visibleRect; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 115 | CGRect settingsFrame = accountGeneralVC.view.frame; |
| 116 | settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height; |
| 117 | NSView* container = [[NSView alloc] initWithFrame:settingsFrame]; |
| 118 | [container addSubview:accountAdvancedVC.view]; |
| 119 | CGRect generalSettingsFrame = accountGeneralVC.view.frame; |
| 120 | generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height; |
| 121 | accountGeneralVC.view.frame = generalSettingsFrame; |
| 122 | [container addSubview:accountGeneralVC.view]; |
| 123 | self.containerView.documentView = container; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 124 | //return scroll to position it was before changing scroll document view |
| 125 | NSPoint oldOrigin = NSMakePoint(0.0, [[self.containerView documentView] frame].size.height |
| 126 | - NSHeight([[self.containerView contentView] bounds]) |
| 127 | - (NSHeight([[accountGeneralVC view] bounds]) |
| 128 | - visibleRect.size.height)); |
| 129 | [[self.containerView documentView] scrollPoint: oldOrigin]; |
| 130 | if(!shouldScroll) { |
| 131 | [self updateInset]; |
| 132 | return; |
| 133 | } |
| 134 | //animte scroll to advanced option |
| 135 | [NSAnimationContext beginGrouping]; |
| 136 | [[NSAnimationContext currentContext] setDuration:0.5]; |
| 137 | NSClipView* clipView = [self.containerView contentView]; |
| 138 | NSPoint newOrigin = NSMakePoint(0.0, accountAdvancedVC.view.frame.size.height |
| 139 | - visibleRect.size.height |
| 140 | + VIEW_INSET); |
| 141 | NSPoint clipViewOrigin = [clipView bounds].origin; |
| 142 | clipViewOrigin.y = clipViewOrigin.y - NSHeight([[accountGeneralVC view] bounds]); |
| 143 | CGFloat accountHeight = NSHeight([[accountGeneralVC view] bounds]); |
| 144 | CGFloat visibleHeight = visibleRect.size.height; |
| 145 | if(accountGeneralVC.view.frame.size.height > visibleRect.size.height) { |
| 146 | clipViewOrigin.y = clipViewOrigin.y |
| 147 | + accountGeneralVC.view.frame.size.height |
| 148 | - visibleRect.size.height |
| 149 | + VIEW_INSET; |
| 150 | } |
| 151 | [[clipView animator] setBoundsOrigin:clipViewOrigin]; |
| 152 | [NSAnimationContext endGrouping]; |
| 153 | [[self.containerView documentView] scrollPoint: newOrigin]; |
| 154 | [self updateInset]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | -(void) scrollToTopScrollView: (NSScrollView *) scrollView { |
| 158 | NSPoint newScrollOrigin; |
| 159 | if ([[scrollView documentView] isFlipped]) { |
| 160 | newScrollOrigin=NSMakePoint(0.0,0.0); |
| 161 | } else { |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 162 | newScrollOrigin=NSMakePoint(0.0, NSMaxY([[ self.containerView documentView] frame]) |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 163 | -NSHeight([[scrollView contentView] bounds])); |
| 164 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 165 | [[scrollView documentView] scrollPoint:newScrollOrigin]; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | #pragma mark - AccountGeneralDelegate methods |
| 170 | |
| 171 | -(void) updateFrame { |
| 172 | if (accountAdvancedVC.view.superview == self.containerView.documentView) { |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 173 | [self displayAllSettingAndScrollToAdvanced: NO]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | [self displayGeneralSettings]; |
| 177 | } |
| 178 | |
| 179 | -(void) triggerAdvancedOptions { |
| 180 | if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) { |
| 181 | [self displayGeneralSettings]; |
| 182 | return; |
| 183 | } |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 184 | [self displayAllSettingAndScrollToAdvanced: YES]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | - (void) hide { |
| 188 | [self.view setHidden:YES]; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 189 | [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame^] | 192 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 193 | @end |