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
|
package it.niedermann.nextcloud.deck.ui.card;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_ACCOUNT_ID;
import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_BOARD_ID;
import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_LOCAL_ID;
public class CardAttachmentsFragment extends Fragment {
private Unbinder unbinder;
private static final String TAG = CardAttachmentsFragment.class.getCanonicalName();
private static final int REQUEST_CODE_ADD_ATTACHMENT = 1;
private SyncManager syncManager;
private long accountId;
private long cardId;
@BindView(R.id.coordinatorLayout)
CoordinatorLayout coordinatorLayout;
@BindView(R.id.fab)
FloatingActionButton fab;
@BindView(R.id.attachments_list)
RecyclerView attachmentsList;
@BindView(R.id.no_attachments)
RelativeLayout noAttachments;
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_edit_tab_attachments, container, false);
unbinder = ButterKnife.bind(this, view);
Bundle args = getArguments();
if (args != null) {
accountId = args.getLong(BUNDLE_KEY_ACCOUNT_ID);
cardId = args.getLong(BUNDLE_KEY_LOCAL_ID);
syncManager = new SyncManager(Objects.requireNonNull(getActivity()));
observeOnce(syncManager.getCardByLocalId(accountId, cardId), CardAttachmentsFragment.this, (fullCard) -> {
if (fullCard.getAttachments().size() == 0) {
this.noAttachments.setVisibility(View.VISIBLE);
this.attachmentsList.setVisibility(View.GONE);
} else {
this.noAttachments.setVisibility(View.GONE);
this.attachmentsList.setVisibility(View.VISIBLE);
syncManager.readAccount(accountId).observe(CardAttachmentsFragment.this, (Account account) -> {
RecyclerView.Adapter adapter = new AttachmentAdapter(account, fullCard.getCard().getId(), fullCard.getAttachments());
attachmentsList.setAdapter(adapter);
});
}
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
fab.setOnClickListener(v -> {
Snackbar.make(coordinatorLayout, "Adding attachments is not yet implemented", Snackbar.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
// startActivityForResult(intent, REQUEST_CODE_ADD_ATTACHMENT);
});
fab.show();
attachmentsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0)
fab.hide();
else if (dy < 0)
fab.show();
}
});
}
}
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_ADD_ATTACHMENT && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (data != null) {
uri = data.getData();
Log.i(TAG, "Uri: " + uri.toString());
syncManager.addAttachmentToCard(accountId, cardId, uri);
}
}
}
public CardAttachmentsFragment() {
}
public static CardAttachmentsFragment newInstance(long accountId, long localId, long boardId) {
Bundle bundle = new Bundle();
bundle.putLong(BUNDLE_KEY_ACCOUNT_ID, accountId);
bundle.putLong(BUNDLE_KEY_BOARD_ID, boardId);
bundle.putLong(BUNDLE_KEY_LOCAL_ID, localId);
CardAttachmentsFragment fragment = new CardAttachmentsFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
|