Skip to content

Commit

Permalink
Merge pull request #14 from Bl4ckL10n/feature/add_visit_link_without_…
Browse files Browse the repository at this point in the history
…saving_button_in_add_new_item_page

Add visit now button in add new item page
  • Loading branch information
maxalmonte14 authored Jun 17, 2022
2 parents 7513483 + 619ccac commit 1f0cc9a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,18 @@
<data name="Button_Save" xml:space="preserve">
<value>Save</value>
</data>
<data name="Button_Visit_Now" xml:space="preserve">
<value>Visit now</value>
</data>
<data name="Entry_Name_Placeholder" xml:space="preserve">
<value>Name</value>
</data>
<data name="Entry_URL_Placeholder" xml:space="preserve">
<value>URL</value>
</data>
<data name="Label_Or" xml:space="preserve">
<value>Or</value>
</data>
<data name="Page_Title" xml:space="preserve">
<value>New Item</value>
</data>
Expand All @@ -141,4 +147,13 @@
<data name="Save_Modal_Title" xml:space="preserve">
<value>Duplicated entry</value>
</data>
<data name="Visit_Now_Modal_Content" xml:space="preserve">
<value>We couldn't open this webpage due to an error.</value>
</data>
<data name="Visit_Now_Modal_Ok_Option" xml:space="preserve">
<value>Ok</value>
</data>
<data name="Visit_Now_Modal_Title" xml:space="preserve">
<value>Oopps!</value>
</data>
</root>
35 changes: 31 additions & 4 deletions BlackLion.QRStore/BlackLion.QRStore/ViewModels/NewItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using BlackLion.QRStore.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace BlackLion.QRStore.ViewModels
Expand Down Expand Up @@ -36,14 +38,16 @@ public string URL

public Command CancelCommand { get; }
public Command SaveCommand { get; }
public Command VisitNowCommand { get; }

public NewItemViewModel()
{
_dataStore = DependencyService.Get<IDataStore<Item>>();
_messageService = DependencyService.Get<IMessageService>();
Title = NewItemPageResources.Page_Title;
SaveCommand = new Command(OnSave, ValidateSave);
CancelCommand = new Command(OnCancel);
CancelCommand = new Command(async () => await OnCancel());
VisitNowCommand = new Command(async() => await OnVisitNow());
PropertyChanged += (_, __) => SaveCommand.ChangeCanExecute();
}

Expand All @@ -56,8 +60,8 @@ private bool ValidateSave()

IsValidURL = URLValidatorHelper.IsValidURL(URL);

return !String.IsNullOrWhiteSpace(url) &&
!String.IsNullOrWhiteSpace(name) &&
return !string.IsNullOrWhiteSpace(url) &&
!string.IsNullOrWhiteSpace(name) &&
isValidURL;
}

Expand All @@ -66,7 +70,7 @@ public void ApplyQueryAttributes(IDictionary<string, string> query)
URL = HttpUtility.UrlDecode(query["url"]);
}

private async void OnCancel()
private async Task OnCancel()
{
await Shell.Current.GoToAsync("//ItemsPage");
}
Expand Down Expand Up @@ -95,5 +99,28 @@ await _messageService.ShowAsync(
await Shell.Current.GoToAsync("//ItemsPage");
}
}

private async Task OnVisitNow()
{
try
{
await Task.Run(async () =>
{
await Browser.OpenAsync(url, BrowserLaunchMode.SystemPreferred);
}).ContinueWith(async _ =>
{
await Task.Delay(300);
await Shell.Current.GoToAsync("//ItemsPage");
});
}
catch (Exception)
{
await _messageService.ShowAsync(
NewItemPageResources.Visit_Now_Modal_Title,
NewItemPageResources.Visit_Now_Modal_Content,
NewItemPageResources.Visit_Now_Modal_Ok_Option
);
}
}
}
}
6 changes: 5 additions & 1 deletion BlackLion.QRStore/BlackLion.QRStore/Views/NewItemPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<local:NewItemViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Padding="15" Spacing="3">
<StackLayout Padding="15" Spacing="10">
<Entry FontSize="Medium"
Placeholder="{x:Static resources:NewItemPageResources.Entry_Name_Placeholder}"
Text="{Binding Name}"/>
Expand Down Expand Up @@ -43,6 +43,10 @@
HorizontalOptions="FillAndExpand"
Text="{x:Static resources:NewItemPageResources.Button_Save}"/>
</StackLayout>
<Label FontSize="Medium"
HorizontalOptions="Center"
Text="{x:Static resources:NewItemPageResources.Label_Or}"/>
<Button Command="{Binding VisitNowCommand}" Text="{x:Static resources:NewItemPageResources.Button_Visit_Now}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

0 comments on commit 1f0cc9a

Please sign in to comment.