Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PickStackActivity.java « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a024f12909877526af3e0be5fd29e1196e3152c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package it.niedermann.nextcloud.deck.ui;

import static androidx.lifecycle.Transformations.switchMap;
import static it.niedermann.nextcloud.deck.DeckApplication.isDarkTheme;
import static it.niedermann.nextcloud.deck.ui.branding.BrandingUtil.getSecondaryForegroundColorDependingOnTheme;
import static it.niedermann.nextcloud.deck.util.DeckColorUtil.contrastRatioIsSufficientBigAreas;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.lifecycle.ViewModelProvider;

import java.util.List;

import it.niedermann.android.util.ColorUtil;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.api.IResponseCallback;
import it.niedermann.nextcloud.deck.databinding.ActivityPickStackBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Board;
import it.niedermann.nextcloud.deck.model.Stack;
import it.niedermann.nextcloud.deck.ui.branding.Branded;
import it.niedermann.nextcloud.deck.ui.branding.BrandingUtil;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.ui.pickstack.PickStackFragment;
import it.niedermann.nextcloud.deck.ui.pickstack.PickStackListener;
import it.niedermann.nextcloud.deck.ui.pickstack.PickStackViewModel;

public abstract class PickStackActivity extends AppCompatActivity implements Branded, PickStackListener {

    private ActivityPickStackBinding binding;
    private PickStackViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

        binding = ActivityPickStackBinding.inflate(getLayoutInflater());
        viewModel = new ViewModelProvider(this).get(PickStackViewModel.class);

        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);

        switchMap(viewModel.hasAccounts(), hasAccounts -> {
            if (hasAccounts) {
                return viewModel.readAccounts();
            } else {
                startActivityForResult(ImportAccountActivity.createIntent(this), ImportAccountActivity.REQUEST_CODE_IMPORT_ACCOUNT);
                return null;
            }
        }).observe(this, (List<Account> accounts) -> {
            if (accounts == null || accounts.size() == 0) {
                throw new IllegalStateException("hasAccounts() returns true, but readAccounts() returns null or has no entry");
            }
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, PickStackFragment.newInstance(showBoardsWithoutEditPermission()))
                    .commit();
        });
        binding.cancel.setOnClickListener((v) -> finish());
        binding.submit.setOnClickListener((v) -> {
            viewModel.setSubmitInProgress(true);
            onSubmit(viewModel.getAccount(), viewModel.getBoardLocalId(), viewModel.getStackLocalId(), new IResponseCallback<>() {
                @Override
                public void onResponse(Void response) {
                    runOnUiThread(() -> viewModel.setSubmitInProgress(false));
                }

                @Override
                public void onError(Throwable throwable) {
                    IResponseCallback.super.onError(throwable);
                    runOnUiThread(() -> {
                        viewModel.setSubmitInProgress(false);
                        ExceptionDialogFragment
                                .newInstance(throwable, viewModel.getAccount())
                                .show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
                    });
                }
            });
        });
        viewModel.submitButtonEnabled().observe(this, (enabled) -> binding.submit.setEnabled(enabled));
        if (requireContent()) {
            viewModel.setContentIsSatisfied(false);
            binding.inputWrapper.setVisibility(View.VISIBLE);
            binding.input.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // Nothing to do here...
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    viewModel.setContentIsSatisfied(s != null && !s.toString().trim().isEmpty());
                }

                @Override
                public void afterTextChanged(Editable s) {
                    // Nothing to do here...
                }
            });
        } else {
            viewModel.setContentIsSatisfied(true);
        }
    }

    @Override
    public void onStackPicked(@NonNull Account account, @Nullable Board board, @Nullable Stack stack) {
        viewModel.setSelected(account, board, stack);
        applyBrand(board == null
                ? ContextCompat.getColor(this, R.color.accent)
                : board.getColor()
        );
    }

    @Override
    public void applyBrand(int mainColor) {
        try {
            @ColorInt final int finalMainColor = contrastRatioIsSufficientBigAreas(mainColor, ContextCompat.getColor(this, R.color.primary))
                    ? mainColor
                    : isDarkTheme(this) ? Color.WHITE : Color.BLACK;
            DrawableCompat.setTintList(binding.submit.getBackground(), ColorStateList.valueOf(finalMainColor));
            binding.submit.setTextColor(ColorUtil.INSTANCE.getForegroundColorForBackgroundColor(finalMainColor));
            binding.cancel.setTextColor(getSecondaryForegroundColorDependingOnTheme(this, mainColor));
            BrandingUtil.applyBrandToEditTextInputLayout(mainColor, binding.inputWrapper);
        } catch (Throwable t) {
            DeckLog.logError(t);
        }
    }

    abstract protected void onSubmit(Account account, long boardLocalId, long stackId, @NonNull IResponseCallback<Void> callback);

    abstract protected boolean showBoardsWithoutEditPermission();

    protected boolean requireContent() {
        return false;
    }

    @NonNull
    protected String getContent() {
        final Editable text = binding.input.getText();
        return text == null ? "" : text.toString();
    }
}