2016-05-03 00:50:16 +03:00
using System ;
using Bit.App.Abstractions ;
2016-05-07 20:42:09 +03:00
using Bit.App.Resources ;
2016-05-03 00:50:16 +03:00
using Xamarin.Forms ;
using XLabs.Ioc ;
2016-05-13 04:30:02 +03:00
using Bit.App.Controls ;
2016-05-18 06:09:20 +03:00
using Acr.UserDialogs ;
2016-05-22 05:50:15 +03:00
using Plugin.Settings.Abstractions ;
using Plugin.Fingerprint.Abstractions ;
2016-06-30 07:36:44 +03:00
using PushNotification.Plugin.Abstractions ;
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
{
2016-06-28 02:53:31 +03:00
public class SettingsPage : ExtendedContentPage
2016-05-03 00:50:16 +03:00
{
2016-05-08 07:28:14 +03:00
private readonly IAuthService _authService ;
2016-05-18 06:09:20 +03:00
private readonly IUserDialogs _userDialogs ;
2016-05-22 05:50:15 +03:00
private readonly ISettings _settings ;
private readonly IFingerprint _fingerprint ;
2016-06-30 07:36:44 +03:00
private readonly IPushNotification _pushNotification ;
2016-05-22 05:50:15 +03:00
// TODO: Model binding context?
2016-05-03 00:50:16 +03:00
public SettingsPage ( )
{
2016-05-08 07:28:14 +03:00
_authService = Resolver . Resolve < IAuthService > ( ) ;
2016-05-18 06:09:20 +03:00
_userDialogs = Resolver . Resolve < IUserDialogs > ( ) ;
2016-05-22 05:50:15 +03:00
_settings = Resolver . Resolve < ISettings > ( ) ;
_fingerprint = Resolver . Resolve < IFingerprint > ( ) ;
2016-06-30 07:36:44 +03:00
_pushNotification = Resolver . Resolve < IPushNotification > ( ) ;
2016-05-08 07:28:14 +03:00
Init ( ) ;
}
2016-05-22 05:50:15 +03:00
private ExtendedSwitchCell PinCell { get ; set ; }
private ExtendedSwitchCell FingerprintCell { get ; set ; }
private ExtendedTextCell LockOptionsCell { get ; set ; }
2016-05-08 07:28:14 +03:00
private void Init ( )
{
2016-05-22 05:50:15 +03:00
FingerprintCell = new ExtendedSwitchCell
{
Text = "Use Touch ID" + ( ! _fingerprint . IsAvailable ? " (Unavilable)" : null ) ,
On = _settings . GetValueOrDefault < bool > ( Constants . SettingFingerprintUnlockOn ) ,
IsEnabled = _fingerprint . IsAvailable
} ;
FingerprintCell . OnChanged + = FingerprintCell_Changed ;
PinCell = new ExtendedSwitchCell
2016-05-18 06:09:20 +03:00
{
2016-05-22 05:50:15 +03:00
Text = "Use PIN Code" ,
On = _settings . GetValueOrDefault < bool > ( Constants . SettingPinUnlockOn )
2016-05-18 06:09:20 +03:00
} ;
2016-05-22 05:50:15 +03:00
PinCell . OnChanged + = PinCell_Changed ;
2016-05-18 06:09:20 +03:00
2016-05-22 05:50:15 +03:00
LockOptionsCell = new ExtendedTextCell
2016-05-18 06:09:20 +03:00
{
2016-05-22 05:50:15 +03:00
Text = "Lock Options" ,
2016-05-25 05:32:39 +03:00
Detail = GetLockOptionsDetailsText ( ) ,
2016-06-28 02:53:31 +03:00
ShowDisclousure = true
2016-05-18 06:09:20 +03:00
} ;
2016-05-22 05:50:15 +03:00
LockOptionsCell . Tapped + = LockOptionsCell_Tapped ;
2016-05-18 06:09:20 +03:00
var changeMasterPasswordCell = new ExtendedTextCell
{
2016-07-01 05:04:45 +03:00
Text = "Change Master Password" ,
ShowDisclousure = true
2016-05-18 06:09:20 +03:00
} ;
changeMasterPasswordCell . Tapped + = ChangeMasterPasswordCell_Tapped ;
2016-07-07 07:00:12 +03:00
var changeEmailCell = new ExtendedTextCell
{
Text = "Change Email" ,
ShowDisclousure = true
} ;
changeEmailCell . Tapped + = ChangeEmailCell_Tapped ;
2016-05-18 06:09:20 +03:00
var foldersCell = new ExtendedTextCell
{
Text = "Folders" ,
2016-06-28 02:53:31 +03:00
ShowDisclousure = true
2016-05-18 06:09:20 +03:00
} ;
2016-05-08 07:28:14 +03:00
foldersCell . Tapped + = FoldersCell_Tapped ;
2016-07-01 05:04:45 +03:00
var syncCell = new ExtendedTextCell
{
Text = "Sync" ,
ShowDisclousure = true
} ;
syncCell . Tapped + = SyncCell_Tapped ;
2016-05-18 06:09:20 +03:00
var lockCell = new ExtendedTextCell
{
2016-06-28 02:53:31 +03:00
Text = "Lock"
2016-05-18 06:09:20 +03:00
} ;
lockCell . Tapped + = LockCell_Tapped ;
var logOutCell = new ExtendedTextCell
{
2016-06-28 02:53:31 +03:00
Text = "Log Out"
2016-05-18 06:09:20 +03:00
} ;
logOutCell . Tapped + = LogOutCell_Tapped ;
2016-05-13 04:30:02 +03:00
var table = new ExtendedTableView
2016-05-08 07:28:14 +03:00
{
2016-05-18 06:09:20 +03:00
EnableScrolling = true ,
2016-05-12 00:30:09 +03:00
Intent = TableIntent . Menu ,
2016-06-29 06:44:47 +03:00
HasUnevenRows = true ,
2016-05-08 07:28:14 +03:00
Root = new TableRoot
{
2016-05-18 06:09:20 +03:00
new TableSection ( "Security" )
{
2016-05-22 05:50:15 +03:00
LockOptionsCell ,
FingerprintCell ,
PinCell ,
2016-07-07 07:00:12 +03:00
changeMasterPasswordCell ,
changeEmailCell
2016-05-18 06:09:20 +03:00
} ,
2016-07-01 05:04:45 +03:00
new TableSection ( "Manage" )
2016-05-08 07:28:14 +03:00
{
2016-07-01 05:04:45 +03:00
foldersCell ,
syncCell
2016-05-18 06:09:20 +03:00
} ,
new TableSection ( "Current Session" )
{
lockCell ,
logOutCell
2016-05-08 07:28:14 +03:00
}
}
} ;
2016-05-03 00:50:16 +03:00
2016-07-20 01:46:39 +03:00
if ( Device . OS = = TargetPlatform . iOS )
2016-05-03 00:50:16 +03:00
{
2016-06-29 06:44:47 +03:00
table . RowHeight = - 1 ;
table . EstimatedRowHeight = 44 ;
}
2016-05-03 00:50:16 +03:00
2016-05-07 20:42:09 +03:00
Title = AppResources . Settings ;
2016-05-18 06:09:20 +03:00
Content = table ;
2016-05-03 00:50:16 +03:00
}
2016-05-08 07:28:14 +03:00
2016-05-22 05:50:15 +03:00
private async void LockOptionsCell_Tapped ( object sender , EventArgs e )
{
var selection = await DisplayActionSheet ( "Lock Options" , AppResources . Cancel , null ,
2016-05-25 05:32:39 +03:00
"Immediately" , "1 minute" , "15 minutes" , "1 hour" , "4 hours" , "Never" ) ;
if ( selection = = AppResources . Cancel )
{
return ;
}
2016-05-22 05:50:15 +03:00
if ( selection = = "Immediately" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 0 ) ;
}
else if ( selection = = "1 minute" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 60 ) ;
}
2016-05-25 05:32:39 +03:00
else if ( selection = = "5 minutes" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 60 * 5 ) ;
}
else if ( selection = = "15 minutes" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 60 * 15 ) ;
}
else if ( selection = = "1 hour" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 60 * 60 ) ;
}
else if ( selection = = "4 hours" )
{
_settings . AddOrUpdateValue ( Constants . SettingLockSeconds , 60 * 60 * 4 ) ;
}
else if ( selection = = "Never" )
2016-05-22 05:50:15 +03:00
{
_settings . Remove ( Constants . SettingLockSeconds ) ;
}
LockOptionsCell . Detail = selection ;
}
2016-07-01 05:04:45 +03:00
private void SyncCell_Tapped ( object sender , EventArgs e )
{
2016-07-02 02:21:12 +03:00
Navigation . PushAsync ( new SettingsSyncPage ( ) ) ;
2016-07-01 05:04:45 +03:00
}
2016-05-18 06:09:20 +03:00
private void LockCell_Tapped ( object sender , EventArgs e )
2016-05-08 07:28:14 +03:00
{
2016-07-19 02:44:29 +03:00
_settings . AddOrUpdateValue ( Constants . SettingLocked , true ) ;
2016-05-22 06:26:35 +03:00
MessagingCenter . Send ( Application . Current , "Lock" , true ) ;
2016-05-08 07:28:14 +03:00
}
2016-05-18 06:09:20 +03:00
private async void LogOutCell_Tapped ( object sender , EventArgs e )
{
if ( ! await _userDialogs . ConfirmAsync ( "Are you sure you want to log out?" , null , AppResources . Yes , AppResources . Cancel ) )
{
return ;
}
2016-07-20 01:46:39 +03:00
MessagingCenter . Send ( Application . Current , "Logout" , ( string ) null ) ;
2016-05-18 06:09:20 +03:00
}
2016-05-22 05:50:15 +03:00
private async void ChangeMasterPasswordCell_Tapped ( object sender , EventArgs e )
2016-05-18 06:09:20 +03:00
{
2016-05-22 05:50:15 +03:00
if ( ! await _userDialogs . ConfirmAsync ( "You can change your master password on the bitwarden.com web vault. Do you want to visit the website now?" , null , AppResources . Yes , AppResources . Cancel ) )
{
return ;
}
2016-05-18 06:09:20 +03:00
2016-05-22 05:50:15 +03:00
Device . OpenUri ( new Uri ( "https://vault.bitwarden.com" ) ) ;
2016-05-18 06:09:20 +03:00
}
2016-07-07 07:00:12 +03:00
private async void ChangeEmailCell_Tapped ( object sender , EventArgs e )
{
if ( ! await _userDialogs . ConfirmAsync ( "You can change your email address on the bitwarden.com web vault. Do you want to visit the website now?" , null , AppResources . Yes , AppResources . Cancel ) )
{
return ;
}
Device . OpenUri ( new Uri ( "https://vault.bitwarden.com" ) ) ;
}
2016-05-22 05:50:15 +03:00
private void FingerprintCell_Changed ( object sender , EventArgs e )
2016-05-18 06:09:20 +03:00
{
2016-05-22 05:50:15 +03:00
var cell = sender as ExtendedSwitchCell ;
if ( cell = = null )
2016-05-18 06:09:20 +03:00
{
return ;
}
2016-05-22 05:50:15 +03:00
_settings . AddOrUpdateValue ( Constants . SettingFingerprintUnlockOn , cell . On ) ;
if ( cell . On )
{
_settings . AddOrUpdateValue ( Constants . SettingPinUnlockOn , false ) ;
PinCell . On = false ;
}
2016-05-18 06:09:20 +03:00
}
2016-05-22 05:50:15 +03:00
private void PinCell_Changed ( object sender , EventArgs e )
2016-05-18 06:09:20 +03:00
{
2016-05-22 05:50:15 +03:00
var cell = sender as ExtendedSwitchCell ;
if ( cell = = null )
{
return ;
}
if ( cell . On )
{
2016-06-12 07:49:35 +03:00
var pinPage = new SettingsPinPage ( ) ;
pinPage . OnPinEntered + = PinEntered ;
Navigation . PushAsync ( pinPage ) ;
2016-05-22 05:50:15 +03:00
}
2016-06-12 07:49:35 +03:00
else
{
_settings . AddOrUpdateValue ( Constants . SettingPinUnlockOn , false ) ;
}
}
private void PinEntered ( object sender , EventArgs args )
{
var page = sender as SettingsPinPage ;
page . Navigation . PopAsync ( ) ;
_authService . PIN = page . Model . PIN ;
_settings . AddOrUpdateValue ( Constants . SettingPinUnlockOn , true ) ;
_settings . AddOrUpdateValue ( Constants . SettingFingerprintUnlockOn , false ) ;
FingerprintCell . On = false ;
2016-05-18 06:09:20 +03:00
}
private void FoldersCell_Tapped ( object sender , EventArgs e )
{
Navigation . PushAsync ( new SettingsListFoldersPage ( ) ) ;
}
2016-05-25 05:32:39 +03:00
private string GetLockOptionsDetailsText ( )
{
var lockSeconds = _settings . GetValueOrDefault < int? > ( Constants . SettingLockSeconds ) ;
if ( ! lockSeconds . HasValue )
{
return "Never" ;
}
if ( lockSeconds . Value = = 60 )
{
return "1 minute" ;
}
else if ( lockSeconds . Value = = 60 * 5 )
{
return "5 minutes" ;
}
else if ( lockSeconds . Value = = 60 * 15 )
{
return "15 minutes" ;
}
else if ( lockSeconds . Value = = 60 * 60 )
{
return "1 hour" ;
}
else if ( lockSeconds . Value = = 60 * 60 * 4 )
{
return "4 hours" ;
}
else
{
return "Immediately" ;
}
}
2016-05-03 00:50:16 +03:00
}
}