<file_sep>﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using static Week12SampleApp.Models.WeatherItemModel;
using Week12SampleApp.Models;

namespace Week12SampleApp.ViewModels
{
    public class MainPageViewModel : BindableBase, INavigationAware
    {
        public DelegateCommand<WeatherItem> RemoveWeatherItemCommand { get; set; }
        public DelegateCommand NavigatePageCommand { get; set; }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private ObservableCollection<WeatherItem> _weatherCollection = new ObservableCollection<WeatherItem>();
        public ObservableCollection<WeatherItem> WeatherCollection
        {
            get { return _weatherCollection; }
            set { SetProperty(ref _weatherCollection, value); }
        }

        INavigationService _navigationService;

        public MainPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            RemoveWeatherItemCommand = new DelegateCommand<WeatherItem>(RemoveWeatherItem);
            NavigatePageCommand = new DelegateCommand(NavigatePage);
        }

        private void NavigatePage()
        {
            _navigationService.NavigateAsync("NavigationSamplePage");
        }

        private void RemoveWeatherItem(WeatherItem itemToDelete)
        {
            WeatherCollection.Remove(itemToDelete);
        }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {

        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {
            Title = "Week 12 Sample App";
        }

        public void OnNavigatingTo(NavigationParameters parameters)
        {

        }
    }
}

<file_sep>﻿using Xamarin.Forms;

namespace Week12SampleApp.Views
{
    public partial class NavigationSamplePage : ContentPage
    {
        public NavigationSamplePage()
        {
            InitializeComponent();
        }
    }
}

<file_sep>﻿using NUnit.Framework;
using System;
using static Week12SampleApp.Models.WeatherItemModel;
using Week12SampleApp.ViewModels;
using Prism.Navigation;
using Moq;

namespace Week12SampleApp.UnitTests
{
    [TestFixture()]
    public class MainPageViewModelTests
    {
        MainPageViewModel mainPageViewModel;

        Mock<INavigationService> navigationServiceMock;

        [SetUp]
        public void Init()
        {
            navigationServiceMock = new Mock<INavigationService>();
            mainPageViewModel = new MainPageViewModel(navigationServiceMock.Object);
        }

        [Test]
        public void TestRemoveWeatherItemCommandRemovesWeatherItemFromCollection()
        {
            var weatherItemToDelete = new WeatherItem();
            weatherItemToDelete.Name = "<NAME>";
            mainPageViewModel.WeatherCollection.Add(weatherItemToDelete);

            mainPageViewModel.RemoveWeatherItemCommand.Execute(weatherItemToDelete);

            CollectionAssert.DoesNotContain(mainPageViewModel.WeatherCollection,
                                            weatherItemToDelete);
        }

        [Test]
        public void TestOnNavigatedToPopulatesTitle()
        {
            mainPageViewModel.Title = string.Empty;
            mainPageViewModel.OnNavigatedTo(null);
            Assert.AreEqual("Week 12 Sample App", mainPageViewModel.Title);
        }

        [Test]
        public void TestNavigatePageCommandCallsNavigateAsync()
        {
            navigationServiceMock.Setup(ns => ns.NavigateAsync(It.IsAny<string>(), It.IsAny<NavigationParameters>(),
                                                             It.IsAny<bool?>(), It.IsAny<bool>()));
            mainPageViewModel.NavigatePageCommand.Execute();
            navigationServiceMock.Verify(ns=> ns.NavigateAsync("NavigationSamplePage", It.IsAny<NavigationParameters>(),
                                          It.IsAny<bool?>(), It.IsAny<bool>()), Times.Once());
        }
    }
}
