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

142 lines
5.8 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.Core;
using Bit.iOS.Extension.Models;
using Foundation;
using MobileCoreServices;
using Newtonsoft.Json;
using UIKit;
using Microsoft.Practices.Unity;
using XLabs.Ioc;
namespace Bit.iOS.Extension
{
public partial class ActionViewController : UIViewController
{
2016-06-03 06:28:17 +03:00
public ActionViewController(IntPtr handle) : base(handle)
{ }
public Context Context { 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();
2016-06-05 01:27:34 +03:00
Debug.WriteLine("BW LOG, Container");
var siteService = Resolver.Resolve<ISiteService>();
Debug.WriteLine("BW LOG, siteService: " + siteService);
var sites = await siteService.GetAllAsync();
Debug.WriteLine("BW LOG, sites: " + sites.Count());
var siteModels = sites.Select(s => new SiteViewModel(s));
Debug.WriteLine("BW LOG, siteModels: " + siteModels.Count());
var filteredSiteModels = siteModels.Where(s => s.HostName == Context.Url?.Host);
Debug.WriteLine("BW LOG, filteredSiteModels: " + filteredSiteModels.Count());
tableView.Source = new TableSource(filteredSiteModels, this);
2016-06-11 08:17:08 +03:00
AutomaticallyAdjustsScrollViewInsets = false;
}
partial void CancelClicked(UIBarButtonItem sender)
{
CompleteRequest(null);
}
2016-06-05 01:27:34 +03:00
private void CompleteRequest(NSDictionary itemData)
2016-06-04 19:10:03 +03:00
{
2016-06-05 01:27:34 +03:00
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);
}
public class TableSource : UITableViewSource
{
private const string CellIdentifier = "TableCell";
private IEnumerable<SiteViewModel> _tableItems;
2016-06-05 01:27:34 +03:00
private Context _context;
private ActionViewController _controller;
public TableSource(IEnumerable<SiteViewModel> items, ActionViewController controller)
{
2016-06-05 01:27:34 +03:00
_tableItems = items;
_context = controller.Context;
_controller = controller;
}
2016-06-05 01:27:34 +03:00
public override nint RowsInSection(UITableView tableview, nint section)
{
2016-06-05 01:27:34 +03:00
return _tableItems.Count();
}
2016-06-05 01:27:34 +03:00
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
2016-06-05 01:27:34 +03:00
var cell = tableView.DequeueReusableCell(CellIdentifier);
var item = _tableItems.ElementAt(indexPath.Row);
2016-06-05 01:27:34 +03:00
// if there are no cells to reuse, create a new one
if(cell == null)
{
2016-06-11 08:17:08 +03:00
cell = new UITableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier);
2016-06-05 01:27:34 +03:00
}
cell.TextLabel.Text = item.Name;
cell.DetailTextLabel.Text = item.Username;
2016-06-05 01:27:34 +03:00
return cell;
}
2016-06-05 01:27:34 +03:00
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
NSDictionary itemData = null;
if(_context.ProviderType == UTType.PropertyList)
{
var fillScript = new FillScript(_context.Details);
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, "me@example.com",
Constants.AppExtensionPasswordKey, "mypassword");
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionFillBrowserAction
|| _context.ProviderType == Constants.UTTypeAppExtensionFillWebViewAction)
{
var fillScript = new FillScript(_context.Details);
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, "me@example.com",
Constants.AppExtensionPasswordKey, "mypassword");
}
else if(_context.ProviderType == Constants.UTTypeAppExtensionChangePasswordAction)
{
itemData = new NSDictionary(
Constants.AppExtensionPasswordKey, "mynewpassword",
Constants.AppExtensionOldPasswordKey, "myoldpassword");
}
2016-06-10 07:36:15 +03:00
Debug.WriteLine("BW LOG, itemData: " + itemData);
2016-06-05 01:27:34 +03:00
_controller.CompleteRequest(itemData);
}
}
}
}