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.Core;
|
|
|
|
using Bit.iOS.Extension.Models;
|
2016-05-07 08:54:44 +03:00
|
|
|
using Foundation;
|
2016-05-27 05:22:38 +03:00
|
|
|
using MobileCoreServices;
|
2016-06-02 07:18:47 +03:00
|
|
|
using Newtonsoft.Json;
|
2016-05-07 08:54:44 +03:00
|
|
|
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 CoreGraphics;
|
|
|
|
using Bit.iOS.Core.Utilities;
|
2016-05-07 08:54:44 +03:00
|
|
|
|
|
|
|
namespace Bit.iOS.Extension
|
|
|
|
{
|
2016-07-09 09:01:23 +03:00
|
|
|
public partial class SiteListViewController : UITableViewController
|
2016-05-07 08:54:44 +03:00
|
|
|
{
|
2016-07-09 09:01:23 +03:00
|
|
|
public SiteListViewController(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();
|
2016-06-05 01:27:34 +03:00
|
|
|
|
2016-07-30 08:11:52 +03:00
|
|
|
if(!Context?.Details?.HasPasswordField ?? false)
|
|
|
|
{
|
|
|
|
CancelBarButton.Title = "Close";
|
|
|
|
}
|
|
|
|
|
2016-07-22 03:59:55 +03:00
|
|
|
Debug.WriteLine("BW LOG, Site list ViewDidLoad.");
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
2016-07-08 07:35:48 +03:00
|
|
|
IEnumerable<SiteViewModel> filteredSiteModels = new List<SiteViewModel>();
|
|
|
|
if(Context.DomainName != null)
|
2016-06-26 07:16:10 +03:00
|
|
|
{
|
|
|
|
var siteService = Resolver.Resolve<ISiteService>();
|
|
|
|
var sites = await siteService.GetAllAsync();
|
|
|
|
var siteModels = sites.Select(s => new SiteViewModel(s));
|
2016-07-27 02:21:57 +03:00
|
|
|
filteredSiteModels = siteModels
|
|
|
|
.Where(s => s.Domain != null && s.Domain.BaseDomain == Context.DomainName.BaseDomain)
|
|
|
|
.ToList();
|
2016-06-26 07:16:10 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 03:59:55 +03:00
|
|
|
Debug.WriteLine("BW LOG, Filtered sites at " + sw.ElapsedMilliseconds + "ms.");
|
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
TableView.RowHeight = UITableView.AutomaticDimension;
|
|
|
|
TableView.EstimatedRowHeight = 44;
|
|
|
|
TableView.Source = new TableSource(filteredSiteModels, this);
|
2016-07-22 03:59:55 +03:00
|
|
|
|
2016-07-26 04:32:15 +03:00
|
|
|
Debug.WriteLine("BW LOG, Set TableView source at " + sw.ElapsedMilliseconds + "ms.");
|
2016-07-22 03:59:55 +03:00
|
|
|
sw.Stop();
|
2016-05-27 05:22:38 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-07-09 09:01:23 +03:00
|
|
|
PerformSegue("siteAddSegue", 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)
|
|
|
|
{
|
2016-07-09 09:01:23 +03:00
|
|
|
var addSiteController = navController.TopViewController as SiteAddViewController;
|
2016-07-08 03:53:01 +03:00
|
|
|
if(addSiteController != null)
|
|
|
|
{
|
|
|
|
addSiteController.Context = Context;
|
2016-07-26 04:32:15 +03:00
|
|
|
addSiteController.SiteListController = this;
|
2016-07-08 03:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 22:23:54 +03:00
|
|
|
public void DismissModal()
|
|
|
|
{
|
2016-07-22 06:41:50 +03:00
|
|
|
DismissViewController(true, () =>
|
|
|
|
{
|
|
|
|
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";
|
|
|
|
|
2016-06-25 08:58:42 +03:00
|
|
|
private IEnumerable<SiteViewModel> _tableItems;
|
2016-06-05 01:27:34 +03:00
|
|
|
private Context _context;
|
2016-07-09 09:01:23 +03:00
|
|
|
private SiteListViewController _controller;
|
2016-06-05 01:27:34 +03:00
|
|
|
|
2016-07-09 09:01:23 +03:00
|
|
|
public TableSource(IEnumerable<SiteViewModel> items, SiteListViewController controller)
|
2016-06-02 07:18:47 +03:00
|
|
|
{
|
2016-06-05 01:27:34 +03:00
|
|
|
_tableItems = items;
|
|
|
|
_context = controller.Context;
|
|
|
|
_controller = controller;
|
2016-06-02 07:18:47 +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");
|
|
|
|
noDataCell.TextLabel.Text = "There are no sites in your vault for this website. Tap to add one.";
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_controller.PerformSegue("siteAddSegue", this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 07:32:57 +03:00
|
|
|
Resolver.Resolve<ISettings>().AddOrUpdateValue(App.Constants.SettingLastBackgroundedDate, DateTime.UtcNow);
|
2016-07-22 06:41:50 +03:00
|
|
|
|
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-07-29 02:12:51 +03:00
|
|
|
if(_context?.Details?.HasPasswordField ?? false && !string.IsNullOrWhiteSpace(item.Password))
|
|
|
|
{
|
|
|
|
_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))
|
|
|
|
{
|
|
|
|
sheet.AddAction(UIAlertAction.Create("Copy Username", UIAlertActionStyle.Default, a =>
|
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Username;
|
|
|
|
var alert = Dialogs.CreateMessageAlert("Copied username!");
|
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(item.Password))
|
|
|
|
{
|
|
|
|
sheet.AddAction(UIAlertAction.Create("Copy Password", UIAlertActionStyle.Default, a =>
|
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Password;
|
|
|
|
var alert = Dialogs.CreateMessageAlert("Copied password!");
|
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
sheet.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
|
|
|
|
_controller.PresentViewController(sheet, true, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var alert = Dialogs.CreateAlert(null, "This site does not have a username or password configured.", "Ok");
|
|
|
|
_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
|
|
|
}
|