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

MoveCardDialogFragment.java « movecard « 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: 97a011398cfac769eea56197575de7359f7ad1a7 (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
package it.niedermann.nextcloud.deck.ui.movecard;

import android.content.Context;
import android.content.res.ColorStateList;
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.DialogFragment;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.DialogMoveCardBinding;
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.model.full.FullStack;
import it.niedermann.nextcloud.deck.ui.branding.BrandedDialogFragment;
import it.niedermann.nextcloud.deck.ui.branding.BrandingUtil;
import it.niedermann.nextcloud.deck.ui.pickstack.PickStackFragment;
import it.niedermann.nextcloud.deck.ui.pickstack.PickStackListener;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

public class MoveCardDialogFragment extends BrandedDialogFragment implements PickStackListener {

    private static final String KEY_ORIGIN_ACCOUNT_ID = "account_id";
    private static final String KEY_ORIGIN_BOARD_LOCAL_ID = "board_local_id";
    private static final String KEY_ORIGIN_CARD_LOCAL_ID = "card_local_id";
    private Long originAccountId;
    private Long originBoardLocalId;
    private Long originCardLocalId;

    private DialogMoveCardBinding binding;
    private PickStackFragment fragment;
    private MoveCardListener moveCardListener;

    private Account selectedAccount;
    private Board selectedBoard;
    private FullStack selectedStack;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (getParentFragment() instanceof MoveCardListener) {
            this.moveCardListener = (MoveCardListener) getParentFragment();
        } else if (context instanceof MoveCardListener) {
            this.moveCardListener = (MoveCardListener) context;
        } else {
            throw new IllegalArgumentException("Caller must implement " + MoveCardListener.class.getSimpleName());
        }

        final Bundle args = requireArguments();
        originAccountId = args.getLong(KEY_ORIGIN_ACCOUNT_ID, -1L);
        if (originAccountId < 0) {
            throw new IllegalArgumentException("Missing " + KEY_ORIGIN_ACCOUNT_ID);
        }
        originCardLocalId = args.getLong(KEY_ORIGIN_CARD_LOCAL_ID, -1L);
        if (originCardLocalId < 0) {
            throw new IllegalArgumentException("Missing " + KEY_ORIGIN_CARD_LOCAL_ID);
        }
        originBoardLocalId = args.getLong(KEY_ORIGIN_BOARD_LOCAL_ID, -1L);
        if (originBoardLocalId < 0) {
            throw new IllegalArgumentException("Missing " + KEY_ORIGIN_BOARD_LOCAL_ID);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogMoveCardBinding.inflate(inflater);
        binding.submit.setOnClickListener((v) -> {
            DeckLog.verbose("[Move card] Attempt to move to " + Stack.class.getSimpleName() + " #" + selectedStack.getLocalId());
            this.moveCardListener.move(originAccountId, originCardLocalId, selectedAccount.getId(), selectedBoard.getLocalId(), selectedStack.getLocalId());
            dismiss();
        });
        binding.cancel.setOnClickListener((v) -> dismiss());
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        fragment = new PickStackFragment();
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }

    @Override
    public void onStackPicked(@NonNull Account account, @Nullable Board board, @Nullable FullStack fullStack) {
        this.selectedAccount = account;
        this.selectedBoard = board;
        this.selectedStack = fullStack;
        if (board == null || fullStack == null) {
            binding.submit.setEnabled(false);
            binding.moveWarning.setVisibility(GONE);
        } else {
            binding.submit.setEnabled(true);
            binding.moveWarning.setVisibility(board.getLocalId().equals(originBoardLocalId) ? GONE : VISIBLE);
        }
    }

    @Override
    public void applyBrand(int mainColor) {
        final ColorStateList mainColorStateList = ColorStateList.valueOf(BrandingUtil.getSecondaryForegroundColorDependingOnTheme(requireContext(), mainColor));
        binding.cancel.setTextColor(mainColorStateList);
        binding.submit.setTextColor(mainColorStateList);
    }

    public static DialogFragment newInstance(long originAccountId, long originBoardLocalId, Long originCardLocalId) {
        final DialogFragment dialogFragment = new MoveCardDialogFragment();
        final Bundle args = new Bundle();
        args.putLong(KEY_ORIGIN_ACCOUNT_ID, originAccountId);
        args.putLong(KEY_ORIGIN_BOARD_LOCAL_ID, originBoardLocalId);
        args.putLong(KEY_ORIGIN_CARD_LOCAL_ID, originCardLocalId);
        dialogFragment.setArguments(args);
        return dialogFragment;
    }
}