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

StackFragment.java « stack « 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: 93ebd009364b240a3fb0ddccf2083621d1bcf736 (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
169
170
171
172
173
174
175
176
177
178
package it.niedermann.nextcloud.deck.ui.stack;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import it.niedermann.android.crosstabdnd.DragAndDropTab;
import it.niedermann.nextcloud.deck.DeckApplication;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.api.IResponseCallback;
import it.niedermann.nextcloud.deck.databinding.FragmentStackBinding;
import it.niedermann.nextcloud.deck.model.Card;
import it.niedermann.nextcloud.deck.model.Stack;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import it.niedermann.nextcloud.deck.ui.MainViewModel;
import it.niedermann.nextcloud.deck.ui.card.CardAdapter;
import it.niedermann.nextcloud.deck.ui.card.SelectCardListener;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment;
import it.niedermann.nextcloud.deck.ui.filter.FilterViewModel;
import it.niedermann.nextcloud.deck.ui.movecard.MoveCardListener;

public class StackFragment extends Fragment implements DragAndDropTab<CardAdapter>, MoveCardListener {

    private static final String KEY_STACK_ID = "stackId";

    private FragmentStackBinding binding;
    private MainViewModel mainViewModel;
    private FragmentActivity activity;
    private OnScrollListener onScrollListener;

    @Nullable
    private CardAdapter adapter = null;
    private LiveData<List<FullCard>> cardsLiveData;

    private long stackId;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        final var args = requireArguments();
        if (!args.containsKey(KEY_STACK_ID)) {
            throw new IllegalArgumentException(KEY_STACK_ID + " is required.");
        }

        stackId = args.getLong(KEY_STACK_ID);

        if (context instanceof OnScrollListener) {
            this.onScrollListener = (OnScrollListener) context;
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activity = requireActivity();
        binding = FragmentStackBinding.inflate(inflater, container, false);
        mainViewModel = new ViewModelProvider(activity).get(MainViewModel.class);

        final var filterViewModel = new ViewModelProvider(activity).get(FilterViewModel.class);

        // This might be a zombie fragment with an empty MainViewModel after Android killed the activity (but not the fragment instance
        // See https://github.com/stefan-niedermann/nextcloud-deck/issues/478
        if (mainViewModel.getCurrentAccount() == null) {
            DeckLog.logError(new IllegalStateException("Cannot populate " + StackFragment.class.getSimpleName() + " because mainViewModel.getCurrentAccount() is null"));
            return binding.getRoot();
        }

        adapter = new CardAdapter(requireActivity(), getChildFragmentManager(), stackId, mainViewModel,
                (requireActivity() instanceof SelectCardListener)
                        ? (SelectCardListener) requireActivity()
                        : null);
        binding.recyclerView.setAdapter(adapter);
        binding.loadingSpinner.show();

        if (onScrollListener != null) {
            binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    if (dy > 0)
                        onScrollListener.onScrollDown();
                    else if (dy < 0)
                        onScrollListener.onScrollUp();
                }
            });
        }

        if (!mainViewModel.currentBoardHasEditPermission()) {
            binding.emptyContentView.hideDescription();
        }

        final Observer<List<FullCard>> cardsObserver = (fullCards) -> activity.runOnUiThread(() -> {
            binding.loadingSpinner.hide();
            if (fullCards != null && fullCards.size() > 0) {
                binding.emptyContentView.setVisibility(View.GONE);
                adapter.setCardList(fullCards);
            } else {
                binding.emptyContentView.setVisibility(View.VISIBLE);
            }
        });

        cardsLiveData = mainViewModel.getFullCardsForStack(mainViewModel.getCurrentAccount().getId(), stackId, filterViewModel.getFilterInformation().getValue());
        cardsLiveData.observe(getViewLifecycleOwner(), cardsObserver);

        filterViewModel.getFilterInformation().observe(getViewLifecycleOwner(), (filterInformation -> {
            cardsLiveData.removeObserver(cardsObserver);
            cardsLiveData = mainViewModel.getFullCardsForStack(mainViewModel.getCurrentAccount().getId(), stackId, filterInformation);
            cardsLiveData.observe(getViewLifecycleOwner(), cardsObserver);
        }));

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        DeckApplication.readCurrentBoardColor().observe(getViewLifecycleOwner(), this::applyBrand);
    }

    @Nullable
    @Override
    public CardAdapter getAdapter() {
        return adapter;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return binding.recyclerView;
    }

    private void applyBrand(int mainColor) {
        if (this.adapter != null) {
            this.adapter.applyBrand(mainColor);
        }
    }

    public static Fragment newInstance(long stackId) {
        final var fragment = new StackFragment();

        final var args = new Bundle();
        args.putLong(KEY_STACK_ID, stackId);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void move(long originAccountId, long originCardLocalId, long targetAccountId, long targetBoardLocalId, long targetStackLocalId) {
        mainViewModel.moveCard(originAccountId, originCardLocalId, targetAccountId, targetBoardLocalId, targetStackLocalId, new IResponseCallback<Void>() {
            @Override
            public void onResponse(Void response) {
                DeckLog.log("Moved", Card.class.getSimpleName(), originCardLocalId, "to", Stack.class.getSimpleName(), targetStackLocalId);
            }

            @Override
            public void onError(Throwable throwable) {
                IResponseCallback.super.onError(throwable);
                if (!SyncManager.ignoreExceptionOnVoidError(throwable)) {
                    ExceptionDialogFragment.newInstance(throwable, null).show(getChildFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
                }
            }
        });
    }

}