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

CardDetailsFragment.java « card « 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: 9c38a5c925cc49fcf68e53c8a86592e4375d0020 (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
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
package it.niedermann.nextcloud.deck.ui.card;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.arch.lifecycle.ViewModelProviders;
import android.content.res.ColorStateList;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.chip.Chip;
import android.support.design.chip.ChipGroup;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.ColorUtils;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import it.niedermann.nextcloud.deck.ColorUtil;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.SupportUtil;
import it.niedermann.nextcloud.deck.databinding.FragmentCardEditTabDetailsBinding;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.model.User;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.viewmodel.FullCardViewModel;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import it.niedermann.nextcloud.deck.ui.widget.DelayedAutoCompleteTextView;

import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_ACCOUNT_ID;
import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_LOCAL_ID;

public class CardDetailsFragment extends Fragment implements DatePickerDialog.OnDateSetListener,
        TimePickerDialog.OnTimeSetListener {
    private static final String TAG = CardDetailsFragment.class.getCanonicalName();

    private FullCardViewModel fullCardViewModel;
    private FullCard card;
    private SyncManager syncManager;
    private DateFormat dateFormat;
    private DateFormat dueTime = new SimpleDateFormat("HH:mm", Locale.ROOT);
    private Unbinder unbinder;

    @BindView(R.id.people)
    DelayedAutoCompleteTextView people;

    @BindView(R.id.peopleList)
    LinearLayout peopleList;

    @BindView(R.id.dueDateDate)
    TextView dueDate;

    @BindView(R.id.dueDateTime)
    TextView dueDateTime;

    @BindView(R.id.clearDueDate)
    ImageView clearDueDate;

    @BindView(R.id.labelsGroup)
    ChipGroup labelsGroup;

    public static CardDetailsFragment newInstance(long accountId, long localId) {
        Bundle bundle = new Bundle();
        bundle.putLong(BUNDLE_KEY_ACCOUNT_ID, accountId);
        bundle.putLong(BUNDLE_KEY_LOCAL_ID, localId);

        CardDetailsFragment fragment = new CardDetailsFragment();
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {


        fullCardViewModel = ViewModelProviders.of(this)
                .get(FullCardViewModel.class);


        FragmentCardEditTabDetailsBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_card_edit_tab_details, container, false);

        binding.setLifecycleOwner(this);
        binding.setEditmodel(fullCardViewModel);

        unbinder = ButterKnife.bind(this, binding.getRoot());
        dateFormat = android.text.format.DateFormat.getDateFormat(getActivity());
        //dueTime = android.text.format.DateFormat.getTimeFormat(getActivity());

        Bundle args = getArguments();
        if (args != null) {
            long accountId = args.getLong(BUNDLE_KEY_ACCOUNT_ID);
            long localId = args.getLong(BUNDLE_KEY_LOCAL_ID);

            setupView(accountId, localId);
        }

        return binding.getRoot();
    }

    private void setupView(long accountId, long localId) {
        syncManager = new SyncManager(getActivity().getApplicationContext(), getActivity());

        this.fullCardViewModel.fullCard = syncManager.getCardByLocalId(accountId, localId);
        this.fullCardViewModel.fullCard.observe(CardDetailsFragment.this, (FullCard card) -> {
            // TODO read/set available card details data
            this.card = card;
            if (this.card != null) {
                // people
                setupPeople(accountId);

                // labels
                setupLabels();

                // due date
                setupDueDate();
            }
        });

        dueDate.setOnClickListener(v -> {
            int year;
            int month;
            int day;

            Calendar cal = Calendar.getInstance();
            if (card.getCard().getDueDate() != null) {
                cal.setTime(card.getCard().getDueDate());
                year = cal.get(Calendar.YEAR);
                month = cal.get(Calendar.MONTH);
                day = cal.get(Calendar.DAY_OF_MONTH);
            } else {
                year = cal.get(Calendar.YEAR);
                month = cal.get(Calendar.MONTH);
                day = cal.get(Calendar.DAY_OF_MONTH);
            }
            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
            datePickerDialog.show();
        });

        dueDateTime.setOnClickListener(v -> {
            int hourOfDay = 0;
            int minutes = 0;

            if (card.getCard().getDueDate() != null) {
                hourOfDay = card.getCard().getDueDate().getHours();
                minutes = card.getCard().getDueDate().getMinutes();
            }
            TimePickerDialog timePickerDialog = new TimePickerDialog(
                    getActivity(), this, hourOfDay, minutes, true);
            timePickerDialog.show();
        });

        clearDueDate.setOnClickListener(v -> {
            this.card.getCard().setDueDate(null);
            syncManager.updateCard(this.card.getCard());
        });
    }

    private void setupDueDate() {
        if (this.card.getCard().getDueDate() != null) {
            dueDate.setText(dateFormat.format(this.card.getCard().getDueDate()));
            dueDateTime.setText(dueTime.format(this.card.getCard().getDueDate()));
        } else {
            dueDate.setText(null);
            dueDateTime.setText(null);
        }
    }

    private void setupLabels() {
        labelsGroup.removeAllViews();
        if (this.card.getLabels() != null && this.card.getLabels().size() > 0) {
            Chip chip;
            for (Label label : this.card.getLabels()) {
                chip = new Chip(getActivity());
                chip.setText(label.getTitle());
                // TODO use grey/white icon depending on textTinting
                chip.setCloseIcon(getContext().getResources().getDrawable(R.drawable.ic_close_circle_grey600));
                chip.setCloseIconVisible(true);
                try {
                    int labelColor = Color.parseColor("#" + label.getColor());
                    ColorStateList c = ColorStateList.valueOf(labelColor);
                    chip.setChipBackgroundColor(c);
                    int color = ColorUtil.getForegroundColorForBackgroundColor(labelColor);
                    chip.setTextColor(color);

                    Drawable wrapDrawable = DrawableCompat.wrap(chip.getCloseIcon());
                    DrawableCompat.setTint(wrapDrawable, ColorUtils.setAlphaComponent(color, 150));
                } catch (IllegalArgumentException e) {
                    Log.e(TAG, "error parsing label color", e);
                }

                labelsGroup.addView(chip);
            }
            labelsGroup.setVisibility(View.VISIBLE);
        } else {
            labelsGroup.setVisibility(View.GONE);
        }
    }

    private void setupPeople(long accountId) {
        people.setThreshold(2);
        people.setAdapter(new UserAutoCompleteAdapter(this, getContext(), accountId));
        people.setOnItemClickListener((adapterView, view, position, id) -> {
            User user = (User) adapterView.getItemAtPosition(position);
            people.setText(user.getDisplayname());
            // TODO: store chosen user, trigger avatar display/fetch
        });

        // TODO implement proper people display + avatar fetching
        // TODO find out how to get the server's Nextcloud URL to build the avatar URL
        if (this.card.getAssignedUsers() != null) {
            try {
                // TODO FIX: NullPointerException!
                // syncManager.getServerUrl()

                //Workaround
                SingleSignOnAccount account = SingleAccountHelper.getCurrentSingleSignOnAccount(getContext());
                ImageView avatar;
                String baseUrl = account.url;
                int px = SupportUtil.getAvatarDimension(getContext());
                int margin = SupportUtil.dpToPx(getContext(), 8);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(px, px);
                params.setMargins(
                        0, 0, margin, 0);
                peopleList.removeAllViews();
                for (User user : this.card.getAssignedUsers()) {
                    avatar = new ImageView(getActivity());
                    avatar.setLayoutParams(params);
                    String uri = baseUrl + "/index.php/avatar/" + Uri.encode(user.getUid()) + "/" + px;
                    peopleList.addView(avatar);
                    avatar.requestLayout();
                    Glide.with(this)
                            .load(uri)
                            .apply(RequestOptions.circleCropTransform())
                            .into(avatar);
                }
            } catch (NextcloudFilesAppAccountNotFoundException e) {
                DeckLog.logError(e);
            } catch (NoCurrentAccountSelectedException e) {
                DeckLog.logError(e);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

        Calendar c = Calendar.getInstance();
        int hourOfDay;
        int minute;

        if (dueDateTime.getText() != null && dueDateTime.length() > 0) {
            hourOfDay = this.card.getCard().getDueDate().getHours();
            minute = this.card.getCard().getDueDate().getMinutes();
        } else {
            hourOfDay = 0;
            minute = 0;
        }

        c.set(year, month, dayOfMonth, hourOfDay, minute);
        this.card.getCard().setDueDate(c.getTime());
        dueDate.setText(dateFormat.format(c.getTime()));
        syncManager.updateCard(this.card.getCard());
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        this.card.getCard().getDueDate().setHours(hourOfDay);
        this.card.getCard().getDueDate().setMinutes(minute);
        dueDateTime.setText(dueTime.format(this.card.getCard().getDueDate().getTime()));
        syncManager.updateCard(this.card.getCard());
    }
}