blob: 56350be74177b123388165b01c6ca794b74200ab (
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
|
package it.niedermann.owncloud.notes.accountpicker;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.databinding.DialogChooseAccountBinding;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.account.AccountChooserAdapter;
import it.niedermann.owncloud.notes.shared.account.AccountChooserViewHolder;
/**
* A {@link DialogFragment} which provides an {@link Account} chooser that hides the current {@link Account}.
* This can be useful when one wants to pick e. g. a target for move a {@link Note} from one {@link Account} to another..
*/
public class AccountPickerDialogFragment extends BrandedDialogFragment {
private static final String PARAM_TARGET_ACCOUNTS = "targetAccounts";
private static final String PARAM_CURRENT_ACCOUNT_ID = "currentAccountId";
private AccountPickerListener accountPickerListener;
private List<Account> targetAccounts;
/**
* Use newInstance()-Method
*/
public AccountPickerDialogFragment() {
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof AccountPickerListener) {
this.accountPickerListener = (AccountPickerListener) context;
} else {
throw new ClassCastException("Caller must implement " + AccountPickerListener.class.getSimpleName());
}
final var args = requireArguments();
if (!args.containsKey(PARAM_TARGET_ACCOUNTS)) {
throw new IllegalArgumentException(PARAM_TARGET_ACCOUNTS + " is required.");
}
final var accounts = (Collection<?>) args.getSerializable(PARAM_TARGET_ACCOUNTS);
if (accounts == null) {
throw new IllegalArgumentException(PARAM_TARGET_ACCOUNTS + " is required.");
}
final long currentAccountId = requireArguments().getLong(PARAM_CURRENT_ACCOUNT_ID, -1L);
targetAccounts = accounts
.stream()
.map(a -> (Account) a)
.filter(a -> a.getId() != currentAccountId)
.collect(Collectors.toList());
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final var dialogBuilder = new BrandedAlertDialogBuilder(requireActivity())
.setTitle(R.string.simple_move)
.setNegativeButton(android.R.string.cancel, null);
if (targetAccounts.size() > 0) {
final var binding = DialogChooseAccountBinding.inflate(LayoutInflater.from(requireContext()));
final var adapter = new AccountChooserAdapter(targetAccounts, (account -> {
accountPickerListener.onAccountPicked(account);
dismiss();
}));
binding.accountsList.setAdapter(adapter);
dialogBuilder.setView(binding.getRoot());
} else {
dialogBuilder.setMessage(getString(R.string.no_other_accounts));
}
return dialogBuilder.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Objects.requireNonNull(requireDialog().getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return super.onCreateView(inflater, container, savedInstanceState);
}
public static DialogFragment newInstance(@NonNull ArrayList<Account> targetAccounts, long currentAccountId) {
final var fragment = new AccountPickerDialogFragment();
final var args = new Bundle();
args.putSerializable(PARAM_TARGET_ACCOUNTS, targetAccounts);
args.putLong(PARAM_CURRENT_ACCOUNT_ID, currentAccountId);
fragment.setArguments(args);
return fragment;
}
@Override
public void applyBrand(int mainColor, int textColor) {
// Nothing to do...
}
}
|