Search in sources :

Example 16 with Account

use of it.niedermann.owncloud.notes.persistence.entity.Account in project nextcloud-notes by stefan-niedermann.

the class ManageAccountsActivity method onChangeFileSuffix.

private void onChangeFileSuffix(@NonNull Account localAccount) {
    final var repository = NotesRepository.getInstance(getApplicationContext());
    final var spinner = new Spinner(this);
    final var wrapper = createDialogViewWrapper();
    final var adapter = ArrayAdapter.createFromResource(this, R.array.settings_file_suffixes, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    final var dialog = new BrandedAlertDialogBuilder(this).setTitle(R.string.settings_file_suffix).setMessage(R.string.settings_file_suffix_description).setView(wrapper).setNeutralButton(android.R.string.cancel, null).setPositiveButton(R.string.action_edit_save, (v, d) -> new Thread(() -> {
        try {
            final Call<NotesSettings> putSettingsCall = repository.putServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), new NotesSettings(null, spinner.getSelectedItem().toString()), getPreferredApiVersion(localAccount.getApiVersion()));
            putSettingsCall.enqueue(new Callback<>() {

                @Override
                public void onResponse(@NonNull Call<NotesSettings> call, @NonNull Response<NotesSettings> response) {
                    final var body = response.body();
                    if (response.isSuccessful() && body != null) {
                        runOnUiThread(() -> Toast.makeText(ManageAccountsActivity.this, getString(R.string.settings_file_suffix_success, body.getNotesPath()), Toast.LENGTH_LONG).show());
                    } else {
                        runOnUiThread(() -> Toast.makeText(ManageAccountsActivity.this, getString(R.string.http_status_code, response.code()), Toast.LENGTH_LONG).show());
                    }
                }

                @Override
                public void onFailure(@NonNull Call<NotesSettings> call, @NonNull Throwable t) {
                    runOnUiThread(() -> ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
                }
            });
        } catch (NextcloudFilesAppAccountNotFoundException e) {
            runOnUiThread(() -> ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
        }
    }).start()).show();
    try {
        repository.getServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), getPreferredApiVersion(localAccount.getApiVersion())).enqueue(new Callback<>() {

            @Override
            public void onResponse(@NonNull Call<NotesSettings> call, @NonNull Response<NotesSettings> response) {
                final NotesSettings body = response.body();
                runOnUiThread(() -> {
                    if (response.isSuccessful() && body != null) {
                        for (int i = 0; i < adapter.getCount(); i++) {
                            if (adapter.getItem(i).equals(body.getFileSuffix())) {
                                spinner.setSelection(i);
                                break;
                            }
                        }
                        wrapper.removeAllViews();
                        wrapper.addView(spinner);
                    } else {
                        dialog.dismiss();
                        ExceptionDialogFragment.newInstance(new Exception(getString(R.string.http_status_code, response.code()))).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
                    }
                });
            }

            @Override
            public void onFailure(@NonNull Call<NotesSettings> call, @NonNull Throwable t) {
                runOnUiThread(() -> {
                    dialog.dismiss();
                    ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
                });
            }
        });
    } catch (NextcloudFilesAppAccountNotFoundException e) {
        dialog.dismiss();
        ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
    }
}
Also used : AttrRes(androidx.annotation.AttrRes) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) LockedActivity(it.niedermann.owncloud.notes.LockedActivity) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) FrameLayout(android.widget.FrameLayout) Response(retrofit2.Response) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) SDK_INT(android.os.Build.VERSION.SDK_INT) Px(androidx.annotation.Px) LOLLIPOP_MR1(android.os.Build.VERSION_CODES.LOLLIPOP_MR1) ActivityManageAccountsBinding(it.niedermann.owncloud.notes.databinding.ActivityManageAccountsBinding) Toast(android.widget.Toast) NotesSettings(it.niedermann.owncloud.notes.shared.model.NotesSettings) IResponseCallback(it.niedermann.owncloud.notes.shared.model.IResponseCallback) ViewModelProvider(androidx.lifecycle.ViewModelProvider) R(it.niedermann.owncloud.notes.R) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) ExceptionDialogFragment(it.niedermann.owncloud.notes.exception.ExceptionDialogFragment) NotesRepository(it.niedermann.owncloud.notes.persistence.NotesRepository) NetworkErrorException(android.accounts.NetworkErrorException) ArrayAdapter(android.widget.ArrayAdapter) AccountImporter(com.nextcloud.android.sso.AccountImporter) BrandedAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder) Nullable(androidx.annotation.Nullable) TypedValue(android.util.TypedValue) Callback(retrofit2.Callback) ApiVersionUtil.getPreferredApiVersion(it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion) BrandedDeleteAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedDeleteAlertDialogBuilder) Call(retrofit2.Call) EditText(android.widget.EditText) Account(it.niedermann.owncloud.notes.persistence.entity.Account) Call(retrofit2.Call) Spinner(android.widget.Spinner) NotesSettings(it.niedermann.owncloud.notes.shared.model.NotesSettings) BrandedAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) NetworkErrorException(android.accounts.NetworkErrorException) Response(retrofit2.Response) IResponseCallback(it.niedermann.owncloud.notes.shared.model.IResponseCallback) Callback(retrofit2.Callback) NonNull(androidx.annotation.NonNull) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Aggregations

Account (it.niedermann.owncloud.notes.persistence.entity.Account)16 Capabilities (it.niedermann.owncloud.notes.shared.model.Capabilities)7 Bundle (android.os.Bundle)6 Test (org.junit.Test)6 NetworkErrorException (android.accounts.NetworkErrorException)5 NonNull (androidx.annotation.NonNull)5 AlertDialog (androidx.appcompat.app.AlertDialog)5 NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)5 R (it.niedermann.owncloud.notes.R)5 Intent (android.content.Intent)4 SDK_INT (android.os.Build.VERSION.SDK_INT)4 Nullable (androidx.annotation.Nullable)4 ViewModelProvider (androidx.lifecycle.ViewModelProvider)4 AccountImporter (com.nextcloud.android.sso.AccountImporter)4 LockedActivity (it.niedermann.owncloud.notes.LockedActivity)4 ExceptionDialogFragment (it.niedermann.owncloud.notes.exception.ExceptionDialogFragment)4 Note (it.niedermann.owncloud.notes.persistence.entity.Note)4 IResponseCallback (it.niedermann.owncloud.notes.shared.model.IResponseCallback)4 Uri (android.net.Uri)3 SingleSignOnAccount (com.nextcloud.android.sso.model.SingleSignOnAccount)3