2017-08-23 18:40:40 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Controls;
|
|
|
|
|
using Bit.App.Resources;
|
2017-12-11 23:51:16 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2017-08-23 18:40:40 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class EnvironmentPage : ExtendedContentPage
|
|
|
|
|
{
|
|
|
|
|
private IAppSettingsService _appSettings;
|
2017-12-22 23:00:11 +03:00
|
|
|
|
private IDeviceActionService _deviceActionService;
|
2017-08-23 18:40:40 +03:00
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
|
|
|
|
|
|
|
|
|
public EnvironmentPage()
|
|
|
|
|
: base(updateActivity: false)
|
|
|
|
|
{
|
|
|
|
|
_appSettings = Resolver.Resolve<IAppSettingsService>();
|
2017-12-22 23:00:11 +03:00
|
|
|
|
_deviceActionService = Resolver.Resolve<IDeviceActionService>();
|
2017-08-23 18:40:40 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FormEntryCell BaseUrlCell { get; set; }
|
2017-08-29 01:08:26 +03:00
|
|
|
|
public FormEntryCell WebVaultUrlCell { get; set; }
|
2017-08-23 18:40:40 +03:00
|
|
|
|
public FormEntryCell ApiUrlCell { get; set; }
|
|
|
|
|
public FormEntryCell IdentityUrlCell { get; set; }
|
2017-10-24 06:20:35 +03:00
|
|
|
|
public FormEntryCell IconsUrlCell { get; set; }
|
2017-08-23 18:40:40 +03:00
|
|
|
|
public StackLayout StackLayout { get; set; }
|
|
|
|
|
public Label SelfHostLabel { get; set; }
|
|
|
|
|
public Label CustomLabel { get; set; }
|
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
|
|
|
|
|
2017-10-24 06:20:35 +03:00
|
|
|
|
IconsUrlCell = new FormEntryCell(AppResources.IconsUrl, entryKeyboard: Keyboard.Url);
|
|
|
|
|
IconsUrlCell.Entry.Text = _appSettings.IconsUrl;
|
|
|
|
|
|
|
|
|
|
IdentityUrlCell = new FormEntryCell(AppResources.IdentityUrl, nextElement: IconsUrlCell.Entry,
|
|
|
|
|
entryKeyboard: Keyboard.Url);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
IdentityUrlCell.Entry.Text = _appSettings.IdentityUrl;
|
2017-10-24 06:20:35 +03:00
|
|
|
|
|
|
|
|
|
ApiUrlCell = new FormEntryCell(AppResources.ApiUrl, nextElement: IdentityUrlCell.Entry,
|
|
|
|
|
entryKeyboard: Keyboard.Url);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
ApiUrlCell.Entry.Text = _appSettings.ApiUrl;
|
2017-10-24 06:20:35 +03:00
|
|
|
|
|
|
|
|
|
WebVaultUrlCell = new FormEntryCell(AppResources.WebVaultUrl, nextElement: ApiUrlCell.Entry,
|
|
|
|
|
entryKeyboard: Keyboard.Url);
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell.Entry.Text = _appSettings.WebVaultUrl;
|
2017-10-24 06:20:35 +03:00
|
|
|
|
|
|
|
|
|
BaseUrlCell = new FormEntryCell(AppResources.ServerUrl, nextElement: WebVaultUrlCell.Entry,
|
|
|
|
|
entryKeyboard: Keyboard.Url);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
BaseUrlCell.Entry.Text = _appSettings.BaseUrl;
|
|
|
|
|
|
|
|
|
|
var table = new FormTableView
|
|
|
|
|
{
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
|
|
|
|
new TableSection(AppResources.SelfHostedEnvironment)
|
|
|
|
|
{
|
|
|
|
|
BaseUrlCell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SelfHostLabel = new Label
|
|
|
|
|
{
|
|
|
|
|
Text = AppResources.SelfHostedEnvironmentFooter,
|
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var table2 = new FormTableView
|
|
|
|
|
{
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
|
|
|
|
new TableSection(AppResources.CustomEnvironment)
|
|
|
|
|
{
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell,
|
2017-08-23 18:40:40 +03:00
|
|
|
|
ApiUrlCell,
|
2017-10-24 06:20:35 +03:00
|
|
|
|
IdentityUrlCell,
|
|
|
|
|
IconsUrlCell
|
2017-08-23 18:40:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CustomLabel = new Label
|
|
|
|
|
{
|
|
|
|
|
Text = AppResources.CustomEnvironmentFooter,
|
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
StackLayout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { table, SelfHostLabel, table2, CustomLabel },
|
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView
|
|
|
|
|
{
|
|
|
|
|
Content = StackLayout
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-11 23:51:16 +03:00
|
|
|
|
var toolbarItem = new ToolbarItem(AppResources.Save, Helpers.ToolbarImage("envelope.png"), async () => await SaveAsync(),
|
2017-08-23 18:40:40 +03:00
|
|
|
|
ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2017-12-13 00:03:25 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.iOS)
|
2017-08-23 18:40:40 +03:00
|
|
|
|
{
|
|
|
|
|
table.RowHeight = table2.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = table2.EstimatedRowHeight = 70;
|
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Close, () =>
|
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", false);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(toolbarItem);
|
|
|
|
|
Title = AppResources.Settings;
|
|
|
|
|
Content = scrollView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
|
|
|
|
BaseUrlCell.InitEvents();
|
2017-10-24 06:20:35 +03:00
|
|
|
|
IconsUrlCell.InitEvents();
|
2017-08-23 18:40:40 +03:00
|
|
|
|
IdentityUrlCell.InitEvents();
|
|
|
|
|
ApiUrlCell.InitEvents();
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell.InitEvents();
|
2017-08-23 18:40:40 +03:00
|
|
|
|
StackLayout.LayoutChanged += Layout_LayoutChanged;
|
|
|
|
|
BaseUrlCell.Entry.FocusWithDelay();
|
|
|
|
|
}
|
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
BaseUrlCell.Dispose();
|
2017-10-24 06:20:35 +03:00
|
|
|
|
IconsUrlCell.Dispose();
|
2017-08-23 18:40:40 +03:00
|
|
|
|
IdentityUrlCell.Dispose();
|
|
|
|
|
ApiUrlCell.Dispose();
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell.Dispose();
|
2017-08-23 18:40:40 +03:00
|
|
|
|
StackLayout.LayoutChanged -= Layout_LayoutChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Layout_LayoutChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SelfHostLabel.WidthRequest = StackLayout.Bounds.Width - SelfHostLabel.Bounds.Left * 2;
|
|
|
|
|
CustomLabel.WidthRequest = StackLayout.Bounds.Width - CustomLabel.Bounds.Left * 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SaveAsync()
|
|
|
|
|
{
|
|
|
|
|
Uri result;
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(BaseUrlCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
BaseUrlCell.Entry.Text = FixUrl(BaseUrlCell.Entry.Text);
|
|
|
|
|
if(!Uri.TryCreate(BaseUrlCell.Entry.Text, UriKind.Absolute, out result))
|
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.ServerUrl),
|
|
|
|
|
AppResources.Ok);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BaseUrlCell.Entry.Text = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 01:08:26 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(WebVaultUrlCell.Entry.Text))
|
2017-08-29 00:50:17 +03:00
|
|
|
|
{
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell.Entry.Text = FixUrl(WebVaultUrlCell.Entry.Text);
|
|
|
|
|
if(!Uri.TryCreate(WebVaultUrlCell.Entry.Text, UriKind.Absolute, out result))
|
2017-08-29 00:50:17 +03:00
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.WebVaultUrl),
|
|
|
|
|
AppResources.Ok);
|
2017-08-29 00:50:17 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-29 01:08:26 +03:00
|
|
|
|
WebVaultUrlCell.Entry.Text = null;
|
2017-08-29 00:50:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 18:40:40 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(ApiUrlCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
ApiUrlCell.Entry.Text = FixUrl(ApiUrlCell.Entry.Text);
|
|
|
|
|
if(!Uri.TryCreate(ApiUrlCell.Entry.Text, UriKind.Absolute, out result))
|
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.ApiUrl),
|
|
|
|
|
AppResources.Ok);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ApiUrlCell.Entry.Text = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(IdentityUrlCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
IdentityUrlCell.Entry.Text = FixUrl(IdentityUrlCell.Entry.Text);
|
|
|
|
|
if(!Uri.TryCreate(IdentityUrlCell.Entry.Text, UriKind.Absolute, out result))
|
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.IdentityUrl),
|
|
|
|
|
AppResources.Ok);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IdentityUrlCell.Entry.Text = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-24 06:20:35 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(IconsUrlCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
IconsUrlCell.Entry.Text = FixUrl(IconsUrlCell.Entry.Text);
|
|
|
|
|
if(!Uri.TryCreate(IconsUrlCell.Entry.Text, UriKind.Absolute, out result))
|
|
|
|
|
{
|
2017-12-23 06:41:48 +03:00
|
|
|
|
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.IconsUrl),
|
|
|
|
|
AppResources.Ok);
|
2017-10-24 06:20:35 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IconsUrlCell.Entry.Text = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 18:40:40 +03:00
|
|
|
|
_appSettings.BaseUrl = BaseUrlCell.Entry.Text;
|
2017-10-24 06:20:35 +03:00
|
|
|
|
_appSettings.IconsUrl = IconsUrlCell.Entry.Text;
|
2017-08-23 18:40:40 +03:00
|
|
|
|
_appSettings.IdentityUrl = IdentityUrlCell.Entry.Text;
|
|
|
|
|
_appSettings.ApiUrl = ApiUrlCell.Entry.Text;
|
2017-08-29 01:08:26 +03:00
|
|
|
|
_appSettings.WebVaultUrl = WebVaultUrlCell.Entry.Text;
|
2017-12-22 23:00:11 +03:00
|
|
|
|
_deviceActionService.Toast(AppResources.EnvironmentSaved);
|
2017-08-23 18:40:40 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("SetEnvironmentUrls");
|
|
|
|
|
await Navigation.PopForDeviceAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string FixUrl(string url)
|
|
|
|
|
{
|
|
|
|
|
url = url.TrimEnd('/');
|
|
|
|
|
if(!url.StartsWith("http://") && !url.StartsWith("https://"))
|
|
|
|
|
{
|
|
|
|
|
url = $"https://{url}";
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class FormTableView : ExtendedTableView
|
|
|
|
|
{
|
|
|
|
|
public FormTableView()
|
|
|
|
|
{
|
|
|
|
|
Intent = TableIntent.Settings;
|
|
|
|
|
EnableScrolling = false;
|
|
|
|
|
HasUnevenRows = true;
|
|
|
|
|
EnableSelection = true;
|
|
|
|
|
VerticalOptions = LayoutOptions.Start;
|
|
|
|
|
NoFooter = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|