From bb4b732b76fe5f20950f977de30d5284104da412 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 28 Jul 2016 19:12:51 -0400 Subject: [PATCH] Added username/password copy options to extension when no password field is detected on the page details intitiated from. --- src/iOS.Core/Utilities/Dialogs.cs | 16 +++++++ src/iOS.Extension/Models/PageDetails.cs | 2 + src/iOS.Extension/SiteListViewController.cs | 49 ++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/iOS.Core/Utilities/Dialogs.cs b/src/iOS.Core/Utilities/Dialogs.cs index 344c94fb6..817013632 100644 --- a/src/iOS.Core/Utilities/Dialogs.cs +++ b/src/iOS.Core/Utilities/Dialogs.cs @@ -35,5 +35,21 @@ namespace Bit.iOS.Core.Utilities alert.AddAction(UIAlertAction.Create(accept, UIAlertActionStyle.Default, acceptHandle)); return alert; } + + public static UIAlertController CreateActionSheet(string title, UIViewController controller) + { + var sheet = UIAlertController.Create(title, null, UIAlertControllerStyle.ActionSheet); + if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) + { + var x = controller.View.Bounds.Width / 2; + var y = controller.View.Bounds.Bottom; + var rect = new CGRect(x, y, 0, 0); + + sheet.PopoverPresentationController.SourceView = controller.View; + sheet.PopoverPresentationController.SourceRect = rect; + sheet.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Unknown; + } + return sheet; + } } } diff --git a/src/iOS.Extension/Models/PageDetails.cs b/src/iOS.Extension/Models/PageDetails.cs index f2fc44e4f..0b60f6c00 100644 --- a/src/iOS.Extension/Models/PageDetails.cs +++ b/src/iOS.Extension/Models/PageDetails.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Bit.iOS.Extension.Models { @@ -13,6 +14,7 @@ namespace Bit.iOS.Extension.Models public Dictionary Forms { get; set; } public List Fields { get; set; } public long CollectedTimestamp { get; set; } + public bool HasPasswordField => Fields.Any(f => f.Type == "password"); public class Form { diff --git a/src/iOS.Extension/SiteListViewController.cs b/src/iOS.Extension/SiteListViewController.cs index b52bc03e6..ec5a6308b 100644 --- a/src/iOS.Extension/SiteListViewController.cs +++ b/src/iOS.Extension/SiteListViewController.cs @@ -11,6 +11,8 @@ using Newtonsoft.Json; using UIKit; using XLabs.Ioc; using Plugin.Settings.Abstractions; +using CoreGraphics; +using Bit.iOS.Core.Utilities; namespace Bit.iOS.Extension { @@ -147,6 +149,9 @@ namespace Bit.iOS.Extension public override void RowSelected(UITableView tableView, NSIndexPath indexPath) { + tableView.DeselectRow(indexPath, true); + tableView.EndEditing(true); + if(_tableItems.Count() == 0) { _controller.PerformSegue("siteAddSegue", this); @@ -162,7 +167,49 @@ namespace Bit.iOS.Extension return; } - _controller.LoadingController.CompleteUsernamePasswordRequest(item.Username, item.Password); + 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); + } } } }