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

ArchivedBoardsActivity.java « archivedboards « 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: 58237dbd5a52b32f96cfe38bd5de15d58fc3f77e (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
158
159
160
161
162
163
164
165
166
167
168
package it.niedermann.nextcloud.deck.ui.archivedboards;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import it.niedermann.android.reactivelivedata.ReactiveLiveData;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.databinding.ActivityArchivedBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Board;
import it.niedermann.nextcloud.deck.model.full.FullBoard;
import it.niedermann.nextcloud.deck.remote.api.IResponseCallback;
import it.niedermann.nextcloud.deck.repository.SyncRepository;
import it.niedermann.nextcloud.deck.ui.board.ArchiveBoardListener;
import it.niedermann.nextcloud.deck.ui.board.DeleteBoardListener;
import it.niedermann.nextcloud.deck.ui.board.edit.EditBoardListener;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.ui.theme.ThemeUtils;
import it.niedermann.nextcloud.deck.ui.theme.Themed;
import it.niedermann.nextcloud.deck.ui.viewmodel.SyncViewModel;

public class ArchivedBoardsActivity extends AppCompatActivity implements Themed, DeleteBoardListener, EditBoardListener, ArchiveBoardListener {

    private static final String KEY_ACCOUNT = "account";
    private ArchivedBoardsViewModel archivedBoardsViewModel;
    private ActivityArchivedBinding binding;
    private ArchivedBoardsAdapter adapter;
    private Account account;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

        final var args = getIntent().getExtras();

        if (args == null || !args.containsKey(KEY_ACCOUNT)) {
            throw new IllegalArgumentException("Provide at least " + KEY_ACCOUNT);
        }

        account = (Account) args.getSerializable(KEY_ACCOUNT);
        binding = ActivityArchivedBinding.inflate(getLayoutInflater());

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

        applyTheme(account.getColor());

        archivedBoardsViewModel = new SyncViewModel.Provider(this, getApplication(), account).get(ArchivedBoardsViewModel.class);

        adapter = new ArchivedBoardsAdapter(account, getSupportFragmentManager(), this::onArchive);
        binding.recyclerView.setAdapter(adapter);

        final var archivedBoards$ = new ReactiveLiveData<>(archivedBoardsViewModel.getArchivedBoards(account.getId()));

        archivedBoards$
                .filter(boards -> boards.size() == 0)
                .distinctUntilChanged()
                .observe(this, this::finish);

        archivedBoards$
                .filter(boards -> boards.size() > 0)
                .distinctUntilChanged()
                .observe(this, adapter::setBoards);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.binding = null;
    }

    @Override
    public void onBoardDeleted(Board board) {
        archivedBoardsViewModel.deleteBoard(board, new IResponseCallback<>() {
            @Override
            public void onResponse(Void response) {
                DeckLog.info("Successfully deleted board", board.getTitle());
            }

            @Override
            public void onError(Throwable throwable) {
                if (SyncRepository.isNoOnVoidError(throwable)) {
                    IResponseCallback.super.onError(throwable);
                    showExceptionDialog(throwable, account);
                }
            }
        });
    }

    @Override
    public void onUpdateBoard(FullBoard fullBoard) {
        archivedBoardsViewModel.updateBoard(fullBoard, new IResponseCallback<>() {
            @Override
            public void onResponse(FullBoard response) {
                DeckLog.info("Successfully updated board", fullBoard.getBoard().getTitle());
            }

            @Override
            public void onError(Throwable throwable) {
                IResponseCallback.super.onError(throwable);
                showExceptionDialog(throwable, account);
            }
        });
    }

    @Override
    public void onArchive(Board board) {
        archivedBoardsViewModel.dearchiveBoard(board, new IResponseCallback<>() {
            @Override
            public void onResponse(FullBoard response) {
                DeckLog.info("Successfully dearchived board", response.getBoard().getTitle());
            }

            @Override
            public void onError(Throwable throwable) {
                IResponseCallback.super.onError(throwable);
                showExceptionDialog(throwable, account);
            }
        });
    }

    @Override
    public void onClone(@NonNull Account account, @NonNull Board board) {
        throw new IllegalStateException("Cloning boards is not available at " + ArchivedBoardsActivity.class.getSimpleName());
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish(); // close this activity as oppose to navigating up
        return true;
    }

    @AnyThread
    private void showExceptionDialog(@NonNull Throwable throwable, @Nullable Account account) {
        ExceptionDialogFragment
                .newInstance(throwable, account)
                .show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

    }

    @Override
    public void applyTheme(int color) {
        final var utils = ThemeUtils.of(color, this);

        utils.platform.themeStatusBar(this);
        utils.material.themeToolbar(binding.toolbar);
    }

    @NonNull
    public static Intent createIntent(@NonNull Context context, @NonNull Account account) {
        return new Intent(context, ArchivedBoardsActivity.class)
                .putExtra(KEY_ACCOUNT, account)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
}