<commit_before>using Foundation;
using System;
using UIKit;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using MDKControl.Core.Models;
using MDKControl.Core.ViewModels;
using MDKControl.iOS.Extensions;
using CoreGraphics;

namespace MDKControl.iOS
{
    partial class DeviceViewController : UITableViewController, INavigationTarget
	{
        private Binding _isConnectedBinding;
        private Binding _modeBinding;

        UISwitch ConnectButton { get; } = new UISwitch();

		public DeviceViewController (IntPtr handle) : base (handle)
		{
		}

        public object NavigationParameter { get; set; }

        public DeviceViewModel Vm { get; private set; }

        public override void ViewDidLoad()
        {
            System.Diagnostics.Debug.WriteLine("DeviceViewController ViewDidLoad");

            Vm = (DeviceViewModel)NavigationParameter;

            Vm.PropertyChanged += (s, e) => {};
            ConnectButton.ValueChanged += (s, e) => {};
            ConnectButton.On = false;

            var tableView = (UITableView)View;
            tableView.BackgroundColor = Colors.DefaultLightGray;
            tableView.BackgroundView = null;
            tableView.AllowsSelection = true;

            ModeSmsTableViewCell.ImageView.Image = new UIImage("Timer.png").MakeThumb(new CGSize(29, 29));
            ModePanoramaTableViewCell.ImageView.Image = new UIImage("PanoramaFilled.png").MakeThumb(new CGSize(29, 29));
            ModeAstroTableViewCell.ImageView.Image = new UIImage("TelescopeFilled.png").MakeThumb(new CGSize(29, 29));

            //ConnectButton = new UISwitch();
            NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(ConnectButton), false);

            _isConnectedBinding = this.SetBinding(() => Vm.IsConnected, () => ConnectButton.On, BindingMode.TwoWay)
                .UpdateTargetTrigger("ValueChanged");
            _isConnectedBinding.ValueChanged += (sender, e) => 
                {
                    UIView.Animate(0.5, () => 
                        {
                            if (Vm.IsConnected)
                            {
                                View.UserInteractionEnabled = true;
                                View.Alpha = 1.0f;
                            }
                            else
                            {
                                View.UserInteractionEnabled = false;
                                View.Alpha = 0.5f;

                            }
                        });
                };
            _isConnectedBinding.ForceUpdateValueFromSourceToTarget();

            /*_modeBinding = this.SetBinding(() => ModeButton.SelectedSegment, () => Vm.ProgramMode, BindingMode.TwoWay)
                .ConvertTargetToSource(m =>
                    {
                        switch (m)
                        {
                            default:
                            case MoCoBusProgramMode.ShootMoveShoot:
                                return 0;
                            case MoCoBusProgramMode.Panorama:
                                return 1;
                            case MoCoBusProgramMode.Astro:
                                return 2;
                        }
                    })
                .WhenSourceChanges(() =>
                    {
                        if (ModeButton.SelectedSegment == 0 && Vm.ProgramMode != MoCoBusProgramMode.ShootMoveShoot)
                        {
                            Vm.SetModeSmsCommand.Execute(null);
                        }
                        else if (ModeButton.SelectedSegment == 1 && Vm.ProgramMode != MoCoBusProgramMode.Panorama)
                        {
                            Vm.SetModePanoCommand.Execute(null);
                        }
                        else if (ModeButton.SelectedSegment == 2 && Vm.ProgramMode != MoCoBusProgramMode.Astro)
                        {
                            Vm.SetModeAstroCommand.Execute(null);
                        }
                    })
                .UpdateSourceTrigger("ValueChanged");*/
                

            /*Vm.PropertyChanged += (sender, e) => 
                {
                    if (e.PropertyName == "ProgramMode")
                    {
                        switch (Vm.ProgramMode)
                        {
                            case MoCoBusProgramMode.ShootMoveShoot:
                                if (ModeButton.SelectedSegment != 0) ModeButton.SelectedSegment = 0;
                                break;
                            case MoCoBusProgramMode.Panorama:
                                if (ModeButton.SelectedSegment != 1) ModeButton.SelectedSegment = 1;
                                break;
                            case MoCoBusProgramMode.Astro:
                                if (ModeButton.SelectedSegment != 2) ModeButton.SelectedSegment = 2;
                                break;
                        }
                    }
                };

            ModeButton.ValueChanged += (sender, e) => 
                {
                    if (ModeButton.SelectedSegment == 0 && Vm.ProgramMode != MoCoBusProgramMode.ShootMoveShoot)
                    {
                        Vm.SetModeSmsCommand.Execute(null);
                    }
                    else if (ModeButton.SelectedSegment == 1 && Vm.ProgramMode != MoCoBusProgramMode.Panorama)
                    {
                        Vm.SetModePanoCommand.Execute(null);
                    }
                    else if (ModeButton.SelectedSegment == 2 && Vm.ProgramMode != MoCoBusProgramMode.Astro)
                    {
                        Vm.SetModeAstroCommand.Execute(null);
                    }
                };*/

            base.ViewDidLoad();
        }

        public override void ViewWillUnload()
        {
            
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt(indexPath);
            System.Diagnostics.Debug.WriteLine(cell);

            //cell.SetSelected(false, true);

            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(AppDelegate.ModeSmsViewKey, Vm.ModeSmsViewModel);
        }
	}
}<commit_msg>implemented program mode change requests and change handling<commit_after>using CoreGraphics;
using Foundation;
using System;
using UIKit;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using MDKControl.Core.Models;
using MDKControl.Core.ViewModels;
using MDKControl.iOS.Extensions;

namespace MDKControl.iOS
{
    partial class DeviceViewController : UITableViewController, INavigationTarget
	{
        private bool _modeChangeRequested = false;
        
        private Binding _isConnectedBinding;

		public DeviceViewController (IntPtr handle) : base (handle)
		{
		}

        private UISwitch ConnectButton { get; } = new UISwitch();

        public object NavigationParameter { get; set; }

        public DeviceViewModel Vm { get; private set; }

        public override void ViewDidLoad()
        {
            System.Diagnostics.Debug.WriteLine("DeviceViewController ViewDidLoad");

            Vm = (DeviceViewModel)NavigationParameter;

            Vm.PropertyChanged += (s, e) => {};
            ConnectButton.ValueChanged += (s, e) => {};
            ConnectButton.On = false;

            var tableView = (UITableView)View;
            tableView.BackgroundColor = Colors.DefaultLightGray;
            tableView.BackgroundView = null;
            tableView.AllowsSelection = true;

            ModeSmsTableViewCell.ImageView.Image = new UIImage("Timer.png").MakeThumb(new CGSize(29, 29));
            ModePanoramaTableViewCell.ImageView.Image = new UIImage("PanoramaFilled.png").MakeThumb(new CGSize(29, 29));
            ModeAstroTableViewCell.ImageView.Image = new UIImage("TelescopeFilled.png").MakeThumb(new CGSize(29, 29));

            NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(ConnectButton), false);

            _isConnectedBinding = this.SetBinding(() => Vm.IsConnected, () => ConnectButton.On, BindingMode.TwoWay)
                .UpdateTargetTrigger("ValueChanged");
            _isConnectedBinding.ValueChanged += (sender, e) => 
                {
                    UIView.Animate(0.5, () => 
                        {
                            if (Vm.IsConnected)
                            {
                                View.UserInteractionEnabled = true;
                                View.Alpha = 1.0f;
                            }
                            else
                            {
                                View.UserInteractionEnabled = false;
                                View.Alpha = 0.5f;

                            }
                        });
                };
            _isConnectedBinding.ForceUpdateValueFromSourceToTarget();

            Vm.PropertyChanged += (sender, e) => 
                {
                    var navService = ServiceLocator.Current.GetInstance<INavigationService>();
                    
                    if (e.PropertyName == "ProgramMode")
                    {
                        System.Diagnostics.Debug.WriteLine("DeviceViewModel PropertyChanged ProgramMode");

                        switch (Vm.ProgramMode)
                        {
                            case MoCoBusProgramMode.ShootMoveShoot:
                                if (navService.CurrentPageKey != AppDelegate.ModeSmsViewKey)
                                {
                                    if (_modeChangeRequested)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating to ModeSmsView since a mode change was requested");
                                        _modeChangeRequested = false;
                                        navService.NavigateTo(AppDelegate.ModeSmsViewKey, Vm.ModeSmsViewModel);
                                    }
                                    else if (navService.CurrentPageKey != ViewModelLocator.DeviceViewKey)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating back to DeviceView since ProgramMode was changed to another mode");
                                        navService.NavigateTo(ViewModelLocator.DeviceViewKey, Vm);
                                    }
                                }
                                break;
                            case MoCoBusProgramMode.Panorama:
                                if (navService.CurrentPageKey != AppDelegate.ModePanoViewKey)
                                {
                                    if (_modeChangeRequested)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating to ModePanoView since a mode change was requested");
                                        _modeChangeRequested = false;
                                        //navService.NavigateTo(AppDelegate.ModePanoViewKey, Vm.ModePanoViewModel);
                                    }
                                    else if (navService.CurrentPageKey != ViewModelLocator.DeviceViewKey)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating back to DeviceView since ProgramMode was changed to another mode");
                                        navService.NavigateTo(ViewModelLocator.DeviceViewKey, Vm);
                                    }
                                }
                                break;
                            case MoCoBusProgramMode.Astro:
                                if (navService.CurrentPageKey != AppDelegate.ModeAstroViewKey)
                                {
                                    if (_modeChangeRequested)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating to ModeAstroView since a mode change was requested");
                                        _modeChangeRequested = false;
                                        //navService.NavigateTo(AppDelegate.ModeAstroViewKey, Vm.ModeAstroViewModel);
                                    }
                                    else if (navService.CurrentPageKey != ViewModelLocator.DeviceViewKey)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Navigating back to DeviceView since ProgramMode was changed to another mode");
                                        navService.NavigateTo(ViewModelLocator.DeviceViewKey, Vm);
                                    }
                                }
                                break;
                        }
                    }
                };

            base.ViewDidLoad();
        }

        /*public override void ViewWillUnload()
        {
            
        }*/

        /*public override void ViewWillDisappear(bool animated)
        {
            if (IsMovingFromParentViewController || IsBeingDismissed)
            {
                Vm.IsConnected = false;
            }
            
            base.ViewWillDisappear(animated);
        }

        public override void WillMoveToParentViewController(UIViewController parent)
        {
            if (IsMovingFromParentViewController || IsBeingDismissed)
            {
                Vm.IsConnected = false;
            }

            base.WillMoveToParentViewController(parent);
        }*/

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            System.Diagnostics.Debug.WriteLine("DeviceViewController RowSelected");

            var cell = tableView.CellAt(indexPath);
            System.Diagnostics.Debug.WriteLine(cell);

            //cell.SetSelected(false, true);

            _modeChangeRequested = true;

            // TODO: Select command based on indexPath
            Vm.SetModeSmsCommand.Execute(null);
        }
	}
}
{'fraction_non_alphanumeric': 0.07426439532591862, 'fraction_numerical': 0.003590032380684218, 'mean_word_length': 1.2275007839448102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '509886', 'n_tokens_mistral': 3376, 'n_tokens_neox': 3093, 'n_words': 549}