示例#1
0
文件: Util.cs 项目: pythe/wristpass
        public static void ShowFilenameDialog(Activity activity, Func<string, Dialog, bool> onOpen, Func<string, Dialog, bool> onCreate, Action onCancel, bool showBrowseButton, string defaultFilename, string detailsText, int requestCodeBrowse)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.SetView(activity.LayoutInflater.Inflate(Resource.Layout.file_selection_filename, null));

            if (onCancel != null)
                builder.SetOnCancelListener(new CancelListener(onCancel));
            Dialog dialog = builder.Create();
            dialog.Show();

            Button openButton = (Button) dialog.FindViewById(Resource.Id.open);
            Button createButton = (Button) dialog.FindViewById(Resource.Id.create);

            TextView enterFilenameDetails = (TextView) dialog.FindViewById(Resource.Id.label_open_by_filename_details);
            openButton.Visibility = onOpen != null ? ViewStates.Visible : ViewStates.Gone;
            createButton.Visibility = onCreate != null? ViewStates.Visible : ViewStates.Gone;
            // Set the initial value of the filename
            EditText editFilename = (EditText) dialog.FindViewById(Resource.Id.file_filename);
            editFilename.Text = defaultFilename;
            enterFilenameDetails.Text = detailsText;
            enterFilenameDetails.Visibility = enterFilenameDetails.Text == "" ? ViewStates.Gone : ViewStates.Visible;

            // Open button
            if (onOpen != null)
                openButton.Click += (sender, args) =>
                    {
                        String fileName = ((EditText) dialog.FindViewById(Resource.Id.file_filename)).Text;
                        if (onOpen(fileName, dialog))
                            dialog.Dismiss();
                    };

            // Create button
            if (onCreate != null)
                createButton.Click += (sender, args) =>
                {
                    String fileName = ((EditText)dialog.FindViewById(Resource.Id.file_filename)).Text;
                    if (onCreate(fileName, dialog))
                        dialog.Dismiss();
                };

            Button cancelButton = (Button) dialog.FindViewById(Resource.Id.fnv_cancel);
            cancelButton.Click += delegate
                {
                    dialog.Dismiss();
                    if (onCancel != null)
                    onCancel();
                };

            ImageButton browseButton = (ImageButton) dialog.FindViewById(Resource.Id.browse_button);
            if (!showBrowseButton)
            {
                browseButton.Visibility = ViewStates.Invisible;
            }
            browseButton.Click += (sender, evt) =>
                {
                    string filename = ((EditText) dialog.FindViewById(Resource.Id.file_filename)).Text;

                    Util.ShowBrowseDialog(activity, requestCodeBrowse, onCreate != null, /*TODO should we prefer ActionOpenDocument here?*/ false);

                };
        }
        public override async Task<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
        {
            MobileServicePreconditionFailedException error;

            do
            {
                error = null;

                try
                {
                    return await operation.ExecuteAsync();
                }
                catch (MobileServicePreconditionFailedException ex)
                {
                    error = ex;
                }

                if (error != null)
                {
                    var localItem = operation.Item.ToObject<ToDoItem>();
                    var serverValue = error.Value;

                    var builder = new AlertDialog.Builder(this.activity);
                    builder.SetTitle("Conflict between local and server versions");
                    builder.SetMessage("How do you want to resolve this conflict?\n\n" + "Local item: \n" + localItem +
                        "\n\nServer item:\n" + serverValue.ToObject<ToDoItem>());

                    var clickTask = new TaskCompletionSource<int>();

                    builder.SetPositiveButton(LOCAL_VERSION, (which, e) =>
                    {
                        clickTask.SetResult(1);
                    });
                    builder.SetNegativeButton(SERVER_VERSION, (which, e) =>
                    {
                        clickTask.SetResult(2);
                    });
                    builder.SetOnCancelListener(new CancelListener(clickTask));

                    builder.Create().Show();

                    int command = await clickTask.Task;
                    if (command == 1)
                    {
                        // Overwrite the server version and try the operation again by continuing the loop
                        operation.Item[MobileServiceSystemColumns.Version] = serverValue[MobileServiceSystemColumns.Version];
                        continue;
                    }
                    else if (command == 2)
                    {
                        return (JObject)serverValue;
                    }
                    else
                    {
                        operation.AbortPush();
                    }
                }
            } while (error != null);

            return null;
        }