2016-05-27 05:22:38 +03:00
|
|
|
using System;
|
2016-06-05 01:27:34 +03:00
|
|
|
using System.Collections.Generic;
|
2016-06-10 07:36:15 +03:00
|
|
|
using System.Diagnostics;
|
2016-06-05 01:27:34 +03:00
|
|
|
using System.Linq;
|
2016-06-25 08:58:42 +03:00
|
|
|
using Bit.App.Abstractions;
|
2016-06-05 00:04:49 +03:00
|
|
|
using Bit.iOS.Extension.Models;
|
2016-05-07 08:54:44 +03:00
|
|
|
using Foundation;
|
|
|
|
using UIKit;
|
2016-06-25 08:58:42 +03:00
|
|
|
using XLabs.Ioc;
|
2016-07-22 06:41:50 +03:00
|
|
|
using Plugin.Settings.Abstractions;
|
2016-07-29 02:12:51 +03:00
|
|
|
using Bit.iOS.Core.Utilities;
|
2016-08-03 01:34:57 +03:00
|
|
|
using System.Threading.Tasks;
|
2016-08-03 02:56:51 +03:00
|
|
|
using Bit.iOS.Core;
|
|
|
|
using MobileCoreServices;
|
2016-08-04 07:06:09 +03:00
|
|
|
using Bit.iOS.Core.Controllers;
|
2016-11-26 01:05:14 +03:00
|
|
|
using Bit.App.Resources;
|
2016-05-07 08:54:44 +03:00
|
|
|
|
|
|
|
namespace Bit.iOS.Extension
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
public partial class LoginListViewController : ExtendedUITableViewController
|
2016-05-07 08:54:44 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
public LoginListViewController(IntPtr handle) : base(handle)
|
2016-06-25 08:58:42 +03:00
|
|
|
{ }
|
2016-05-07 08:54:44 +03:00
|
|
|
|
2016-06-05 00:04:49 +03:00
|
|
|
public Context Context { get; set; }
|
2016-07-26 04:32:15 +03:00
|
|
|
public LoadingViewController LoadingController { get; set; }
|
2016-05-07 08:54:44 +03:00
|
|
|
|
2016-06-14 07:19:18 +03:00
|
|
|
public override void ViewWillAppear(bool animated)
|
|
|
|
{
|
|
|
|
UINavigationBar.Appearance.ShadowImage = new UIImage();
|
|
|
|
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
|
|
|
|
base.ViewWillAppear(animated);
|
|
|
|
}
|
|
|
|
|
2016-06-25 08:58:42 +03:00
|
|
|
public async override void ViewDidLoad()
|
2016-06-03 03:35:54 +03:00
|
|
|
{
|
2016-06-03 06:28:17 +03:00
|
|
|
base.ViewDidLoad();
|
2017-01-03 08:17:15 +03:00
|
|
|
NavItem.Title = AppResources.Logins;
|
2016-08-03 02:56:51 +03:00
|
|
|
if(!CanAutoFill())
|
2016-07-30 08:11:52 +03:00
|
|
|
{
|
2016-11-26 01:05:14 +03:00
|
|
|
CancelBarButton.Title = AppResources.Close;
|
2016-07-30 08:11:52 +03:00
|
|
|
}
|
2016-12-27 05:22:55 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CancelBarButton.Title = AppResources.Cancel;
|
|
|
|
}
|
2016-07-30 08:11:52 +03:00
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
TableView.RowHeight = UITableView.AutomaticDimension;
|
|
|
|
TableView.EstimatedRowHeight = 44;
|
2016-08-03 01:34:57 +03:00
|
|
|
TableView.Source = new TableSource(this);
|
|
|
|
await ((TableSource)TableView.Source).LoadItemsAsync();
|
2016-05-27 05:22:38 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 02:56:51 +03:00
|
|
|
public bool CanAutoFill()
|
|
|
|
{
|
|
|
|
if(Context.ProviderType != Constants.UTTypeAppExtensionFillBrowserAction
|
|
|
|
&& Context.ProviderType != Constants.UTTypeAppExtensionFillWebViewAction
|
|
|
|
&& Context.ProviderType != UTType.PropertyList)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Context.Details?.HasPasswordField ?? false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
partial void CancelBarButton_Activated(UIBarButtonItem sender)
|
2016-06-05 00:04:49 +03:00
|
|
|
{
|
2016-07-26 04:32:15 +03:00
|
|
|
LoadingController.CompleteRequest(null);
|
2016-06-05 01:27:34 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
partial void AddBarButton_Activated(UIBarButtonItem sender)
|
2016-07-08 03:53:01 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
PerformSegue("loginAddSegue", this);
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
|
|
|
|
{
|
|
|
|
var navController = segue.DestinationViewController as UINavigationController;
|
|
|
|
if(navController != null)
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
var addLoginController = navController.TopViewController as LoginAddViewController;
|
|
|
|
if(addLoginController != null)
|
2016-07-08 03:53:01 +03:00
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
addLoginController.Context = Context;
|
|
|
|
addLoginController.LoginListController = this;
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 22:23:54 +03:00
|
|
|
public void DismissModal()
|
|
|
|
{
|
2016-08-03 01:34:57 +03:00
|
|
|
DismissViewController(true, async () =>
|
2016-07-22 06:41:50 +03:00
|
|
|
{
|
2016-08-03 01:34:57 +03:00
|
|
|
await ((TableSource)TableView.Source).LoadItemsAsync();
|
2016-07-22 06:41:50 +03:00
|
|
|
TableView.ReloadData();
|
|
|
|
});
|
2016-07-09 22:23:54 +03:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:27:34 +03:00
|
|
|
public class TableSource : UITableViewSource
|
|
|
|
{
|
|
|
|
private const string CellIdentifier = "TableCell";
|
|
|
|
|
2017-05-11 07:34:54 +03:00
|
|
|
private IEnumerable<LoginViewModel> _tableItems = new List<LoginViewModel>();
|
2016-06-05 01:27:34 +03:00
|
|
|
private Context _context;
|
2017-01-03 08:17:15 +03:00
|
|
|
private LoginListViewController _controller;
|
2016-06-05 01:27:34 +03:00
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
public TableSource(LoginListViewController controller)
|
2016-06-02 07:18:47 +03:00
|
|
|
{
|
2016-06-05 01:27:34 +03:00
|
|
|
_context = controller.Context;
|
|
|
|
_controller = controller;
|
2016-06-02 07:18:47 +03:00
|
|
|
}
|
2016-06-05 01:27:34 +03:00
|
|
|
|
2016-08-03 01:34:57 +03:00
|
|
|
public async Task LoadItemsAsync()
|
|
|
|
{
|
2017-02-09 08:12:09 +03:00
|
|
|
var loginService = Resolver.Resolve<ILoginService>();
|
|
|
|
var logins = await loginService.GetAllAsync(_context.UrlString);
|
2017-02-14 03:12:02 +03:00
|
|
|
_tableItems = logins?.Item1.Select(s => new LoginViewModel(s))
|
2017-02-09 08:12:09 +03:00
|
|
|
.OrderBy(s => s.Name)
|
|
|
|
.ThenBy(s => s.Username)
|
2017-02-14 03:12:02 +03:00
|
|
|
.ToList() ?? new List<LoginViewModel>();
|
2016-08-03 01:34:57 +03:00
|
|
|
}
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
public IEnumerable<LoginViewModel> TableItems { get; set; }
|
2016-08-03 01:34:57 +03:00
|
|
|
|
2016-06-05 01:27:34 +03:00
|
|
|
public override nint RowsInSection(UITableView tableview, nint section)
|
2016-05-31 05:51:53 +03:00
|
|
|
{
|
2016-07-22 06:41:50 +03:00
|
|
|
return _tableItems.Count() == 0 ? 1 : _tableItems.Count();
|
2016-05-31 05:51:53 +03:00
|
|
|
}
|
2016-06-05 01:27:34 +03:00
|
|
|
|
|
|
|
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
|
2016-05-31 05:51:53 +03:00
|
|
|
{
|
2016-07-22 06:41:50 +03:00
|
|
|
if(_tableItems.Count() == 0)
|
|
|
|
{
|
|
|
|
var noDataCell = new UITableViewCell(UITableViewCellStyle.Default, "NoDataCell");
|
2017-01-03 08:17:15 +03:00
|
|
|
noDataCell.TextLabel.Text = AppResources.NoLoginsTap;
|
2016-07-22 06:41:50 +03:00
|
|
|
noDataCell.TextLabel.TextAlignment = UITextAlignment.Center;
|
|
|
|
noDataCell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
|
|
|
|
noDataCell.TextLabel.Lines = 0;
|
|
|
|
return noDataCell;
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:27:34 +03:00
|
|
|
var cell = tableView.DequeueReusableCell(CellIdentifier);
|
2016-05-30 10:08:12 +03:00
|
|
|
|
2016-06-05 01:27:34 +03:00
|
|
|
// if there are no cells to reuse, create a new one
|
|
|
|
if(cell == null)
|
|
|
|
{
|
2016-07-22 06:41:50 +03:00
|
|
|
Debug.WriteLine("BW Log, Make new cell for list.");
|
2016-06-11 08:17:08 +03:00
|
|
|
cell = new UITableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier);
|
2016-07-27 02:21:57 +03:00
|
|
|
cell.DetailTextLabel.TextColor = cell.DetailTextLabel.TintColor = new UIColor(red: 0.47f, green: 0.47f, blue: 0.47f, alpha: 1.0f);
|
2016-06-05 01:27:34 +03:00
|
|
|
}
|
2016-07-15 02:01:34 +03:00
|
|
|
return cell;
|
|
|
|
}
|
2016-05-30 10:08:12 +03:00
|
|
|
|
2016-07-20 06:04:37 +03:00
|
|
|
public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
|
2016-07-15 02:01:34 +03:00
|
|
|
{
|
2016-07-22 06:41:50 +03:00
|
|
|
if(_tableItems.Count() == 0 || cell == null)
|
2016-07-15 02:01:34 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-20 06:04:37 +03:00
|
|
|
var item = _tableItems.ElementAt(indexPath.Row);
|
2016-06-25 08:58:42 +03:00
|
|
|
cell.TextLabel.Text = item.Name;
|
|
|
|
cell.DetailTextLabel.Text = item.Username;
|
2016-06-05 01:27:34 +03:00
|
|
|
}
|
2016-06-02 07:18:47 +03:00
|
|
|
|
2016-06-05 01:27:34 +03:00
|
|
|
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
|
|
|
|
{
|
2016-07-29 02:12:51 +03:00
|
|
|
tableView.DeselectRow(indexPath, true);
|
|
|
|
tableView.EndEditing(true);
|
|
|
|
|
2016-07-22 06:41:50 +03:00
|
|
|
if(_tableItems.Count() == 0)
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
_controller.PerformSegue("loginAddSegue", this);
|
2016-07-22 06:41:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-25 09:54:34 +03:00
|
|
|
var item = _tableItems.ElementAt(indexPath.Row);
|
|
|
|
if(item == null)
|
|
|
|
{
|
2016-07-26 04:32:15 +03:00
|
|
|
_controller.LoadingController.CompleteRequest(null);
|
2016-07-22 06:41:50 +03:00
|
|
|
return;
|
2016-06-25 09:54:34 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 02:56:51 +03:00
|
|
|
if(_controller.CanAutoFill() && !string.IsNullOrWhiteSpace(item.Password))
|
2016-07-29 02:12:51 +03:00
|
|
|
{
|
|
|
|
_controller.LoadingController.CompleteUsernamePasswordRequest(item.Username, item.Password);
|
|
|
|
}
|
|
|
|
else if(!string.IsNullOrWhiteSpace(item.Username) || !string.IsNullOrWhiteSpace(item.Password))
|
|
|
|
{
|
|
|
|
var sheet = Dialogs.CreateActionSheet(item.Name, _controller);
|
|
|
|
if(!string.IsNullOrWhiteSpace(item.Username))
|
|
|
|
{
|
2016-11-26 01:05:14 +03:00
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.CopyUsername, UIAlertActionStyle.Default, a =>
|
2016-07-29 02:12:51 +03:00
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Username;
|
2016-11-26 01:05:14 +03:00
|
|
|
var alert = Dialogs.CreateMessageAlert(AppResources.CopyUsername);
|
2016-07-29 02:12:51 +03:00
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(item.Password))
|
|
|
|
{
|
2016-11-26 01:05:14 +03:00
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.CopyPassword, UIAlertActionStyle.Default, a =>
|
2016-07-29 02:12:51 +03:00
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Password;
|
2016-11-26 01:05:14 +03:00
|
|
|
var alert = Dialogs.CreateMessageAlert(AppResources.CopiedPassword);
|
2016-07-29 02:12:51 +03:00
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-11-26 01:05:14 +03:00
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.Cancel, UIAlertActionStyle.Cancel, null));
|
2016-07-29 02:12:51 +03:00
|
|
|
_controller.PresentViewController(sheet, true, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-26 01:05:14 +03:00
|
|
|
var alert = Dialogs.CreateAlert(null, AppResources.NoUsernamePasswordConfigured, AppResources.Ok);
|
2016-07-29 02:12:51 +03:00
|
|
|
_controller.PresentViewController(alert, true, null);
|
|
|
|
}
|
2016-06-05 01:27:34 +03:00
|
|
|
}
|
2016-06-02 07:18:47 +03:00
|
|
|
}
|
2016-05-07 08:54:44 +03:00
|
|
|
}
|
2016-06-05 00:04:49 +03:00
|
|
|
}
|