protected async Task <MvxViewModelInstanceRequest> NavigationRouteRequest(string path, IMvxBundle presentationBundle = null)
        {
            KeyValuePair <Regex, Type> entry;

            if (!TryGetRoute(path, out entry))
            {
                return(null);
            }

            var regex           = entry.Key;
            var match           = regex.Match(path);
            var paramDict       = BuildParamDictionary(regex, match);
            var parameterValues = new MvxBundle(paramDict);

            var           viewModelType = entry.Value;
            IMvxViewModel viewModel;

            if (viewModelType.GetInterfaces().Contains(typeof(IMvxNavigationFacade)))
            {
                var facade = (IMvxNavigationFacade)Mvx.IocConstruct(viewModelType);

                try
                {
                    var facadeRequest = await facade.BuildViewModelRequest(path, paramDict);

                    viewModel = (IMvxViewModel)Mvx.IocConstruct(facadeRequest.ViewModelType);

                    if (facadeRequest == null)
                    {
                        Mvx.TaggedWarning("MvxNavigationService", "Facade did not return a valid MvxViewModelRequest.");
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    Mvx.TaggedError("MvxNavigationService",
                                    "Exception thrown while processing URL: {0} with RoutingFacade: {1}, {2}",
                                    path, viewModelType, ex);
                    return(null);
                }
            }
            else
            {
                try
                {
                    viewModel = (IMvxViewModel)Mvx.IocConstruct(viewModelType);
                }
                catch (Exception exception)
                {
                    throw exception.MvxWrap("Problem creating viewModel of type {0}", viewModelType.Name);
                }
            }

            return(new MvxViewModelInstanceRequest(viewModel)
            {
                ParameterValues = parameterValues.SafeGetData(), PresentationValues = presentationBundle?.SafeGetData()
            });
        }
Пример #2
0
        private async Task <IMvxViewModel> NavigateRoute(string path)
        {
            KeyValuePair <Regex, Type> entry;

            if (!TryGetRoute(path, out entry))
            {
                return(null);
            }

            var regex     = entry.Key;
            var match     = regex.Match(path);
            var paramDict = BuildParamDictionary(regex, match);

            var viewModelType           = entry.Value;
            MvxViewModelRequest request = null;
            IMvxViewModel       viewModel;

            if (viewModelType.GetInterfaces().Contains(typeof(IMvxNavigationFacade)))
            {
                var facade = (IMvxNavigationFacade)Mvx.IocConstruct(viewModelType);

                try
                {
                    var facadeRequest = await facade.BuildViewModelRequest(path, paramDict);

                    viewModel = (IMvxViewModel)Mvx.IocConstruct(facadeRequest.ViewModelType);

                    if (facadeRequest == null)
                    {
                        Mvx.TaggedWarning("MvxNavigationService", "Facade did not return a valid MvxViewModelRequest.");
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    Mvx.TaggedError("MvxNavigationService",
                                    "Exception thrown while processing URL: {0} with RoutingFacade: {1}, {2}",
                                    path, viewModelType, ex);
                    return(null);
                }
            }
            else
            {
                viewModel = (IMvxViewModel)Mvx.IocConstruct(viewModelType);
            }

            request = new MvxViewModelInstanceRequest(viewModel)
            {
                ParameterValues = new MvxBundle(paramDict).SafeGetData()
            };
            _viewDispatcher.ShowViewModel(request);

            return(viewModel);
        }
Пример #3
0
        public static List <AdvertisementRecord> ParseAdvertismentData(NSDictionary advertisementData)
        {
            var records = new List <AdvertisementRecord>();

            /*var keys = new List<NSString>
             * {
             *  CBAdvertisement.DataLocalNameKey,
             *  CBAdvertisement.DataManufacturerDataKey,
             *  CBAdvertisement.DataOverflowServiceUUIDsKey, //ToDo ??which one is this according to ble spec
             *  CBAdvertisement.DataServiceDataKey,
             *  CBAdvertisement.DataServiceUUIDsKey,
             *  CBAdvertisement.DataSolicitedServiceUUIDsKey,
             *  CBAdvertisement.DataTxPowerLevelKey
             * };*/

            foreach (var o in advertisementData.Keys)
            {
                var key = (NSString)o;
                if (key == CBAdvertisement.DataLocalNameKey)
                {
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.CompleteLocalName,
                                                        NSData.FromString(advertisementData.ObjectForKey(key) as NSString).ToArray()));
                }
                else if (key == CBAdvertisement.DataManufacturerDataKey)
                {
                    var arr = ((NSData)advertisementData.ObjectForKey(key)).ToArray();
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.ManufacturerSpecificData, arr));
                }
                else if (key == CBAdvertisement.DataServiceUUIDsKey)
                {
                    var array = (NSArray)advertisementData.ObjectForKey(key);

                    var dataList = new List <NSData>();
                    for (nuint i = 0; i < array.Count; i++)
                    {
                        var cbuuid = array.GetItem <CBUUID>(i);
                        dataList.Add(cbuuid.Data);
                    }
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.UuidsComplete128Bit,
                                                        dataList.SelectMany(d => d.ToArray()).ToArray()));
                }
                else
                {
                    Mvx.TaggedWarning("Parsing Advertisement",
                                      "Ignoring Advertisement entry for key {0}, since we don't know how to parse it yet",
                                      key.ToString());
                }
            }

            return(records);
        }
Пример #4
0
        async Task Reload()
        {
            try
            {
                var items = await HatenaFotolife.FetchHotfotosAsync();

                Items = items;
            }
            catch (Exception e)
            {
                // TODO: エラーのメッセージを投げる
                Mvx.TaggedWarning(LogTag, "Reload Exception: {0}", e.ToString());
            }
        }
        private void OnBackgroundTimeExpired(CancellationTokenSource cancellationTokenSource)
        {
            Mvx.TaggedWarning(Tag, "Background task expired");

            try
            {
                cancellationTokenSource.Cancel();
            }
            catch (ObjectDisposedException)
            {
            }

            // Task will end after some time to prevent app terminating by OS.
            Timer = NSTimer.CreateScheduledTimer(ForceEndingTime, timer => End());
        }
        public async Task RouteAsync(string url, MvxRequestedBy requestedBy)
        {
            KeyValuePair <Regex, Type> entry;

            if (!TryGetRoute(url, out entry))
            {
                return;
            }

            var regex     = entry.Key;
            var match     = regex.Match(url);
            var paramDict = BuildParamDictionary(regex, match);

            var viewModelType           = entry.Value;
            MvxViewModelRequest request = null;

            if (viewModelType.GetInterfaces().Contains(typeof(IMvxRoutingFacade)))
            {
                var facade = (IMvxRoutingFacade)Mvx.IocConstruct(viewModelType);

                try
                {
                    request = await facade.BuildViewModelRequest(url, paramDict, requestedBy);
                }
                catch (Exception ex)
                {
                    Mvx.TaggedError("MvxRoutingService",
                                    "Exception thrown while processing URL: {0} with RoutingFacade: {1}, {2}",
                                    url, viewModelType, ex);
                }

                if (request == null)
                {
                    Mvx.TaggedWarning("MvxRoutingService", "Facade did not return a valid MvxViewModelRequest.");
                    return;
                }
            }
            else
            {
                request = new MvxViewModelRequest(
                    viewModelType,
                    new MvxBundle(paramDict),
                    null,
                    requestedBy);
            }

            _viewDispatcher.ShowViewModel(request);
        }
Пример #7
0
        // HACK: UNTESTED - this API has only been tested on iOS
        public Task <ICharacteristic> ReadAsync()
        {
            var tcs = new TaskCompletionSource <ICharacteristic>();

            if (!CanRead)
            {
                throw new InvalidOperationException("Characteristic does not support READ");
            }
            EventHandler <CharacteristicReadEventArgs> updated = null;

            updated = (object sender, CharacteristicReadEventArgs e) =>
            {
                // it may be other characteristics, so we need to test
                if (e.Characteristic.ID == ID)
                {
                    tcs.TrySetResult(e.Characteristic);
                    if (_gattCallback != null)
                    {
                        _gattCallback.CharacteristicValueUpdated -= updated;
                    }
                }
            };

            if (_gattCallback != null)
            {
                // wire up the characteristic value updating on the gattcallback
                _gattCallback.CharacteristicValueUpdated += updated;
            }
            else
            {
                tcs.TrySetException(new MemberAccessException("Gatt callback is null"));
                return(tcs.Task);
            }


            Mvx.TaggedTrace("ReadAsync", "requesting characteristic read");
            var ret = _gatt.ReadCharacteristic(_nativeCharacteristic);

            if (!ret)
            {
                _gattCallback.CharacteristicValueUpdated -= updated;
                Mvx.TaggedWarning("ReadAsync", "Gatt read characteristic call returned {0}", ret);
                tcs.TrySetException(new InvalidOperationException("Gatt read characteristic call failed"));
            }

            return(tcs.Task);
        }
Пример #8
0
        public static Task DisconnectAsync(this IAdapter adapter, IDevice device)
        {
            if (!adapter.ConnectedDevices.Contains(device))
            {
                Mvx.Trace("Disconnect async: device {0} not in the list of connected devices.", device.Name);
                return(Task.FromResult(false));
            }

            var tcs = new TaskCompletionSource <IDevice>();
            EventHandler <DeviceConnectionEventArgs> h  = null;
            EventHandler <DeviceConnectionEventArgs> he = null;

            h = (sender, e) =>
            {
                Mvx.TaggedTrace("DisconnectAsync", "Disconnected: {0} {1}", e.Device.ID, e.Device.Name);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceDisconnected    -= h;
                    adapter.DeviceConnectionError -= he;
                    tcs.TrySetResult(e.Device);
                }
            };

            he = (sender, e) =>
            {
                // Would be nice to use C#6.0 null-conditional operators like e.Device?.ID
                Mvx.TaggedWarning("DisconnectAsync", "Disconnect Error: {0} {1}",
                                  (e.Device != null ? e.Device.ID.ToString() : ""),
                                  (e.Device != null ? e.Device.Name : ""));
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnectionError -= he;
                    adapter.DeviceDisconnected    -= h;
                    tcs.TrySetException(new Exception("Disconnect operation exception"));
                }
            };


            adapter.DeviceDisconnected    += h;
            adapter.DeviceConnectionError += he;

            adapter.DisconnectDevice(device);

            return(tcs.Task);
        }
Пример #9
0
        /// <summary>
        /// Asynchronously gets the requested service
        /// </summary>
        public static Task <IDevice> ConnectAsync(this IAdapter adapter, IDevice device)
        {
            if (device.State == DeviceState.Connected)
            {
                return(Task.FromResult <IDevice>(device));
            }

            var tcs = new TaskCompletionSource <IDevice>();
            EventHandler <DeviceConnectionEventArgs> h  = null;
            EventHandler <DeviceConnectionEventArgs> he = null;

            h = (sender, e) =>
            {
                Mvx.TaggedTrace("ConnectAsync", "Connected: {0} {1}", e.Device.ID, e.Device.Name);
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnected       -= h;
                    adapter.DeviceConnectionError -= he;
                    tcs.TrySetResult(e.Device);
                }
            };

            he = (sender, e) =>
            {
                // Would be nice to use C#6.0 null-conditional operators like e.Device?.ID
                Mvx.TaggedWarning("ConnectAsync", "Connection Error: {0} {1}",
                                  (e.Device != null ? e.Device.ID.ToString() : ""),
                                  (e.Device != null ? e.Device.Name : ""));
                if (e.Device.ID == device.ID)
                {
                    adapter.DeviceConnectionError -= he;
                    adapter.DeviceConnected       -= h;
                    tcs.TrySetException(new Exception("Connect operation exception"));
                }
            };

            adapter.DeviceConnected       += h;
            adapter.DeviceConnectionError += he;

            adapter.ConnectToDevice(device);

            return(tcs.Task);
        }
Пример #10
0
        private static bool TryGetRoute(string url, out KeyValuePair <Regex, Type> entry)
        {
            try
            {
                var matches = Routes.Where(t => t.Key.IsMatch(url)).ToList();

                switch (matches.Count)
                {
                case 0:
                    entry = default(KeyValuePair <Regex, Type>);
                    Mvx.TaggedTrace("MvxNavigationService", "Unable to find routing for {0}", url);
                    return(false);

                case 1:
                    entry = matches[0];
                    return(true);
                }

                var directMatch = matches.Where(t => t.Key.Match(url).Groups.Count == 1).ToList();

                if (directMatch.Count == 1)
                {
                    entry = directMatch[0];
                    return(true);
                }

                Mvx.TaggedWarning("MvxNavigationService",
                                  "The following regular expressions match the provided url ({0}), each RegEx must be unique (otherwise try using IMvxRoutingFacade): {1}",
                                  matches.Count - 1,
                                  string.Join(", ", matches.Select(t => t.Key.ToString())));
                // there is more than one match
                return(false);
            }
            catch (Exception ex)
            {
                Mvx.TaggedError("MvxNavigationService", "Unable to determine routability: {0}", ex);
                return(false);
            }
        }
Пример #11
0
 public void Warn(string format, params object[] args)
 {
     Mvx.TaggedWarning(_tag, format, args);
 }
Пример #12
0
 public static void TaggedWarning(string tag, string message, params object[] args)
 {
     Mvx.TaggedWarning(tag, message, args);
 }
Пример #13
0
 public void Warning(string message, params object[] args)
 {
     Mvx.TaggedWarning(_tag, message, args);
 }