addAccount method
Add new account
Implementation
Future<void> addAccount(AccountModel acc, {bool saveChanges=true}) async {
_logs?.print('Adding new account: ${acc.uri}');
try {
_generateRandomLocalPort(acc);
acc.myAccId = await SiprixVoipSdk().addAccount(acc) ?? 0;
acc.regState = (acc.expireTime==0) ? RegState.removed : RegState.inProgress;
acc.regText = (acc.expireTime==0) ? "Removed" : "In progress...";
_integrateAddedAccount(acc, saveChanges);
} on PlatformException catch (err) {
if(err.code == SiprixVoipSdk.eDuplicateAccount.toString()) {
int existingAccId = err.details;
int idx = _accounts.indexWhere((account) => (account.myAccId == existingAccId));
if(idx==-1) {
//This case is possible in Android when:
// - activity started as usual and initialized SDK Core
// - activity destroyed, but SDK Core is still running (as Service)
// - activity started again, loaded saved state and has to sync it
acc.myAccId = existingAccId;
acc.regState = (acc.expireTime==0) ? RegState.removed : RegState.success;
acc.regText = (acc.expireTime==0) ? "Removed" : "200 OK";
_integrateAddedAccount(acc, saveChanges);
}
}
else {
_logs?.print('Can\'t add account: ${err.code} ${err.message} ');
return Future.error((err.message==null) ? err.code : err.message!);
}
} on Exception catch (err) {
_logs?.print('Can\'t add account: ${err.toString()}');
return Future.error(err.toString());
}
}