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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
package it.niedermann.nextcloud.deck.ui.card;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.ActivityEditBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import it.niedermann.nextcloud.deck.ui.branding.BrandedActivity;
import it.niedermann.nextcloud.deck.ui.branding.BrandedAlertDialogBuilder;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.util.CardUtil;
import static android.graphics.Color.parseColor;
import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
public class EditActivity extends BrandedActivity {
private static final String BUNDLE_KEY_ACCOUNT = "account";
private static final String BUNDLE_KEY_BOARD_ID = "boardId";
private static final String BUNDLE_KEY_STACK_ID = "stackId";
private static final String BUNDLE_KEY_CARD_ID = "cardId";
private static final String BUNDLE_KEY_TITLE = "title";
private ActivityEditBinding binding;
private EditCardViewModel viewModel;
private SyncManager syncManager;
private static final int[] tabTitles = new int[]{
R.string.card_edit_details,
R.string.card_edit_attachments,
R.string.card_edit_activity
};
private static final int[] tabTitlesWithComments = new int[]{
R.string.card_edit_details,
R.string.card_edit_attachments,
R.string.card_edit_comments,
R.string.card_edit_activity
};
private static final int[] tabIcons = new int[]{
R.drawable.ic_home_grey600_24dp,
R.drawable.ic_attach_file_grey600_24dp,
R.drawable.ic_activity_light_grey
};
private static final int[] tabIconsWithComments = new int[]{
R.drawable.ic_home_grey600_24dp,
R.drawable.ic_attach_file_grey600_24dp,
R.drawable.type_comment_grey600_36dp,
R.drawable.ic_activity_light_grey
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this));
binding = ActivityEditBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
viewModel = new ViewModelProvider(this).get(EditCardViewModel.class);
syncManager = new SyncManager(this);
loadDataFromIntent();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
loadDataFromIntent();
applyBrand(parseColor(viewModel.getAccount().getColor()));
}
private void loadDataFromIntent() {
final Bundle args = getIntent().getExtras();
if (args == null || !args.containsKey(BUNDLE_KEY_ACCOUNT) || !args.containsKey(BUNDLE_KEY_BOARD_ID)) {
throw new IllegalArgumentException("Provide at least " + BUNDLE_KEY_ACCOUNT + " and " + BUNDLE_KEY_BOARD_ID + " of the card that should be edited or created.");
}
long cardId = args.getLong(BUNDLE_KEY_CARD_ID);
if (cardId == 0L) {
viewModel.setCreateMode(true);
if (!args.containsKey(BUNDLE_KEY_STACK_ID)) {
throw new IllegalArgumentException("When creating a card, passing the " + BUNDLE_KEY_STACK_ID + " is mandatory");
}
}
final Account account = (Account) args.getSerializable(BUNDLE_KEY_ACCOUNT);
if (account == null) {
throw new IllegalArgumentException(BUNDLE_KEY_ACCOUNT + " must not be null.");
}
viewModel.setAccount(account);
final long boardId = args.getLong(BUNDLE_KEY_BOARD_ID);
observeOnce(syncManager.getFullBoardById(account.getId(), boardId), EditActivity.this, (fullBoard -> {
viewModel.setCanEdit(fullBoard.getBoard().isPermissionEdit());
invalidateOptionsMenu();
if (viewModel.isCreateMode()) {
viewModel.initializeNewCard(boardId, args.getLong(BUNDLE_KEY_STACK_ID), account.getServerDeckVersionAsObject().isSupported(this));
invalidateOptionsMenu();
String title = args.getString(BUNDLE_KEY_TITLE);
if (!TextUtils.isEmpty(title)) {
if (title.length() > viewModel.getAccount().getServerDeckVersionAsObject().getCardTitleMaxLength()) {
viewModel.getFullCard().getCard().setDescription(title);
} else {
viewModel.getFullCard().getCard().setTitle(title);
}
}
setupViewPager();
setupTitle();
} else {
observeOnce(syncManager.getCardByLocalId(account.getId(), cardId), EditActivity.this, (fullCard) -> {
if (fullCard == null) {
new BrandedAlertDialogBuilder(this)
.setTitle(R.string.card_not_found)
.setMessage(R.string.card_not_found_message)
.setPositiveButton(R.string.simple_close, (a, b) -> super.finish())
.show();
} else {
viewModel.initializeExistingCard(boardId, fullCard, account.getServerDeckVersionAsObject().isSupported(this));
invalidateOptionsMenu();
setupViewPager();
setupTitle();
}
});
}
}));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (viewModel.canEdit()) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.card_edit_menu, menu);
} else {
menu.clear();
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_card_save) {
saveAndFinish();
}
return super.onOptionsItemSelected(item);
}
private void saveAndFinish() {
if (!viewModel.isPendingCreation()) {
viewModel.setPendingCreation(true);
final String title = viewModel.getFullCard().getCard().getTitle();
if (title == null || title.trim().isEmpty()) {
viewModel.getFullCard().getCard().setTitle(CardUtil.generateTitleFromDescription(viewModel.getFullCard().getCard().getDescription()));
}
viewModel.getFullCard().getCard().setTitle(viewModel.getFullCard().getCard().getTitle().trim());
binding.title.setText(viewModel.getFullCard().getCard().getTitle());
if (viewModel.getFullCard().getCard().getTitle().isEmpty()) {
new BrandedAlertDialogBuilder(this)
.setTitle(R.string.title_is_mandatory)
.setMessage(R.string.provide_at_least_a_title_or_description)
.setPositiveButton(android.R.string.ok, null)
.setOnDismissListener(dialog -> viewModel.setPendingCreation(false))
.show();
} else {
if (viewModel.isCreateMode()) {
observeOnce(syncManager.createFullCard(viewModel.getAccount().getId(), viewModel.getBoardId(), viewModel.getFullCard().getCard().getStackId(), viewModel.getFullCard()), EditActivity.this, (card) -> super.finish());
} else {
observeOnce(syncManager.updateCard(viewModel.getFullCard()), EditActivity.this, (card) -> super.finish());
}
}
}
}
private void setupViewPager() {
binding.tabLayout.removeAllTabs();
binding.tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final CardTabAdapter adapter = new CardTabAdapter(getSupportFragmentManager(), getLifecycle());
final TabLayoutMediator mediator = new TabLayoutMediator(binding.tabLayout, binding.pager, (tab, position) -> {
tab.setIcon(!viewModel.isCreateMode() && viewModel.hasCommentsAbility()
? tabIconsWithComments[position]
: tabIcons[position]
);
tab.setContentDescription(!viewModel.isCreateMode() && viewModel.hasCommentsAbility()
? tabTitlesWithComments[position]
: tabTitles[position]
);
});
runOnUiThread(() -> {
binding.pager.setOffscreenPageLimit(2);
binding.pager.setAdapter(adapter);
mediator.attach();
});
if (!viewModel.isCreateMode() && viewModel.hasCommentsAbility()) {
runOnUiThread(() -> {
mediator.detach();
adapter.enableComments();
binding.pager.setOffscreenPageLimit(3);
mediator.attach();
});
}
}
private void setupTitle() {
binding.title.setText(viewModel.getFullCard().getCard().getTitle());
binding.title.setFilters(new InputFilter[]{new InputFilter.LengthFilter(viewModel.getAccount().getServerDeckVersionAsObject().getCardTitleMaxLength())});
if (viewModel.canEdit()) {
if (viewModel.isCreateMode()) {
binding.title.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
if (viewModel.getFullCard().getCard().getTitle() != null) {
binding.title.setSelection(viewModel.getFullCard().getCard().getTitle().length());
}
}
binding.title.setHint(getString(viewModel.isCreateMode() ? R.string.simple_add : R.string.edit));
binding.title.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
viewModel.getFullCard().getCard().setTitle(binding.title.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
} else {
binding.title.setEnabled(false);
}
}
@Override
public boolean onSupportNavigateUp() {
finish(); // close this activity as oppose to navigating up
return true;
}
@Override
public void onBackPressed() {
finish();
}
@Override
public void finish() {
if (!viewModel.hasChanges() && viewModel.canEdit()) {
new BrandedAlertDialogBuilder(this)
.setTitle(R.string.simple_save)
.setMessage(R.string.do_you_want_to_save_your_changes)
.setPositiveButton(R.string.simple_save, (dialog, whichButton) -> saveAndFinish())
.setNegativeButton(R.string.simple_discard, (dialog, whichButton) -> super.finish()).show();
} else {
directFinish();
}
}
/**
* Performs a call of {@link AppCompatActivity#finish()} without checking for changes
*/
public void directFinish() {
super.finish();
}
@Override
public void applyBrand(int mainColor) {
applyBrandToPrimaryToolbar(mainColor, binding.toolbar);
applyBrandToPrimaryTabLayout(mainColor, binding.tabLayout);
}
@NonNull
public static Intent createNewCardIntent(@NonNull Context context, @NonNull Account account, Long boardId, Long stackId, @NonNull String title) {
return createNewCardIntent(context, account, boardId, stackId)
.putExtra(BUNDLE_KEY_TITLE, title);
}
@NonNull
public static Intent createNewCardIntent(@NonNull Context context, @NonNull Account account, Long boardId, Long stackId) {
return createBasicIntent(context, account, boardId)
.putExtra(BUNDLE_KEY_STACK_ID, stackId);
}
@NonNull
public static Intent createEditCardIntent(@NonNull Context context, @NonNull Account account, Long boardId, Long cardId) {
return createBasicIntent(context, account, boardId)
.putExtra(BUNDLE_KEY_CARD_ID, cardId);
}
private static Intent createBasicIntent(@NonNull Context context, @NonNull Account account, Long boardId) {
return new Intent(context, EditActivity.class)
.putExtra(BUNDLE_KEY_ACCOUNT, account)
.putExtra(BUNDLE_KEY_BOARD_ID, boardId)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
|