Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2018-2019 Savoir-faire Linux Inc. |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 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> |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 22 | #import <api/newaccountmodel.h> |
| 23 | |
| 24 | #import "views/NSColor+RingTheme.h" |
| 25 | #import "AccountSettingsVC.h" |
| 26 | #import "AccRingGeneralVC.h" |
| 27 | #import "AccSipGeneralVC.h" |
| 28 | #import "AccAdvancedRingVC.h" |
| 29 | #import "AccAdvancedSipVC.h" |
| 30 | |
| 31 | @interface AccountSettingsVC () |
| 32 | |
| 33 | @property (unsafe_unretained) IBOutlet NSScrollView *containerView; |
| 34 | @property (unsafe_unretained) IBOutlet NSView *settingsView; |
| 35 | |
| 36 | @end |
| 37 | |
| 38 | @implementation AccountSettingsVC |
| 39 | |
| 40 | std::string selectedAccountID; |
| 41 | NSViewController <AccountGeneralProtocol>* accountGeneralVC; |
| 42 | NSViewController <AccountAdvancedProtocol>* accountAdvancedVC; |
| 43 | AccRingGeneralVC* ringGeneralVC; |
| 44 | AccSipGeneralVC* sipGeneralVC; |
| 45 | AccAdvancedRingVC* ringAdvancedVC; |
| 46 | AccAdvancedSipVC* sipAdvancedVC; |
| 47 | |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 48 | CGFloat const VIEW_INSET = 40; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 49 | |
| 50 | @synthesize accountModel; |
| 51 | |
| 52 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel |
| 53 | { |
| 54 | if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil]) |
| 55 | { |
| 56 | self.accountModel= accountModel; |
| 57 | } |
| 58 | ringGeneralVC = [[AccRingGeneralVC alloc] initWithNibName:@"AccRingGeneral" bundle:nil accountmodel: accountModel]; |
| 59 | sipGeneralVC = [[AccSipGeneralVC alloc] initWithNibName:@"AccSipGeneral" bundle:nil accountmodel: accountModel]; |
| 60 | ringAdvancedVC = [[AccAdvancedRingVC alloc] initWithNibName:@"AccAdvancedRing" bundle:nil accountmodel: accountModel]; |
| 61 | sipAdvancedVC = [[AccAdvancedSipVC alloc] initWithNibName:@"AccAdvancedSip" bundle:nil accountmodel: accountModel]; |
| 62 | return self; |
| 63 | } |
| 64 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 65 | - (void) initFrame |
| 66 | { |
| 67 | [self.view setFrame:self.view.superview.bounds]; |
| 68 | [self.view setHidden:YES]; |
| 69 | } |
| 70 | |
| 71 | - (void) setSelectedAccount:(std::string) account { |
| 72 | selectedAccountID = account; |
| 73 | const auto& accountInfo = accountModel->getAccountInfo(selectedAccountID); |
| 74 | if (accountInfo.profileInfo.type == lrc::api::profile::Type::RING) { |
| 75 | accountGeneralVC = ringGeneralVC; |
| 76 | accountGeneralVC.delegate = self; |
| 77 | accountAdvancedVC = ringAdvancedVC; |
| 78 | } else if (accountInfo.profileInfo.type == lrc::api::profile::Type::SIP){ |
| 79 | accountGeneralVC = sipGeneralVC; |
| 80 | accountGeneralVC.delegate = self; |
| 81 | accountAdvancedVC = sipAdvancedVC; |
| 82 | } else { |
| 83 | [self hide]; |
| 84 | return; |
| 85 | } |
Kateryna Kostiuk | 39ce23d | 2018-10-02 14:33:37 -0400 | [diff] [blame] | 86 | [self.view.window makeFirstResponder:self.view]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 87 | [accountGeneralVC setSelectedAccount: selectedAccountID]; |
| 88 | [accountAdvancedVC setSelectedAccount: selectedAccountID]; |
| 89 | [self displayGeneralSettings]; |
| 90 | } |
| 91 | |
| 92 | - (void) show { |
| 93 | [self.view setHidden:NO]; |
| 94 | [self displayGeneralSettings]; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 95 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInset) name:NSWindowDidResizeNotification object:nil]; |
| 96 | } |
| 97 | |
| 98 | - (void)updateInset { |
| 99 | if(self.containerView.documentView.frame.size.height > (self.containerView.frame.size.height + VIEW_INSET) && self.containerView.contentInsets.bottom <= VIEW_INSET) { |
| 100 | return; |
| 101 | } |
| 102 | int bottomInset = self.containerView.frame.size.height - self.containerView.documentView.frame.size.height - VIEW_INSET; |
| 103 | self.containerView.contentInsets = NSEdgeInsetsMake(VIEW_INSET, 0, bottomInset, 0); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | -(void)displayGeneralSettings { |
| 107 | self.containerView.documentView = accountGeneralVC.view; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 108 | [self updateInset]; |
| 109 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 112 | -(void)displayAllSettingAndScrollToAdvanced: (BOOL) shouldScroll { |
| 113 | CGRect visibleRect = self.containerView.visibleRect; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 114 | CGRect settingsFrame = accountGeneralVC.view.frame; |
| 115 | settingsFrame.size.height = settingsFrame.size.height + accountAdvancedVC.view.frame.size.height; |
| 116 | NSView* container = [[NSView alloc] initWithFrame:settingsFrame]; |
Kateryna Kostiuk | 782ac77 | 2019-11-29 16:12:30 -0500 | [diff] [blame] | 117 | CGRect advancedFrame = accountAdvancedVC.view.frame; |
| 118 | advancedFrame.size.width = settingsFrame.size.width; |
| 119 | accountAdvancedVC.view.frame = advancedFrame; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 120 | [container addSubview:accountAdvancedVC.view]; |
| 121 | CGRect generalSettingsFrame = accountGeneralVC.view.frame; |
| 122 | generalSettingsFrame.origin.y = accountAdvancedVC.view.frame.size.height; |
| 123 | accountGeneralVC.view.frame = generalSettingsFrame; |
| 124 | [container addSubview:accountGeneralVC.view]; |
| 125 | self.containerView.documentView = container; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 126 | //return scroll to position it was before changing scroll document view |
| 127 | NSPoint oldOrigin = NSMakePoint(0.0, [[self.containerView documentView] frame].size.height |
| 128 | - NSHeight([[self.containerView contentView] bounds]) |
| 129 | - (NSHeight([[accountGeneralVC view] bounds]) |
| 130 | - visibleRect.size.height)); |
| 131 | [[self.containerView documentView] scrollPoint: oldOrigin]; |
| 132 | if(!shouldScroll) { |
| 133 | [self updateInset]; |
| 134 | return; |
| 135 | } |
| 136 | //animte scroll to advanced option |
| 137 | [NSAnimationContext beginGrouping]; |
| 138 | [[NSAnimationContext currentContext] setDuration:0.5]; |
| 139 | NSClipView* clipView = [self.containerView contentView]; |
| 140 | NSPoint newOrigin = NSMakePoint(0.0, accountAdvancedVC.view.frame.size.height |
| 141 | - visibleRect.size.height |
| 142 | + VIEW_INSET); |
| 143 | NSPoint clipViewOrigin = [clipView bounds].origin; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 144 | clipViewOrigin.y = clipViewOrigin.y - NSHeight([[accountGeneralVC view] bounds]) - VIEW_INSET * 0.5; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 145 | CGFloat accountHeight = NSHeight([[accountGeneralVC view] bounds]); |
| 146 | CGFloat visibleHeight = visibleRect.size.height; |
| 147 | if(accountGeneralVC.view.frame.size.height > visibleRect.size.height) { |
| 148 | clipViewOrigin.y = clipViewOrigin.y |
| 149 | + accountGeneralVC.view.frame.size.height |
| 150 | - visibleRect.size.height |
| 151 | + VIEW_INSET; |
| 152 | } |
| 153 | [[clipView animator] setBoundsOrigin:clipViewOrigin]; |
| 154 | [NSAnimationContext endGrouping]; |
| 155 | [[self.containerView documentView] scrollPoint: newOrigin]; |
| 156 | [self updateInset]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | -(void) scrollToTopScrollView: (NSScrollView *) scrollView { |
| 160 | NSPoint newScrollOrigin; |
| 161 | if ([[scrollView documentView] isFlipped]) { |
| 162 | newScrollOrigin=NSMakePoint(0.0,0.0); |
| 163 | } else { |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 164 | newScrollOrigin=NSMakePoint(0.0, NSMaxY([[ self.containerView documentView] frame]) |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 165 | -NSHeight([[scrollView contentView] bounds])); |
| 166 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 167 | [[scrollView documentView] scrollPoint:newScrollOrigin]; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | #pragma mark - AccountGeneralDelegate methods |
| 172 | |
| 173 | -(void) updateFrame { |
| 174 | if (accountAdvancedVC.view.superview == self.containerView.documentView) { |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 175 | [self displayAllSettingAndScrollToAdvanced: NO]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | [self displayGeneralSettings]; |
| 179 | } |
| 180 | |
| 181 | -(void) triggerAdvancedOptions { |
| 182 | if(self.containerView.documentView.frame.size.height == (accountGeneralVC.view.frame.size.height + accountAdvancedVC.view.frame.size.height)) { |
| 183 | [self displayGeneralSettings]; |
| 184 | return; |
| 185 | } |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 186 | [self displayAllSettingAndScrollToAdvanced: YES]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | - (void) hide { |
| 190 | [self.view setHidden:YES]; |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 191 | [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Kateryna Kostiuk | a852594 | 2018-10-17 14:33:39 -0400 | [diff] [blame] | 194 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 195 | @end |