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
|
package org.fox.ttrss;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
import androidx.viewpager2.widget.ViewPager2;
import org.fox.ttrss.types.GalleryEntry;
import org.fox.ttrss.util.DiffFragmentStateAdapter;
import me.relex.circleindicator.CircleIndicator3;
public class GalleryActivity extends CommonActivity {
private final String TAG = this.getClass().getSimpleName();
protected String m_title;
private ArticleImagesPagerAdapter m_adapter;
public String m_content;
private ViewPager2 m_pager; // TODO replace with viewpager2
private ProgressBar m_checkProgress;
private boolean m_firstWasSelected;
private static class GalleryEntryDiffItemCallback extends DiffUtil.ItemCallback<GalleryEntry> {
@Override
public boolean areItemsTheSame(@NonNull GalleryEntry oldItem, @NonNull GalleryEntry newItem) {
return oldItem.url.equals(newItem.url);
}
@Override
public boolean areContentsTheSame(@NonNull GalleryEntry oldItem, @NonNull GalleryEntry newItem) {
return oldItem.url.equals(newItem.url) && oldItem.type.equals(newItem.type);
}
}
private static class ArticleImagesPagerAdapter extends DiffFragmentStateAdapter<GalleryEntry> {
protected ArticleImagesPagerAdapter(FragmentActivity fragmentActivity, DiffUtil.ItemCallback<GalleryEntry> diffCallback) {
super(fragmentActivity, diffCallback);
}
@Override
public Fragment createFragment(int position) {
GalleryEntry item = getItem(position);
switch (item.type) {
case TYPE_IMAGE: {
GalleryImageFragment frag = new GalleryImageFragment();
frag.initialize(item.url);
return frag;
}
case TYPE_VIDEO:
GalleryVideoFragment frag = new GalleryVideoFragment();
frag.initialize(item.url, item.coverUrl);
return frag;
}
return null;
}
}
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("m_title", m_title);
out.putString("m_content", m_content);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// ActivityCompat.postponeEnterTransition(this);
// we use that before parent onCreate so let's init locally
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
setAppTheme(m_prefs);
super.onCreate(savedInstanceState);
Window window = getWindow();
WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(window, window.getDecorView());
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars());
setContentView(R.layout.activity_gallery);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().hide();
setTitle(m_title);
m_adapter = new ArticleImagesPagerAdapter(this, new GalleryEntryDiffItemCallback());
m_pager = findViewById(R.id.gallery_pager);
m_pager.setAdapter(m_adapter);
m_checkProgress = findViewById(R.id.gallery_check_progress);
if (savedInstanceState == null) {
m_title = getIntent().getStringExtra("title");
m_content = getIntent().getStringExtra("content");
} else {
// ArrayList<GalleryEntry> list = savedInstanceState.getParcelableArrayList("m_items");
m_title = savedInstanceState.getString("m_title");
m_content = savedInstanceState.getString("m_content");
}
GalleryModel model = new ViewModelProvider(this).get(GalleryModel.class);
// this should be dealt with first so that transition completes properly
String firstSrc = getIntent().getStringExtra("firstSrc");
/* but what about videos? if (firstSrc != null) {
List<GalleryEntry> initialItems = new ArrayList<GalleryEntry>();
initialItems.add(0, new GalleryEntry(firstSrc, GalleryEntry.GalleryEntryType.TYPE_IMAGE, null));
m_adapter.submitList(initialItems);
model.update(initialItems);
} */
model.collectItems(m_content, firstSrc);
model.getItemsToCheck().observe(this, itemsToCheck -> {
Log.d(TAG, "observed items to check=" + itemsToCheck);
m_checkProgress.setMax(itemsToCheck);
m_checkProgress.setProgress(0);
});
model.getIsChecking().observe(this, isChecking -> {
Log.d(TAG, "observed isChecking=" + isChecking);
m_checkProgress.setVisibility(isChecking ? View.VISIBLE : View.GONE);
});
model.getCheckProgress().observe(this, progress -> {
Log.d(TAG, "observed item check progress=" + progress);
m_checkProgress.setProgress(progress);
});
CircleIndicator3 indicator = findViewById(R.id.gallery_pager_indicator);
indicator.setViewPager(m_pager);
model.getItems().observe(this, galleryEntries -> {
Log.d(TAG, "observed gallery entries=" + galleryEntries + " firstSrc=" + firstSrc);
m_adapter.submitList(galleryEntries, () -> {
indicator.setViewPager(m_pager);
if (!m_firstWasSelected) {
for (GalleryEntry entry : galleryEntries) {
if (entry.url.equals(firstSrc)) {
int position = galleryEntries.indexOf(entry);
Log.d(TAG, "selecting first src=" + firstSrc + " pos=" + position);
m_pager.setCurrentItem(position);
m_firstWasSelected = true;
break;
}
}
}
});
});
findViewById(R.id.gallery_overflow).setOnClickListener(v -> {
try {
GalleryEntry entry = m_adapter.getCurrentList().get(m_pager.getCurrentItem());
PopupMenu popup = new PopupMenu(GalleryActivity.this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.content_gallery_entry, popup.getMenu());
popup.getMenu().findItem(R.id.article_img_share)
.setVisible(entry.type == GalleryEntry.GalleryEntryType.TYPE_IMAGE);
popup.setOnMenuItemClickListener(item -> onImageMenuItemSelected(item, entry));
popup.show();
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "selecting gallery item", e);
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int position = m_pager.getCurrentItem();
try {
GalleryEntry entry = m_adapter.getCurrentList().get(position);
if (onImageMenuItemSelected(item, entry))
return true;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "gallery menu item selected", e);
}
return super.onContextItemSelected(item);
}
public boolean onImageMenuItemSelected(MenuItem item, GalleryEntry entry) {
String url = entry.url;
int itemId = item.getItemId();
if (itemId == R.id.article_img_open) {
if (url != null) {
try {
openUri(Uri.parse(url));
} catch (Exception e) {
Log.e(TAG, "opening image URL", e);
toast(R.string.error_other_error);
}
}
return true;
} else if (itemId == R.id.article_img_copy) {
if (url != null) {
copyToClipboard(url);
}
return true;
} else if (itemId == R.id.article_img_share) {
if (url != null) {
if (entry.type == GalleryEntry.GalleryEntryType.TYPE_IMAGE) {
Log.d(TAG, "image sharing image from URL=" + url);
shareImageFromUri(url);
}
}
return true;
} else if (itemId == R.id.article_img_share_url) {
if (url != null) {
shareText(url);
}
return true;
} else if (itemId == R.id.article_img_view_caption) {
if (url != null) {
displayImageCaption(url, m_content);
}
return true;
}
Log.d(TAG, "onImageMenuItemSelected, unhandled id=" + item.getItemId());
return false;
}
}
|