bitwarden-android/src/iOS.Extension/LoginListViewController.cs

291 lines
11 KiB
C#
Raw Normal View History

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;
using Bit.App.Abstractions;
using Bit.iOS.Extension.Models;
using Foundation;
using UIKit;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
using Bit.iOS.Core.Utilities;
using System.Threading.Tasks;
2016-08-03 02:56:51 +03:00
using Bit.iOS.Core;
using MobileCoreServices;
using Bit.iOS.Core.Controllers;
using Bit.App.Resources;
using Bit.App.Models;
namespace Bit.iOS.Extension
{
2017-01-03 08:17:15 +03:00
public partial class LoginListViewController : ExtendedUITableViewController
{
2017-01-03 08:17:15 +03:00
public LoginListViewController(IntPtr handle) : base(handle)
{ }
public Context Context { get; set; }
public LoadingViewController LoadingController { get; set; }
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);
}
public async override void ViewDidLoad()
{
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())
{
CancelBarButton.Title = AppResources.Close;
}
else
{
CancelBarButton.Title = AppResources.Cancel;
}
TableView.RowHeight = UITableView.AutomaticDimension;
TableView.EstimatedRowHeight = 44;
TableView.Source = new TableSource(this);
await ((TableSource)TableView.Source).LoadItemsAsync();
}
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;
}
partial void CancelBarButton_Activated(UIBarButtonItem sender)
{
LoadingController.CompleteRequest(null);
2016-06-05 01:27:34 +03:00
}
partial void AddBarButton_Activated(UIBarButtonItem sender)
{
2017-01-03 08:17:15 +03:00
PerformSegue("loginAddSegue", this);
}
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)
{
2017-01-03 08:17:15 +03:00
addLoginController.Context = Context;
addLoginController.LoginListController = this;
}
}
}
public void DismissModal()
{
DismissViewController(true, async () =>
{
await ((TableSource)TableView.Source).LoadItemsAsync();
TableView.ReloadData();
});
}
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;
2017-07-21 18:39:22 +03:00
private ILoginService _loginService;
private ISettings _settings;
private bool _isPremium;
2016-06-05 01:27:34 +03:00
2017-01-03 08:17:15 +03:00
public TableSource(LoginListViewController controller)
{
2016-06-05 01:27:34 +03:00
_context = controller.Context;
_controller = controller;
2017-07-21 18:39:22 +03:00
_isPremium = Resolver.Resolve<ITokenService>()?.TokenPremium ?? false;
_loginService = Resolver.Resolve<ILoginService>();
_settings = Resolver.Resolve<ISettings>();
}
2016-06-05 01:27:34 +03:00
public async Task LoadItemsAsync()
{
var combinedLogins = new List<Login>();
2017-07-21 18:39:22 +03:00
var logins = await _loginService.GetAllAsync(_context.UrlString);
if(logins?.Item1 != null)
{
combinedLogins.AddRange(logins.Item1);
}
if(logins?.Item2 != null)
{
combinedLogins.AddRange(logins.Item2);
}
_tableItems = combinedLogins.Select(s => new LoginViewModel(s))
.OrderBy(s => s.Name)
.ThenBy(s => s.Username)
.ToList() ?? new List<LoginViewModel>();
}
2017-01-03 08:17:15 +03:00
public IEnumerable<LoginViewModel> TableItems { get; set; }
2016-06-05 01:27:34 +03:00
public override nint RowsInSection(UITableView tableview, nint section)
{
2017-05-19 18:20:47 +03:00
return _tableItems == null || _tableItems.Count() == 0 ? 1 : _tableItems.Count();
}
2016-06-05 01:27:34 +03:00
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
2017-05-19 18:20:47 +03:00
if(_tableItems == null || _tableItems.Count() == 0)
{
var noDataCell = new UITableViewCell(UITableViewCellStyle.Default, "NoDataCell");
2017-01-03 08:17:15 +03:00
noDataCell.TextLabel.Text = AppResources.NoLoginsTap;
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-06-05 01:27:34 +03:00
// if there are no cells to reuse, create a new one
if(cell == null)
{
Debug.WriteLine("BW Log, Make new cell for list.");
2016-06-11 08:17:08 +03:00
cell = new UITableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier);
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
}
return cell;
}
public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
2017-05-19 18:20:47 +03:00
if(_tableItems == null || _tableItems.Count() == 0 || cell == null)
{
return;
}
var item = _tableItems.ElementAt(indexPath.Row);
cell.TextLabel.Text = item.Name;
cell.DetailTextLabel.Text = item.Username;
2016-06-05 01:27:34 +03:00
}
2016-06-05 01:27:34 +03:00
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
tableView.EndEditing(true);
2017-05-19 18:20:47 +03:00
if(_tableItems == null || _tableItems.Count() == 0)
{
2017-01-03 08:17:15 +03:00
_controller.PerformSegue("loginAddSegue", this);
return;
}
var item = _tableItems.ElementAt(indexPath.Row);
if(item == null)
{
_controller.LoadingController.CompleteRequest(null);
return;
}
2016-08-03 02:56:51 +03:00
if(_controller.CanAutoFill() && !string.IsNullOrWhiteSpace(item.Password))
{
2017-07-21 18:39:22 +03:00
string totp = null;
if(!_settings.GetValueOrDefault(App.Constants.SettingDisableTotpCopy, false))
{
totp = GetTotp(item);
}
_controller.LoadingController.CompleteUsernamePasswordRequest(item.Username, item.Password,
item.Fields.Value, totp);
}
2017-07-21 18:39:22 +03:00
else if(!string.IsNullOrWhiteSpace(item.Username) || !string.IsNullOrWhiteSpace(item.Password) ||
!string.IsNullOrWhiteSpace(item.Totp.Value))
{
var sheet = Dialogs.CreateActionSheet(item.Name, _controller);
if(!string.IsNullOrWhiteSpace(item.Username))
{
sheet.AddAction(UIAlertAction.Create(AppResources.CopyUsername, UIAlertActionStyle.Default, a =>
{
UIPasteboard clipboard = UIPasteboard.General;
clipboard.String = item.Username;
var alert = Dialogs.CreateMessageAlert(AppResources.CopyUsername);
_controller.PresentViewController(alert, true, () =>
{
_controller.DismissViewController(true, null);
});
}));
}
if(!string.IsNullOrWhiteSpace(item.Password))
{
sheet.AddAction(UIAlertAction.Create(AppResources.CopyPassword, UIAlertActionStyle.Default, a =>
{
UIPasteboard clipboard = UIPasteboard.General;
clipboard.String = item.Password;
var alert = Dialogs.CreateMessageAlert(AppResources.CopiedPassword);
_controller.PresentViewController(alert, true, () =>
{
_controller.DismissViewController(true, null);
});
}));
}
2017-07-21 18:39:22 +03:00
if(!string.IsNullOrWhiteSpace(item.Totp.Value))
{
sheet.AddAction(UIAlertAction.Create(AppResources.CopyTotp, UIAlertActionStyle.Default, a =>
{
var totp = GetTotp(item);
if(string.IsNullOrWhiteSpace(totp))
{
return;
}
UIPasteboard clipboard = UIPasteboard.General;
clipboard.String = totp;
var alert = Dialogs.CreateMessageAlert(AppResources.CopiedTotp);
_controller.PresentViewController(alert, true, () =>
{
_controller.DismissViewController(true, null);
});
}));
}
sheet.AddAction(UIAlertAction.Create(AppResources.Cancel, UIAlertActionStyle.Cancel, null));
_controller.PresentViewController(sheet, true, null);
}
else
{
var alert = Dialogs.CreateAlert(null, AppResources.NoUsernamePasswordConfigured, AppResources.Ok);
_controller.PresentViewController(alert, true, null);
}
2016-06-05 01:27:34 +03:00
}
2017-07-21 18:39:22 +03:00
private string GetTotp(LoginViewModel item)
{
string totp = null;
if(_isPremium)
{
if(item != null && !string.IsNullOrWhiteSpace(item.Totp.Value))
{
totp = App.Utilities.Crypto.Totp(item.Totp.Value);
}
}
return totp;
}
}
}
}