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
|
package it.niedermann.nextcloud.deck.ui.card;
import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
import static it.niedermann.nextcloud.deck.ui.branding.BrandingUtil.applyBrandToPrimaryTabLayout;
import static it.niedermann.nextcloud.deck.ui.branding.BrandingUtil.tintMenuIcon;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import it.niedermann.nextcloud.deck.DeckLog;
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.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.ocs.Version;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.util.CardUtil;
public class EditActivity extends AppCompatActivity {
private static final String BUNDLE_KEY_ACCOUNT = "account";
private static final String BUNDLE_KEY_BOARD_LOCAL_ID = "boardLocalId";
private static final String BUNDLE_KEY_CARD_LOCAL_ID = "cardLocalId";
private ActivityEditBinding binding;
private EditCardViewModel viewModel;
/**
* @deprecated This is only here to maintain compatibility with {@link Version#supportsComments()}
*/
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
};
/**
* @deprecated This is only here to maintain compatibility with {@link Version#supportsComments()}
*/
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());
viewModel = new ViewModelProvider(this).get(EditCardViewModel.class);
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
viewModel.getBrandingColor().observe(this, this::applyBoardBranding);
loadDataFromIntent();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
viewModel.setBrandingColor(ContextCompat.getColor(this, R.color.primary));
setIntent(intent);
loadDataFromIntent();
}
private void loadDataFromIntent() {
final var args = getIntent().getExtras();
if (args == null || !args.containsKey(BUNDLE_KEY_ACCOUNT) || !args.containsKey(BUNDLE_KEY_BOARD_LOCAL_ID) || !args.containsKey(BUNDLE_KEY_CARD_LOCAL_ID)) {
throw new IllegalArgumentException("Provide at least " + BUNDLE_KEY_ACCOUNT + " and " + BUNDLE_KEY_BOARD_LOCAL_ID + " and " + BUNDLE_KEY_CARD_LOCAL_ID + " of the card that should be edited.");
}
final var 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 cardLocalId = args.getLong(BUNDLE_KEY_CARD_LOCAL_ID);
if (cardLocalId <= 0L) {
throw new IllegalArgumentException(BUNDLE_KEY_CARD_LOCAL_ID + " must be a positive long but was " + cardLocalId);
}
final long boardLocalId = args.getLong(BUNDLE_KEY_BOARD_LOCAL_ID);
if (boardLocalId <= 0L) {
throw new IllegalArgumentException(BUNDLE_KEY_BOARD_LOCAL_ID + " must be a positive integer but was " + boardLocalId);
}
observeOnce(viewModel.getFullBoardById(account.getId(), boardLocalId), EditActivity.this, (fullBoard -> {
viewModel.setBrandingColor(fullBoard.getBoard().getColor());
viewModel.setCanEdit(fullBoard.getBoard().isPermissionEdit());
invalidateOptionsMenu();
observeOnce(viewModel.getFullCardWithProjectsByLocalId(account.getId(), cardLocalId), EditActivity.this, (fullCard) -> {
if (fullCard == null) {
new AlertDialog.Builder(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(boardLocalId, fullCard, account.getServerDeckVersionAsObject().isSupported());
invalidateOptionsMenu();
setupViewPager();
setupTitle();
}
});
}));
DeckLog.verbose("Finished loading intent data: { accountId =", viewModel.getAccount().getId(), "cardId =", cardLocalId, "}");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (viewModel.canEdit()) {
getMenuInflater().inflate(R.menu.card_edit_menu, menu);
@ColorInt final int colorAccent = ContextCompat.getColor(this, R.color.accent);
for (int i = 0; i < menu.size(); i++) {
tintMenuIcon(menu.getItem(i), colorAccent);
}
} 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);
}
@Override
public boolean onSupportNavigateUp() {
finish(); // close this activity as oppose to navigating up
return true;
}
/**
* Tries to save the current {@link FullCard} from the {@link EditCardViewModel} and then finishes this activity.
*/
private void saveAndFinish() {
if (!viewModel.isPendingSaveOperation()) {
viewModel.setPendingSaveOperation(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 AlertDialog.Builder(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.setPendingSaveOperation(false))
.show();
} else {
viewModel.saveCard(response -> DeckLog.info("Successfully saved card", response.getCard().getTitle()));
super.finish();
}
}
}
private void setupViewPager() {
binding.tabLayout.removeAllTabs();
binding.tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final var adapter = new CardTabAdapter(this);
final var mediator = new TabLayoutMediator(binding.tabLayout, binding.pager, (tab, position) -> {
tab.setIcon(viewModel.hasCommentsAbility()
? tabIconsWithComments[position]
: tabIcons[position]
);
tab.setContentDescription(viewModel.hasCommentsAbility()
? tabTitlesWithComments[position]
: tabTitles[position]
);
});
binding.pager.setAdapter(adapter);
if (viewModel.hasCommentsAbility()) {
adapter.enableComments();
binding.pager.setOffscreenPageLimit(3);
} else {
binding.pager.setOffscreenPageLimit(2);
}
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()) {
binding.title.setHint(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 void finish() {
if (!viewModel.hasChanges() && viewModel.canEdit()) {
new AlertDialog.Builder(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 {
super.finish();
}
}
/**
* Performs a call of {@link AppCompatActivity#finish()} without checking for changes
*/
public void directFinish() {
super.finish();
}
private void applyBoardBranding(int mainColor) {
final var navigationIcon = binding.toolbar.getNavigationIcon();
if (navigationIcon == null) {
DeckLog.error("Expected navigationIcon to be present.");
} else {
DrawableCompat.setTint(binding.toolbar.getNavigationIcon(), ContextCompat.getColor(this, R.color.accent));
}
applyBrandToPrimaryTabLayout(mainColor, binding.tabLayout);
}
@NonNull
public static Intent createEditCardIntent(@NonNull Context context, @NonNull Account account, long boardLocalId, long cardLocalId) {
return new Intent(context, EditActivity.class)
.putExtra(BUNDLE_KEY_ACCOUNT, account)
.putExtra(BUNDLE_KEY_BOARD_LOCAL_ID, boardLocalId)
.putExtra(BUNDLE_KEY_CARD_LOCAL_ID, cardLocalId)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
|