Пример #1
0
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            try
            {
                if (_isWaiting || item == null)
                {
                    return(false);
                }

                switch (item.ItemId)
                {
                case Android.Resource.Id.Home:
                    Finish();
                    return(true);

                case Resource.Id.menuItemFileBrowserUpward:
                    if (_currentParent != null && _currentParent.DocumentId != _root.DocumentId)
                    {
                        var previousParentId = _previousParents.Count == 0
                                ? GetParent(_currentParent.DocumentId) ?? _root.DocumentId
                                : _previousParents.Pop();
                        ReplaceFragment(parentDocumentId: previousParentId);
                    }
                    return(true);

                case Resource.Id.menuItemFileBrowserCreateFolder:
                    var dlg = new WidgetUtil();
                    dlg.InputDialog(context: this, titleId: Resource.String.CreateFolderTitle,
                                    layoutId: Resource.Layout.text_input, editTextId: Resource.Id.txtInput,
                                    okAction: (view, value) =>
                    {
                        if (string.IsNullOrEmpty(value))
                        {
                            return(false);
                        }

                        try
                        {
                            var docId = _documentsProvider.CreateDocument(_currentParent.DocumentId,
                                                                          DocumentsContract.Document.MimeTypeDir, value);
                            _previousParents.Push(_currentParent.DocumentId);
                            ReplaceFragment(useCustomAnimation: false, parentDocumentId: docId);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex);
                            Toast.MakeText(this, GetString(Resource.String.InternalError),
                                           AppSettings.ToastLength)
                            .Show();
                            return(false);
                        }

                        return(true);
                    });
                    return(true);

                case Resource.Id.menuItemFileBrowserSortByName:
                    item.SetChecked(!item.IsChecked);
                    if (item.IsChecked && _orderBy != FileDocumentItemSortOrders.Default && _orderBy != FileDocumentItemSortOrders.ByName)
                    {
                        _orderBy = FileDocumentItemSortOrders.ByName;
                        ReplaceFragment();
                    }
                    return(true);

                case Resource.Id.menuItemFileBrowserSortByDate:
                    item.SetChecked(!item.IsChecked);
                    if (item.IsChecked && _orderBy != FileDocumentItemSortOrders.ByLastModified)
                    {
                        _orderBy = FileDocumentItemSortOrders.ByLastModified;
                        ReplaceFragment();
                    }
                    return(true);

                case Resource.Id.menuItemFileBrowserSortBySize:
                    item.SetChecked(!item.IsChecked);
                    if (item.IsChecked && _orderBy != FileDocumentItemSortOrders.BySize)
                    {
                        _orderBy = FileDocumentItemSortOrders.BySize;
                        ReplaceFragment();
                    }
                    return(true);

                case Resource.Id.menuItemFileBrowserRefresh:
                    ReplaceFragment();
                    return(true);

                case Resource.Id.menuItemFileBrowserSettings:
                    var intent = new Intent(this, typeof(FileBrowserSettingsActivity));
                    _doNotCancel = true;
                    StartActivityForResult(intent, MenuSettingsRequestCode);
                    break;

                default:
                    return(base.OnOptionsItemSelected(item));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Toast.MakeText(this, GetString(Resource.String.InternalError), AppSettings.ToastLength)
                .Show();
            }

            return(false);
        }
Пример #2
0
        public bool ApiKeyDialog(Activity activity, ProviderTypes providerType, int providerId,
                                 bool alwaysShowDialog, Action okAction, Action cancelAction, ToastLength toastLength, ILog log)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }

            var isValidServiceProvider = true;
            var isValidApiKey          = true;

            switch (providerType)
            {
            case ProviderTypes.WeatherProvider:
                isValidServiceProvider = AppSettings.Default.ValidateWeatherProvider(providerId);
                break;
            }

            if (isValidServiceProvider)
            {
                isValidApiKey = AppSettings.Default.ValidateApiKey(providerId);
            }

            var result = isValidServiceProvider && isValidApiKey;

            if (alwaysShowDialog || !result)
            {
                int providerNameId;
                switch (providerType)
                {
                case ProviderTypes.WeatherProvider:
                    providerNameId = AppSettings.Default.GetWeatherProviderNameById(providerId);
                    break;

                default:
                    providerNameId = AppSettings.Default.GetProviderNameById(providerId);
                    break;
                }

                string message = null;

                activity.RunOnUiThread(() =>
                {
                    if (!isValidServiceProvider)
                    {
                        message = string.Format(activity.GetString(Resource.String.WeatherProviderIsNotImplemented),
                                                providerNameId);
                    }
                    else if (alwaysShowDialog || !isValidApiKey)
                    {
                        string GetErrorHandler()
                        {
                            switch (providerType)
                            {
                            case ProviderTypes.WeatherProvider:
                                return(Properties.Resources.UndefinedWeatherProviderApiKey);

                            default:
                                return(Http.Properties.Resources.UndefinedServerApiKey);
                            }
                        }

                        Exception GetExceptionHandler()
                        {
                            switch (providerType)
                            {
                            case ProviderTypes.WeatherProvider:
                                return(new WeatherApiKeyException());

                            default:
                                return(new ApiKeyException());
                            }
                        }

                        var dlg = new WidgetUtil();
                        dlg.InputDialog(context: activity, titleId: providerNameId,
                                        layoutId: Resource.Layout.text_input, editTextId: Resource.Id.txtInput,
                                        createAction: (view, input) =>
                        {
                            if (input == null)
                            {
                                return;
                            }

                            // android:inputType="textPassword"
                            input.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
                        },
                                        okAction: (view, value) =>
                        {
                            if (string.IsNullOrEmpty(value))
                            {
                                return(false);
                            }

                            try
                            {
                                AppSettings.Default.SaveApiKey(providerId, value);
                                if (!AppSettings.Default.ValidateApiKey(providerId))
                                {
                                    throw GetExceptionHandler();
                                }

                                okAction?.Invoke();
                            }
                            catch (Exception ex)
                            {
                                log?.ErrorFormat("{0} {1}", "OkAction", ex);
                                Toast.MakeText(activity, activity.GetString(Resource.String.InternalError),
                                               toastLength).Show();
                            }

                            return(true);
                        },
                                        cancelAction: () =>
                        {
                            cancelAction?.Invoke();
                            if (!AppSettings.Default.ValidateApiKey(providerId))
                            {
                                message = GetErrorHandler();
                                if (!string.IsNullOrEmpty(message))
                                {
                                    Toast.MakeText(activity, message, toastLength).Show();
                                }
                            }
                        });

                        return;
                    }

                    if (!string.IsNullOrEmpty(message))
                    {
                        Toast.MakeText(activity, message, toastLength).Show();
                    }
                });
            }

            return(result);
        }