Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/navigation command #99

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public ContactListPageModel (IDatabaseService databaseService)
public override void Init (object initData)
{
Contacts = new ObservableCollection<Contact> (_databaseService.GetContacts ());
AddContact = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<ContactPageModel>());
}

protected override void ViewIsAppearing (object sender, EventArgs e)
Expand Down Expand Up @@ -52,13 +53,7 @@ public Contact SelectedContact {
}
}

public Command AddContact {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<ContactPageModel> ();
});
}
}
public ICommand AddContact { get; private set; }

public Command<Contact> ContactSelected {
get {
Expand All @@ -71,7 +66,7 @@ public Command<Contact> ContactSelected {
public ICommand OpenFirst
{
get
{
{
return new FreshAwaitCommand(async (contact, tcs) =>
{
await CoreMethods.PushPageModel<ContactPageModel>(this.Contacts.First());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using PropertyChanged;
using FreshMvvm;
using System;
using System.Windows.Input;
using System.Threading.Tasks;

namespace FreshMvvmSampleApp
{
Expand All @@ -10,6 +12,12 @@ public class ContactPageModel : FreshBasePageModel
{
IDatabaseService _dataService;

public ICommand SaveCommand { get; private set; }
public ICommand TestModal { get; private set; }
public ICommand TestModalNavigationBasic { get; private set; }
public ICommand TestModalNavigationTabbed { get; private set; }
public ICommand TestModalNavigationMasterDetail { get; private set; }

public ContactPageModel (IDatabaseService dataService)
{
_dataService = dataService;
Expand All @@ -31,62 +39,42 @@ public override void Init (object initData)
} else {
Contact = new Contact ();
}
}

public Command SaveCommand {
get {
return new Command (() => {
_dataService.UpdateContact (Contact);
CoreMethods.PopPageModel (Contact);
}
);
}
var sharedLock = new SharedLock();
SaveCommand = CoreMethods.CreateCommand(SaveCommandLogic, sharedLock);
TestModal = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<ModalPageModel>(null, true), sharedLock);
TestModalNavigationBasic = CoreMethods.CreateCommand(TestModalNavigationBasicLogic, sharedLock);
TestModalNavigationTabbed = CoreMethods.CreateCommand(TestModalNavigationTabbedLogic, sharedLock);
TestModalNavigationMasterDetail = CoreMethods.CreateCommand(TestModalNavigationMasterDetailLogic, sharedLock);
}

public Command TestModal {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<ModalPageModel> (null, true);
});
}
private async Task SaveCommandLogic()
{
_dataService.UpdateContact(Contact);
await CoreMethods.PopPageModel(Contact);
}

public Command TestModalNavigationBasic {
get {
return new Command (async () => {

var page = FreshPageModelResolver.ResolvePageModel<MainMenuPageModel> ();
var basicNavContainer = new FreshNavigationContainer (page, Guid.NewGuid ().ToString ());
await CoreMethods.PushNewNavigationServiceModal(basicNavContainer, new FreshBasePageModel[] { page.GetModel() });
});
}
private async Task TestModalNavigationBasicLogic()
{
var page = FreshPageModelResolver.ResolvePageModel<MainMenuPageModel>();
var basicNavContainer = new FreshNavigationContainer(page, Guid.NewGuid().ToString());
await CoreMethods.PushNewNavigationServiceModal(basicNavContainer, new FreshBasePageModel[] { page.GetModel() });
}


public Command TestModalNavigationTabbed {
get {
return new Command (async () => {

var tabbedNavigation = new FreshTabbedNavigationContainer (Guid.NewGuid ().ToString ());
tabbedNavigation.AddTab<ContactListPageModel> ("Contacts", "contacts.png", null);
tabbedNavigation.AddTab<QuoteListPageModel> ("Quotes", "document.png", null);
await CoreMethods.PushNewNavigationServiceModal(tabbedNavigation);
});
}
public async Task TestModalNavigationTabbedLogic() {
var tabbedNavigation = new FreshTabbedNavigationContainer (Guid.NewGuid ().ToString ());
tabbedNavigation.AddTab<ContactListPageModel> ("Contacts", "contacts.png", null);
tabbedNavigation.AddTab<QuoteListPageModel> ("Quotes", "document.png", null);
await CoreMethods.PushNewNavigationServiceModal(tabbedNavigation);
}

public Command TestModalNavigationMasterDetail {
get {
return new Command (async () => {

var masterDetailNav = new FreshMasterDetailNavigationContainer (Guid.NewGuid ().ToString ());
masterDetailNav.Init ("Menu", "Menu.png");
masterDetailNav.AddPage<ContactListPageModel> ("Contacts", null);
masterDetailNav.AddPage<QuoteListPageModel> ("Quotes", null);
await CoreMethods.PushNewNavigationServiceModal(masterDetailNav);

});
}
public async Task TestModalNavigationMasterDetailLogic()
{
var masterDetailNav = new FreshMasterDetailNavigationContainer(Guid.NewGuid().ToString());
masterDetailNav.Init("Menu", "Menu.png");
masterDetailNav.AddPage<ContactListPageModel>("Contacts", null);
masterDetailNav.AddPage<QuoteListPageModel>("Quotes", null);
await CoreMethods.PushNewNavigationServiceModal(masterDetailNav);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
using Xamarin.Forms;
using FreshMvvm;
using System.Windows.Input;

namespace FreshMvvmSampleApp
{
public class MainMenuPageModel : FreshBasePageModel
{
public MainMenuPageModel ()
{
}
public ICommand ShowQuotes { get; private set; }
public ICommand ShowContacts { get; private set; }

public Command ShowQuotes {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<QuoteListPageModel> ();
});
}
}

public Command ShowContacts {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<ContactListPageModel> ();
});
}
public override void Init(object initData)
{
var sharedLock = new SharedLock();
ShowQuotes = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<QuoteListPageModel>(), sharedLock);
ShowContacts = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<ContactListPageModel>(), sharedLock);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FreshMvvm;
using PropertyChanged;
using System.Diagnostics;
using System.Windows.Input;

namespace FreshMvvmSampleApp
{
Expand All @@ -21,6 +22,7 @@ public QuoteListPageModel (IDatabaseService databaseService)
public override void Init (object initData)
{
Quotes = new ObservableCollection<Quote> (_databaseService.GetQuotes ());
AddQuote = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<QuotePageModel>());
}

protected override void ViewIsAppearing (object sender, System.EventArgs e)
Expand All @@ -42,13 +44,7 @@ public override void ReverseInit (object value)
}
}

public Command AddQuote {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<QuotePageModel> ();
});
}
}
public ICommand AddQuote { get; private set; }

Quote _selectedQuote;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Xamarin.Forms;
using PropertyChanged;
using FreshMvvm;
using System.Windows.Input;
using System.Threading.Tasks;

namespace FreshMvvmSampleApp
{
Expand All @@ -9,6 +11,9 @@ public class QuotePageModel : FreshBasePageModel
{
IDatabaseService _databaseService;

public ICommand SaveCommand { get; private set; }
public ICommand TestModal { get; private set; }

public Quote Quote { get; set; }

public QuotePageModel (IDatabaseService databaseService)
Expand All @@ -17,27 +22,18 @@ public QuotePageModel (IDatabaseService databaseService)
}

public override void Init (object initData)
{
Quote = initData as Quote;
if (Quote == null)
Quote = new Quote ();
}
{
Quote = (initData as Quote) ?? new Quote();

public Command SaveCommand {
get {
return new Command (async () => {
_databaseService.UpdateQuote (Quote);
await CoreMethods.PopPageModel (Quote);
});
}
var sharedLock = new SharedLock();
SaveCommand = CoreMethods.CreateCommand(SaveCommandLogic, sharedLock);
TestModal = CoreMethods.CreateCommand(() => CoreMethods.PushPageModel<ModalPageModel>(null, true), sharedLock);
}

public Command TestModal {
get {
return new Command (async () => {
await CoreMethods.PushPageModel<ModalPageModel> (null, true);
});
}
private async Task SaveCommandLogic()
{
_databaseService.UpdateQuote (Quote);
await CoreMethods.PopPageModel (Quote);
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/FreshMvvm.Tests/Fixtures/FreshNavigationCommandFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using NUnit.Framework;
using System.Threading.Tasks;

namespace FreshMvvm.Tests.Fixtures
{
[TestFixture]
public class FreshNavigationCommandFixture
{
SharedLock _sharedLock;

[SetUp]
public void Setup()
{
_sharedLock = new SharedLock();
}

[Test]
public async Task OnlyOneCommandWillExecuteTests()
{
//Flow: A executes, B executes, A starts, B ignored, A completes.
int countBefore = 0;
int countAfter = 0;
Func<object, Task> execute = async (obj) =>
{
countBefore++;
await Task.Delay(100);
countAfter++;
};

//Execute is async void, so this will run in the background.
var first = new FreshNavigationCommand(execute, _sharedLock).ExecuteAsync(null);
var second = new FreshNavigationCommand(execute, _sharedLock).ExecuteAsync(null);

await Task.WhenAll(first, second); //Need to let execute finish.

Assert.AreEqual(1, countBefore);
Assert.AreEqual(1, countAfter);
}
}
}
1 change: 1 addition & 0 deletions src/FreshMvvm.Tests/FreshMvvm.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="Mocks\MockFreshBasePageModel.cs" />
<Compile Include="Fixtures\MultipleNavigationProviderTests.cs" />
<Compile Include="Helpers\Helper.cs" />
<Compile Include="Fixtures\FreshNavigationCommandFixture.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Xamarin.Forms.2.3.0.49\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.2.3.0.49\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
Expand Down
21 changes: 21 additions & 0 deletions src/FreshMvvm.Tests/Mocks/MockPageModelCoreMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace FreshMvvm.Tests.Mocks
Expand Down Expand Up @@ -209,5 +210,25 @@ public Task<FreshBasePageModel> SwitchSelectedMaster<T>() where T : FreshBasePag
{
throw new NotImplementedException();
}

public ICommand CreateCommand(Func<Task> execute, SharedLock sharedLock = null)
{
throw new NotImplementedException();
}

public ICommand CreateCommand(Func<object, Task> execute, SharedLock sharedLock = null)
{
throw new NotImplementedException();
}

public ICommand CreateCommand<TValue>(Func<TValue, Task> execute, SharedLock sharedLock = null)
{
throw new NotImplementedException();
}

public ICommand CreateCommand<TValue>(Func<TValue, Task> execute, Func<string, TValue> stringConverter, SharedLock sharedLock = null)
{
throw new NotImplementedException();
}
}
}
2 changes: 2 additions & 0 deletions src/FreshMvvm/FreshMvvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<Compile Include="PropertyChangedExtensions.cs" />
<Compile Include="IFreshPageModelMapper.cs" />
<Compile Include="FreshPageModelMapper.cs" />
<Compile Include="IFreshNavigationCommand.cs" />
<Compile Include="FreshNavigationCommand.cs" />
<Compile Include="NavigationContainers\FreshMasterDetailNavigationContainer.cs" />
<Compile Include="NavigationContainers\FreshTabbedFONavigationContainer.cs" />
<Compile Include="NavigationContainers\FreshTabbedNavigationContainer.cs" />
Expand Down
Loading