Ejemplo n.º 1
0
        public override Task <RequestResult <TagsBundle> > LoadTagsAsync()
        {
            var source = new TaskCompletionSource <RequestResult <TagsBundle> >();

            nativeManager.GetTags(new Callback()
            {
                ResultCallback = (Result obj) =>
                {
                    var bundle = obj.Data as Tags.TagsBundle;
                    var tags   = new TagsBundle();
                    if (bundle != null)
                    {
                        var jsonObject   = bundle.ToJson();
                        var keysIterator = jsonObject.Keys();

                        while (keysIterator.HasNext)
                        {
                            var key = (keysIterator.Next() as Java.Lang.String).ToString();
                            var val = jsonObject.Get(key);
                            if (val is Java.Lang.String)
                            {
                                tags.PutString(key, val.ToString());
                            }
                            else if (val is Java.Lang.Integer)
                            {
                                tags.PutInteger(key, (val as Java.Lang.Integer).IntValue());
                            }
                            else if (val is Org.Json.JSONArray)
                            {
                                var jsonArray = val as Org.Json.JSONArray;
                                string[] list = new string[jsonArray.Length()];
                                for (int i = 0; i < list.Length; ++i)
                                {
                                    list[i] = jsonArray.Get(i).ToString();
                                }
                                tags.PutList(key, list);
                            }
                        }
                    }
                    if (obj.Exception != null)
                    {
                        source.SetResult(new RequestResult <TagsBundle>()
                        {
                            Error = ErrorFromJavaException(obj.Exception)
                        });
                    }
                    else
                    {
                        source.SetResult(new RequestResult <TagsBundle>()
                        {
                            Result = tags
                        });
                    }
                }
            });
            return(source.Task);
        }
Ejemplo n.º 2
0
        public override Task <RequestResult <TagsBundle> > LoadTagsAsync()
        {
            var source = new TaskCompletionSource <RequestResult <TagsBundle> >();

            nativeManager.LoadTags((NSDictionary tagsDict) =>
            {
                var tags = new TagsBundle();
                foreach (var kv in tagsDict)
                {
                    if (kv.Key is NSString)
                    {
                        if (kv.Value is NSString)
                        {
                            tags.PutString(kv.Key.ToString(), kv.Value.ToString());
                        }
                        else if (kv.Value is NSNumber)
                        {
                            tags.PutInteger(kv.Key.ToString(), (kv.Value as NSNumber).Int32Value);
                        }
                        else if (kv.Value is NSArray)
                        {
                            tags.PutList(kv.Key.ToString(), NSArray.ArrayFromHandle <string>(kv.Value.Handle, (input) => new NSObject(input).Description));
                        }
                    }
                }
                source.SetResult(new RequestResult <TagsBundle>()
                {
                    Result = tags
                });
            }, (error) =>
            {
                source.SetResult(new RequestResult <TagsBundle>()
                {
                    Error = ErrorFromNSError(error)
                });
            });
            return(source.Task);
        }