centralized complete request code into loading view controller. added support for save login provider type.

This commit is contained in:
Kyle Spearrin 2016-07-25 21:32:15 -04:00
parent b81eb007ab
commit ca23c9568d
6 changed files with 101 additions and 110 deletions

View File

@ -26,6 +26,8 @@ namespace Bit.iOS.Extension
{
private Context _context = new Context();
private bool _setupHockeyApp = false;
private readonly JsonSerializerSettings _jsonSettings =
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
public LoadingViewController(IntPtr handle) : base(handle)
{
@ -90,7 +92,7 @@ namespace Bit.iOS.Extension
{
var alert = Dialogs.CreateAlert(null, "You must log into the main bitwarden app before you can use the extension.", AppResources.Ok, (a) =>
{
CompleteRequest();
CompleteRequest(null);
});
PresentViewController(alert, true, null);
return;
@ -110,20 +112,18 @@ namespace Bit.iOS.Extension
PerformSegue("lockPasswordSegue", this);
break;
default:
PerformSegue("siteListSegue", this);
if(_context.ProviderType == Constants.UTTypeAppExtensionSaveLoginAction)
{
PerformSegue("newSiteSegue", this);
}
else
{
PerformSegue("siteListSegue", this);
}
break;
}
}
private void CompleteRequest()
{
var resultsProvider = new NSItemProvider(null, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
ExtensionContext.CompleteRequest(returningItems, null);
}
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
var navController = segue.DestinationViewController as UINavigationController;
@ -138,25 +138,27 @@ namespace Bit.iOS.Extension
if(listSiteController != null)
{
listSiteController.Context = _context;
listSiteController.LoadingController = this;
}
else if(addSiteController != null)
{
addSiteController.Context = _context;
addSiteController.LoadingController = this;
}
else if(fingerprintViewController != null)
{
fingerprintViewController.Context = _context;
fingerprintViewController.LoadingViewController = this;
fingerprintViewController.LoadingController = this;
}
else if(pinViewController != null)
{
pinViewController.Context = _context;
pinViewController.LoadingViewController = this;
pinViewController.LoadingController = this;
}
else if(passwordViewController != null)
{
passwordViewController.Context = _context;
passwordViewController.LoadingViewController = this;
passwordViewController.LoadingController = this;
}
}
}
@ -171,6 +173,56 @@ namespace Bit.iOS.Extension
});
}
public void CompleteUsernamePasswordRequest(string username, string password)
{
NSDictionary itemData = null;
if(_context.ProviderType == UTType.PropertyList)
{
var fillScript = new FillScript(_context.Details, username, password);
var scriptJson = JsonConvert.SerializeObject(fillScript, _jsonSettings);
var scriptDict = new NSDictionary(Constants.AppExtensionWebViewPageFillScript, scriptJson);
itemData = new NSDictionary(NSJavaScriptExtension.FinalizeArgumentKey, scriptDict);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionFindLoginAction)
{
itemData = new NSDictionary(
Constants.AppExtensionUsernameKey, username,
Constants.AppExtensionPasswordKey, password);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionFillBrowserAction
|| _context.ProviderType == Constants.UTTypeAppExtensionFillWebViewAction)
{
var fillScript = new FillScript(_context.Details, username, password);
var scriptJson = JsonConvert.SerializeObject(fillScript, _jsonSettings);
itemData = new NSDictionary(Constants.AppExtensionWebViewPageFillScript, scriptJson);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionSaveLoginAction)
{
itemData = new NSDictionary(
Constants.AppExtensionUsernameKey, username,
Constants.AppExtensionPasswordKey, password);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionChangePasswordAction)
{
itemData = new NSDictionary(
Constants.AppExtensionPasswordKey, string.Empty,
Constants.AppExtensionOldPasswordKey, password);
}
CompleteRequest(itemData);
}
public void CompleteRequest(NSDictionary itemData)
{
Debug.WriteLine("BW LOG, itemData: " + itemData);
var resultsProvider = new NSItemProvider(itemData, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
ExtensionContext.CompleteRequest(returningItems, null);
}
private void SetIoc()
{
var container = new UnityContainer();

View File

@ -3,8 +3,6 @@ using Bit.iOS.Extension.Models;
using UIKit;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
using Foundation;
using MobileCoreServices;
using Plugin.Fingerprint.Abstractions;
using System.Threading.Tasks;
using Bit.App;
@ -20,7 +18,7 @@ namespace Bit.iOS.Extension
{ }
public Context Context { get; set; }
public LoadingViewController LoadingViewController { get; set; }
public LoadingViewController LoadingController { get; set; }
public override void ViewWillAppear(bool animated)
{
@ -58,16 +56,7 @@ namespace Bit.iOS.Extension
partial void CancelButton_Activated(UIBarButtonItem sender)
{
CompleteRequest();
}
private void CompleteRequest()
{
var resultsProvider = new NSItemProvider(null, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
Context.ExtContext.CompleteRequest(returningItems, null);
LoadingController.CompleteRequest(null);
}
public async Task CheckFingerprintAsync()
@ -76,7 +65,7 @@ namespace Bit.iOS.Extension
if(result.Authenticated)
{
_settings.AddOrUpdateValue(Constants.SettingLocked, false);
LoadingViewController.DismissLockAndContinue();
LoadingController.DismissLockAndContinue();
}
}
}

View File

@ -4,10 +4,8 @@ using UIKit;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
using Foundation;
using MobileCoreServices;
using Bit.iOS.Core.Views;
using Bit.App.Resources;
using System.Threading.Tasks;
using Bit.iOS.Core.Utilities;
using Bit.App.Abstractions;
using System.Linq;
@ -25,7 +23,7 @@ namespace Bit.iOS.Extension
{ }
public Context Context { get; set; }
public LoadingViewController LoadingViewController { get; set; }
public LoadingViewController LoadingController { get; set; }
public FormEntryTableViewCell MasterPasswordCell { get; set; } = new FormEntryTableViewCell(
AppResources.MasterPassword, useLabelAsPlaceholder: true);
@ -88,7 +86,7 @@ namespace Bit.iOS.Extension
{
_settings.AddOrUpdateValue(Constants.SettingLocked, false);
MasterPasswordCell.TextField.ResignFirstResponder();
LoadingViewController.DismissLockAndContinue();
LoadingController.DismissLockAndContinue();
}
else
{
@ -108,16 +106,7 @@ namespace Bit.iOS.Extension
partial void CancelButton_Activated(UIBarButtonItem sender)
{
CompleteRequest();
}
private void CompleteRequest()
{
var resultsProvider = new NSItemProvider(null, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
Context.ExtContext.CompleteRequest(returningItems, null);
LoadingController.CompleteRequest(null);
}
public class TableSource : UITableViewSource

View File

@ -3,8 +3,6 @@ using Bit.iOS.Extension.Models;
using UIKit;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
using Foundation;
using MobileCoreServices;
using Bit.App.Abstractions;
using Bit.iOS.Core.Utilities;
using Bit.App.Resources;
@ -22,7 +20,7 @@ namespace Bit.iOS.Extension
{ }
public Context Context { get; set; }
public LoadingViewController LoadingViewController { get; set; }
public LoadingViewController LoadingController { get; set; }
public override void ViewWillAppear(bool animated)
{
@ -68,7 +66,7 @@ namespace Bit.iOS.Extension
Debug.WriteLine("BW Log, Start Dismiss PIN controller.");
_settings.AddOrUpdateValue(Constants.SettingLocked, false);
PinTextField.ResignFirstResponder();
LoadingViewController.DismissLockAndContinue();
LoadingController.DismissLockAndContinue();
}
else
{
@ -98,16 +96,7 @@ namespace Bit.iOS.Extension
partial void CancelButton_Activated(UIBarButtonItem sender)
{
CompleteRequest();
}
private void CompleteRequest()
{
var resultsProvider = new NSItemProvider(null, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
Context.ExtContext.CompleteRequest(returningItems, null);
LoadingController.CompleteRequest(null);
}
}
}

View File

@ -27,7 +27,8 @@ namespace Bit.iOS.Extension
{ }
public Context Context { get; set; }
public SiteListViewController Parent { get; set; }
public SiteListViewController SiteListController { get; set; }
public LoadingViewController LoadingController { get; set; }
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);
@ -114,7 +115,14 @@ namespace Bit.iOS.Extension
partial void CancelBarButton_Activated(UIBarButtonItem sender)
{
DismissViewController(true, null);
if(SiteListController != null)
{
DismissViewController(true, null);
}
else
{
LoadingController.CompleteRequest(null);
}
}
async partial void SaveBarButton_Activated(UIBarButtonItem sender)
@ -152,7 +160,15 @@ namespace Bit.iOS.Extension
var loadingAlert = Dialogs.CreateLoadingAlert("Saving...");
PresentViewController(loadingAlert, true, null);
await saveTask;
Parent.DismissModal();
if(SiteListController != null)
{
SiteListController.DismissModal();
}
else if(LoadingController != null)
{
LoadingController.CompleteUsernamePasswordRequest(UsernameCell.TextField.Text, PasswordCell.TextField.Text);
}
}
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)

View File

@ -20,6 +20,7 @@ namespace Bit.iOS.Extension
{ }
public Context Context { get; set; }
public LoadingViewController LoadingController { get; set; }
public override void ViewWillAppear(bool animated)
{
@ -50,22 +51,13 @@ namespace Bit.iOS.Extension
TableView.EstimatedRowHeight = 44;
TableView.Source = new TableSource(filteredSiteModels, this);
Debug.WriteLine("BW LOG, Set TableView srouce at " + sw.ElapsedMilliseconds + "ms.");
Debug.WriteLine("BW LOG, Set TableView source at " + sw.ElapsedMilliseconds + "ms.");
sw.Stop();
}
partial void CancelBarButton_Activated(UIBarButtonItem sender)
{
CompleteRequest(null);
}
private void CompleteRequest(NSDictionary itemData)
{
var resultsProvider = new NSItemProvider(itemData, UTType.PropertyList);
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
var returningItems = new NSExtensionItem[] { resultsItem };
Context.ExtContext.CompleteRequest(returningItems, null);
LoadingController.CompleteRequest(null);
}
partial void AddBarButton_Activated(UIBarButtonItem sender)
@ -82,7 +74,7 @@ namespace Bit.iOS.Extension
if(addSiteController != null)
{
addSiteController.Context = Context;
addSiteController.Parent = this;
addSiteController.SiteListController = this;
}
}
}
@ -159,52 +151,16 @@ namespace Bit.iOS.Extension
return;
}
Resolver.Resolve<ISettings>().AddOrUpdateValue(Bit.App.Constants.SettingLastBackgroundedDate, DateTime.UtcNow);
Resolver.Resolve<ISettings>().AddOrUpdateValue(App.Constants.SettingLastBackgroundedDate, DateTime.UtcNow);
var item = _tableItems.ElementAt(indexPath.Row);
if(item == null)
{
_controller.CompleteRequest(null);
_controller.LoadingController.CompleteRequest(null);
return;
}
NSDictionary itemData = null;
if(_context.ProviderType == UTType.PropertyList)
{
var fillScript = new FillScript(_context.Details, item.Username, item.Password);
var scriptJson = JsonConvert.SerializeObject(fillScript, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var scriptDict = new NSDictionary(Constants.AppExtensionWebViewPageFillScript, scriptJson);
itemData = new NSDictionary(NSJavaScriptExtension.FinalizeArgumentKey, scriptDict);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionFindLoginAction)
{
itemData = new NSDictionary(
Constants.AppExtensionUsernameKey, item.Username,
Constants.AppExtensionPasswordKey, item.Password);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionFillBrowserAction
|| _context.ProviderType == Constants.UTTypeAppExtensionFillWebViewAction)
{
var fillScript = new FillScript(_context.Details, item.Username, item.Password);
var scriptJson = JsonConvert.SerializeObject(fillScript, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
itemData = new NSDictionary(Constants.AppExtensionWebViewPageFillScript, scriptJson);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionSaveLoginAction)
{
itemData = new NSDictionary(
Constants.AppExtensionUsernameKey, item.Username,
Constants.AppExtensionPasswordKey, item.Password);
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionChangePasswordAction)
{
itemData = new NSDictionary(
Constants.AppExtensionPasswordKey, "mynewpassword",
Constants.AppExtensionOldPasswordKey, "myoldpassword");
}
Debug.WriteLine("BW LOG, itemData: " + itemData);
_controller.CompleteRequest(itemData);
_controller.LoadingController.CompleteUsernamePasswordRequest(item.Username, item.Password);
}
}
}