bitwarden-android/src/App/Pages/EnvironmentPage.cs

267 lines
9.7 KiB
C#
Raw Normal View History

using System;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Resources;
using Bit.App.Utilities;
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;
private IGoogleAnalyticsService _googleAnalyticsService;
public EnvironmentPage()
: base(updateActivity: false)
{
_appSettings = Resolver.Resolve<IAppSettingsService>();
2017-12-22 23:00:11 +03:00
_deviceActionService = Resolver.Resolve<IDeviceActionService>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
Init();
}
public FormEntryCell BaseUrlCell { get; set; }
2017-08-29 01:08:26 +03:00
public FormEntryCell WebVaultUrlCell { get; set; }
public FormEntryCell ApiUrlCell { get; set; }
public FormEntryCell IdentityUrlCell { get; set; }
2017-10-24 06:20:35 +03:00
public FormEntryCell IconsUrlCell { get; set; }
2018-01-03 20:18:15 +03:00
public RedrawableStackLayout 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);
IdentityUrlCell.Entry.Text = _appSettings.IdentityUrl;
2017-10-24 06:20:35 +03:00
ApiUrlCell = new FormEntryCell(AppResources.ApiUrl, nextElement: IdentityUrlCell.Entry,
entryKeyboard: Keyboard.Url);
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);
BaseUrlCell.Entry.Text = _appSettings.BaseUrl;
2018-01-03 20:18:15 +03:00
var table = new FormTableView(this)
{
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"],
2018-01-03 20:18:15 +03:00
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 5)
};
2018-01-03 20:18:15 +03:00
var table2 = new FormTableView(this)
{
Root = new TableRoot
{
new TableSection(AppResources.CustomEnvironment)
{
2017-08-29 01:08:26 +03:00
WebVaultUrlCell,
ApiUrlCell,
2017-10-24 06:20:35 +03:00
IdentityUrlCell,
IconsUrlCell
}
}
};
CustomLabel = new Label
{
Text = AppResources.CustomEnvironmentFooter,
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"],
2018-01-03 20:18:15 +03:00
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 5)
};
2018-01-03 20:18:15 +03:00
StackLayout = new RedrawableStackLayout
{
Children = { table, SelfHostLabel, table2, CustomLabel },
Spacing = 0
};
var scrollView = new ScrollView
{
Content = StackLayout
};
var toolbarItem = new ToolbarItem(AppResources.Save, Helpers.ToolbarImage("envelope.png"), async () => await SaveAsync(),
ToolbarItemOrder.Default, 0);
if(Device.RuntimePlatform == Device.iOS)
{
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();
IdentityUrlCell.InitEvents();
ApiUrlCell.InitEvents();
2017-08-29 01:08:26 +03:00
WebVaultUrlCell.InitEvents();
BaseUrlCell.Entry.FocusWithDelay();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
BaseUrlCell.Dispose();
2017-10-24 06:20:35 +03:00
IconsUrlCell.Dispose();
IdentityUrlCell.Dispose();
ApiUrlCell.Dispose();
2017-08-29 01:08:26 +03:00
WebVaultUrlCell.Dispose();
}
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))
{
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.ServerUrl),
AppResources.Ok);
return;
}
}
else
{
BaseUrlCell.Entry.Text = null;
}
2017-08-29 01:08:26 +03:00
if(!string.IsNullOrWhiteSpace(WebVaultUrlCell.Entry.Text))
{
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))
{
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.WebVaultUrl),
AppResources.Ok);
return;
}
}
else
{
2017-08-29 01:08:26 +03:00
WebVaultUrlCell.Entry.Text = null;
}
if(!string.IsNullOrWhiteSpace(ApiUrlCell.Entry.Text))
{
ApiUrlCell.Entry.Text = FixUrl(ApiUrlCell.Entry.Text);
if(!Uri.TryCreate(ApiUrlCell.Entry.Text, UriKind.Absolute, out result))
{
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.ApiUrl),
AppResources.Ok);
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))
{
await DisplayAlert(null, string.Format(AppResources.FormattedIncorrectly, AppResources.IdentityUrl),
AppResources.Ok);
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))
{
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;
}
_appSettings.BaseUrl = BaseUrlCell.Entry.Text;
2017-10-24 06:20:35 +03:00
_appSettings.IconsUrl = IconsUrlCell.Entry.Text;
_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);
_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
{
2018-01-03 20:18:15 +03:00
public FormTableView(EnvironmentPage page)
{
Intent = TableIntent.Settings;
EnableScrolling = false;
HasUnevenRows = true;
EnableSelection = true;
VerticalOptions = LayoutOptions.Start;
NoFooter = true;
2018-01-03 20:18:15 +03:00
WrappingStackLayout = () => page.StackLayout;
}
}
}
}