Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 4 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> |
| 5 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | // LRC |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 23 | #import <api/newaccountmodel.h> |
| 24 | #import <api/account.h> |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 25 | |
| 26 | #import "AccountSelectionManager.h" |
| 27 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 28 | @implementation AccountSelectionManager { |
| 29 | |
| 30 | const lrc::api::NewAccountModel* accMdl_; |
| 31 | |
| 32 | } |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 33 | |
| 34 | NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey"; |
| 35 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 36 | - (id) initWithAccountModel:(const lrc::api::NewAccountModel*) accMdl { |
| 37 | accMdl_ = accMdl; |
| 38 | return [self init]; |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 41 | - (void) saveAccountWithId:(NSString*)accId |
| 42 | { |
| 43 | [[NSUserDefaults standardUserDefaults] setObject:accId forKey:savedUserAccountKey]; |
| 44 | } |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 45 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 46 | - (NSString*) getSavedAccountId |
| 47 | { |
| 48 | return [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey]; |
| 49 | } |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 50 | |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 51 | - (void) clearSelectedAccount |
| 52 | { |
| 53 | [[NSUserDefaults standardUserDefaults] setObject:nil forKey:savedUserAccountKey]; |
| 54 | } |
| 55 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 56 | - (const lrc::api::account::Info&) savedAccount |
| 57 | { |
Anthony Léonard | ccb9d42 | 2018-02-13 16:00:23 -0500 | [diff] [blame] | 58 | NSString* savedAccountId = [self getSavedAccountId]; |
| 59 | if (savedAccountId == nil) { |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 60 | NSException* noAccEx = [NSException |
| 61 | exceptionWithName:@"NoAccountSavedException" |
| 62 | reason:@"No saved account" |
| 63 | userInfo:nil]; |
| 64 | @throw noAccEx; |
| 65 | } else { |
| 66 | try { |
| 67 | return accMdl_->getAccountInfo(std::string([savedAccountId UTF8String])); |
| 68 | } catch (std::out_of_range& e) { |
| 69 | NSException* outOfRangeEx = [NSException |
| 70 | exceptionWithName:@"outofrange" |
| 71 | reason:@"out of range" |
| 72 | userInfo:nil]; |
| 73 | @throw outOfRangeEx; |
| 74 | } |
| 75 | } |
| 76 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | - (void) setSavedAccount:(const lrc::api::account::Info&) acc |
| 80 | { |
| 81 | if (acc.profileInfo.type == lrc::api::profile::Type::INVALID) |
| 82 | return; |
Anthony Léonard | be33911 | 2017-12-26 11:36:48 -0500 | [diff] [blame] | 83 | |
| 84 | [self saveAccountWithId:@(acc.id.c_str())]; |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @end |