2016-07-08 03:53:01 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
using Bit.App.Models;
|
2016-07-09 08:07:02 +03:00
|
|
|
using Bit.App.Resources;
|
|
|
|
using Bit.iOS.Core.Views;
|
2016-07-08 03:53:01 +03:00
|
|
|
using Bit.iOS.Extension.Models;
|
|
|
|
using Foundation;
|
|
|
|
using UIKit;
|
2016-07-09 20:11:18 +03:00
|
|
|
using XLabs.Ioc;
|
|
|
|
using Bit.App;
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-07-09 22:23:54 +03:00
|
|
|
using Bit.iOS.Core.Utilities;
|
2016-08-04 07:06:09 +03:00
|
|
|
using Bit.iOS.Core.Controllers;
|
2016-07-08 03:53:01 +03:00
|
|
|
|
|
|
|
namespace Bit.iOS.Extension
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
public partial class LoginAddViewController : ExtendedUITableViewController
|
2016-07-08 03:53:01 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
private ILoginService _loginService;
|
2016-07-13 06:55:52 +03:00
|
|
|
private IFolderService _folderService;
|
2016-07-09 22:23:54 +03:00
|
|
|
private IConnectivity _connectivity;
|
2016-07-13 06:55:52 +03:00
|
|
|
private IEnumerable<Folder> _folders;
|
2016-08-04 07:25:10 +03:00
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
2016-07-09 22:23:54 +03:00
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
public LoginAddViewController(IntPtr handle) : base(handle)
|
2016-07-08 03:53:01 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
public Context Context { get; set; }
|
2017-01-03 08:17:15 +03:00
|
|
|
public LoginListViewController LoginListController { get; set; }
|
2016-07-26 04:32:15 +03:00
|
|
|
public LoadingViewController LoadingController { get; set; }
|
2016-07-09 08:07:02 +03:00
|
|
|
public FormEntryTableViewCell NameCell { get; set; } = new FormEntryTableViewCell(AppResources.Name);
|
|
|
|
public FormEntryTableViewCell UriCell { get; set; } = new FormEntryTableViewCell(AppResources.URI);
|
|
|
|
public FormEntryTableViewCell UsernameCell { get; set; } = new FormEntryTableViewCell(AppResources.Username);
|
|
|
|
public FormEntryTableViewCell PasswordCell { get; set; } = new FormEntryTableViewCell(AppResources.Password);
|
|
|
|
public UITableViewCell GeneratePasswordCell { get; set; } = new UITableViewCell(UITableViewCellStyle.Subtitle, "GeneratePasswordCell");
|
2016-11-26 01:05:14 +03:00
|
|
|
public SwitchTableViewCell FavoriteCell { get; set; } = new SwitchTableViewCell(AppResources.Favorite);
|
2016-07-09 08:07:02 +03:00
|
|
|
public FormEntryTableViewCell NotesCell { get; set; } = new FormEntryTableViewCell(useTextView: true, height: 90);
|
2016-07-13 05:38:36 +03:00
|
|
|
public PickerTableViewCell FolderCell { get; set; } = new PickerTableViewCell(AppResources.Folder);
|
2016-07-08 03:53:01 +03:00
|
|
|
|
|
|
|
public override void ViewWillAppear(bool animated)
|
|
|
|
{
|
|
|
|
UINavigationBar.Appearance.ShadowImage = new UIImage();
|
|
|
|
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
|
|
|
|
base.ViewWillAppear(animated);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ViewDidLoad()
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
_loginService = Resolver.Resolve<ILoginService>();
|
2016-07-09 22:23:54 +03:00
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-07-13 06:55:52 +03:00
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
2016-08-04 07:25:10 +03:00
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-07-09 22:23:54 +03:00
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
NavItem.Title = AppResources.AddLogin;
|
2016-12-27 05:22:55 +03:00
|
|
|
CancelBarButton.Title = AppResources.Cancel;
|
|
|
|
SaveBarButton.Title = AppResources.Save;
|
2016-07-09 08:07:02 +03:00
|
|
|
View.BackgroundColor = new UIColor(red: 0.94f, green: 0.94f, blue: 0.96f, alpha: 1.0f);
|
|
|
|
|
2017-02-09 08:12:09 +03:00
|
|
|
NameCell.TextField.Text = Context?.Uri?.Host ?? string.Empty;
|
2016-07-09 08:07:02 +03:00
|
|
|
NameCell.TextField.ReturnKeyType = UIReturnKeyType.Next;
|
|
|
|
NameCell.TextField.ShouldReturn += (UITextField tf) =>
|
|
|
|
{
|
|
|
|
UriCell.TextField.BecomeFirstResponder();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-02-09 08:12:09 +03:00
|
|
|
UriCell.TextField.Text = Context?.UrlString ?? string.Empty;
|
2016-07-09 08:07:02 +03:00
|
|
|
UriCell.TextField.KeyboardType = UIKeyboardType.Url;
|
|
|
|
UriCell.TextField.ReturnKeyType = UIReturnKeyType.Next;
|
|
|
|
UriCell.TextField.ShouldReturn += (UITextField tf) =>
|
|
|
|
{
|
|
|
|
UsernameCell.TextField.BecomeFirstResponder();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
UsernameCell.TextField.AutocapitalizationType = UITextAutocapitalizationType.None;
|
|
|
|
UsernameCell.TextField.AutocorrectionType = UITextAutocorrectionType.No;
|
|
|
|
UsernameCell.TextField.SpellCheckingType = UITextSpellCheckingType.No;
|
|
|
|
UsernameCell.TextField.ReturnKeyType = UIReturnKeyType.Next;
|
|
|
|
UsernameCell.TextField.ShouldReturn += (UITextField tf) =>
|
|
|
|
{
|
|
|
|
PasswordCell.TextField.BecomeFirstResponder();
|
|
|
|
return true;
|
|
|
|
};
|
2016-07-08 07:35:48 +03:00
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
PasswordCell.TextField.SecureTextEntry = true;
|
|
|
|
PasswordCell.TextField.ReturnKeyType = UIReturnKeyType.Next;
|
|
|
|
PasswordCell.TextField.ShouldReturn += (UITextField tf) =>
|
|
|
|
{
|
|
|
|
NotesCell.TextView.BecomeFirstResponder();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-11-26 01:05:14 +03:00
|
|
|
GeneratePasswordCell.TextLabel.Text = AppResources.GeneratePassword;
|
2016-07-09 08:07:02 +03:00
|
|
|
GeneratePasswordCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
|
2016-07-08 07:35:48 +03:00
|
|
|
|
2016-07-13 06:55:52 +03:00
|
|
|
_folders = _folderService.GetAllAsync().GetAwaiter().GetResult();
|
|
|
|
var folderNames = _folders.Select(s => s.Name.Decrypt()).OrderBy(s => s).ToList();
|
|
|
|
folderNames.Insert(0, AppResources.FolderNone);
|
|
|
|
FolderCell.Items = folderNames;
|
2016-07-13 05:38:36 +03:00
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
TableView.RowHeight = UITableView.AutomaticDimension;
|
|
|
|
TableView.EstimatedRowHeight = 70;
|
|
|
|
TableView.Source = new TableSource(this);
|
2016-07-13 06:55:52 +03:00
|
|
|
TableView.AllowsSelection = true;
|
2016-07-08 03:53:01 +03:00
|
|
|
|
|
|
|
base.ViewDidLoad();
|
|
|
|
}
|
|
|
|
|
2016-07-09 22:23:54 +03:00
|
|
|
public override void ViewDidAppear(bool animated)
|
|
|
|
{
|
|
|
|
base.ViewDidAppear(animated);
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
{
|
|
|
|
AlertNoConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
partial void CancelBarButton_Activated(UIBarButtonItem sender)
|
2016-07-08 03:53:01 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
if(LoginListController != null)
|
2016-07-26 04:32:15 +03:00
|
|
|
{
|
|
|
|
DismissViewController(true, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LoadingController.CompleteRequest(null);
|
|
|
|
}
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 20:11:18 +03:00
|
|
|
async partial void SaveBarButton_Activated(UIBarButtonItem sender)
|
2016-07-08 03:53:01 +03:00
|
|
|
{
|
2016-07-09 22:23:54 +03:00
|
|
|
if(!_connectivity.IsConnected)
|
2016-07-09 20:11:18 +03:00
|
|
|
{
|
|
|
|
AlertNoConnection();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.TextField.Text))
|
|
|
|
{
|
|
|
|
DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(NameCell.TextField.Text))
|
|
|
|
{
|
|
|
|
DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
var login = new Login
|
2016-07-09 20:11:18 +03:00
|
|
|
{
|
2016-07-09 22:23:54 +03:00
|
|
|
Uri = string.IsNullOrWhiteSpace(UriCell.TextField.Text) ? null : UriCell.TextField.Text.Encrypt(),
|
|
|
|
Name = string.IsNullOrWhiteSpace(NameCell.TextField.Text) ? null : NameCell.TextField.Text.Encrypt(),
|
|
|
|
Username = string.IsNullOrWhiteSpace(UsernameCell.TextField.Text) ? null : UsernameCell.TextField.Text.Encrypt(),
|
|
|
|
Password = string.IsNullOrWhiteSpace(PasswordCell.TextField.Text) ? null : PasswordCell.TextField.Text.Encrypt(),
|
|
|
|
Notes = string.IsNullOrWhiteSpace(NotesCell.TextView.Text) ? null : NotesCell.TextView.Text.Encrypt(),
|
2016-07-13 06:55:52 +03:00
|
|
|
Favorite = FavoriteCell.Switch.On,
|
|
|
|
FolderId = FolderCell.SelectedIndex == 0 ? null : _folders.ElementAtOrDefault(FolderCell.SelectedIndex - 1)?.Id
|
2016-07-09 20:11:18 +03:00
|
|
|
};
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
var saveTask = _loginService.SaveAsync(login);
|
2016-11-26 01:05:14 +03:00
|
|
|
var loadingAlert = Dialogs.CreateLoadingAlert(AppResources.Saving);
|
2016-07-09 22:23:54 +03:00
|
|
|
PresentViewController(loadingAlert, true, null);
|
2016-07-09 20:11:18 +03:00
|
|
|
await saveTask;
|
2016-07-26 04:32:15 +03:00
|
|
|
|
2016-07-31 01:16:09 +03:00
|
|
|
if(saveTask.Result.Succeeded)
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
_googleAnalyticsService.TrackExtensionEvent("CreatedLogin");
|
|
|
|
if(LoginListController != null)
|
2016-07-31 01:16:09 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
LoginListController.DismissModal();
|
2016-07-31 01:16:09 +03:00
|
|
|
}
|
|
|
|
else if(LoadingController != null)
|
|
|
|
{
|
|
|
|
LoadingController.CompleteUsernamePasswordRequest(UsernameCell.TextField.Text, PasswordCell.TextField.Text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(saveTask.Result.Errors.Count() > 0)
|
2016-07-26 04:32:15 +03:00
|
|
|
{
|
2016-07-31 01:16:09 +03:00
|
|
|
DisplayAlert(AppResources.AnErrorHasOccurred, saveTask.Result.Errors.First().Message, AppResources.Ok);
|
2016-07-26 04:32:15 +03:00
|
|
|
}
|
2016-07-31 01:16:09 +03:00
|
|
|
else
|
2016-07-26 04:32:15 +03:00
|
|
|
{
|
2016-07-31 01:16:09 +03:00
|
|
|
DisplayAlert(null, AppResources.AnErrorHasOccurred, AppResources.Ok);
|
2016-07-26 04:32:15 +03:00
|
|
|
}
|
2016-07-09 20:11:18 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 07:56:55 +03:00
|
|
|
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
|
|
|
|
{
|
|
|
|
var navController = segue.DestinationViewController as UINavigationController;
|
|
|
|
if(navController != null)
|
|
|
|
{
|
|
|
|
var passwordGeneratorController = navController.TopViewController as PasswordGeneratorViewController;
|
|
|
|
if(passwordGeneratorController != null)
|
|
|
|
{
|
|
|
|
passwordGeneratorController.Context = Context;
|
|
|
|
passwordGeneratorController.Parent = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 20:11:18 +03:00
|
|
|
public void DisplayAlert(string title, string message, string accept)
|
|
|
|
{
|
2016-07-09 22:23:54 +03:00
|
|
|
var alert = Dialogs.CreateAlert(title, message, accept);
|
2016-07-09 20:11:18 +03:00
|
|
|
PresentViewController(alert, true, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AlertNoConnection()
|
|
|
|
{
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
2016-07-08 07:06:10 +03:00
|
|
|
|
|
|
|
public class TableSource : UITableViewSource
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
private LoginAddViewController _controller;
|
2016-07-08 07:06:10 +03:00
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
public TableSource(LoginAddViewController controller)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
|
|
|
_controller = controller;
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
2016-07-09 08:07:02 +03:00
|
|
|
if(indexPath.Section == 0)
|
|
|
|
{
|
|
|
|
if(indexPath.Row == 0)
|
|
|
|
{
|
|
|
|
return _controller.NameCell;
|
|
|
|
}
|
|
|
|
else if(indexPath.Row == 1)
|
|
|
|
{
|
|
|
|
return _controller.UriCell;
|
|
|
|
}
|
|
|
|
else if(indexPath.Row == 2)
|
|
|
|
{
|
|
|
|
return _controller.UsernameCell;
|
|
|
|
}
|
|
|
|
else if(indexPath.Row == 3)
|
|
|
|
{
|
|
|
|
return _controller.PasswordCell;
|
|
|
|
}
|
|
|
|
else if(indexPath.Row == 4)
|
|
|
|
{
|
|
|
|
return _controller.GeneratePasswordCell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(indexPath.Section == 1)
|
|
|
|
{
|
2016-07-13 05:38:36 +03:00
|
|
|
if(indexPath.Row == 0)
|
|
|
|
{
|
|
|
|
return _controller.FolderCell;
|
|
|
|
}
|
|
|
|
else if(indexPath.Row == 1)
|
2016-07-09 08:07:02 +03:00
|
|
|
{
|
|
|
|
return _controller.FavoriteCell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(indexPath.Section == 2)
|
|
|
|
{
|
|
|
|
return _controller.NotesCell;
|
2016-07-08 07:06:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new UITableViewCell();
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
|
|
|
return UITableView.AutomaticDimension;
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
public override nint NumberOfSections(UITableView tableView)
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override nint RowsInSection(UITableView tableview, nint section)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
2016-07-09 08:07:02 +03:00
|
|
|
if(section == 0)
|
|
|
|
{
|
2016-07-08 07:06:10 +03:00
|
|
|
return 5;
|
2016-07-09 08:07:02 +03:00
|
|
|
}
|
|
|
|
else if(section == 1)
|
|
|
|
{
|
2016-07-08 07:06:10 +03:00
|
|
|
return 2;
|
2016-07-09 08:07:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-08 07:06:10 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
|
|
|
return UITableView.AutomaticDimension;
|
2016-07-09 08:07:02 +03:00
|
|
|
}
|
2016-07-08 07:06:10 +03:00
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
public override string TitleForHeader(UITableView tableView, nint section)
|
2016-07-08 07:06:10 +03:00
|
|
|
{
|
2016-07-09 08:07:02 +03:00
|
|
|
if(section == 0)
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
return AppResources.LoginInformation;
|
2016-07-09 08:07:02 +03:00
|
|
|
}
|
|
|
|
else if(section == 2)
|
|
|
|
{
|
2016-11-26 01:05:14 +03:00
|
|
|
return AppResources.Notes;
|
2016-07-08 07:06:10 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 08:07:02 +03:00
|
|
|
return null;
|
2016-07-08 07:06:10 +03:00
|
|
|
}
|
2016-07-13 06:55:52 +03:00
|
|
|
|
|
|
|
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
|
|
|
|
{
|
|
|
|
tableView.DeselectRow(indexPath, true);
|
|
|
|
tableView.EndEditing(true);
|
|
|
|
|
|
|
|
if(indexPath.Section == 0 && indexPath.Row == 4)
|
|
|
|
{
|
2016-07-14 07:56:55 +03:00
|
|
|
_controller.PerformSegue("passwordGeneratorSegue", this);
|
2016-07-13 06:55:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cell = tableView.CellAt(indexPath);
|
|
|
|
if(cell == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectableCell = cell as ISelectable;
|
|
|
|
if(selectableCell != null)
|
|
|
|
{
|
|
|
|
selectableCell.Select();
|
|
|
|
}
|
|
|
|
}
|
2016-07-08 07:06:10 +03:00
|
|
|
}
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
|
|
|
}
|