Skip to content

Commit f65afab

Browse files
authored
Version 2.1.0 (#13)
* improve parsing script * 2.0.3 * autoplay option for animated GIFs * protect `checkLength`
1 parent 0889f7a commit f65afab

21 files changed

+118
-12
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "io.github.chiver"
88
minSdkVersion 24
99
targetSdkVersion 28
10-
versionCode 202
11-
versionName "2.0.2"
10+
versionCode 210
11+
versionName "2.1.0"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/src/main/assets/script.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
document.addEventListener('DOMContentLoaded', (event) => {
2-
setTimeout(function(){
2+
3+
var attempts = 0;
4+
var limit = 5;
5+
var checkLength=2;
6+
7+
try {
8+
var expected = CHIVE_GALLERY_ITEMS.items.length;
9+
if (expected > checkLength){
10+
checkLength = expected;
11+
}
12+
console.info("Chiver is expecting " + checkLength + " items");
13+
} catch (e) {}
14+
15+
var parse = function() {
316
console.info("Chiver is parsing :)");
17+
418
var nodes = document.querySelectorAll('div.gallery-icon > img,div.gallery-icon > video > source');
19+
20+
if (nodes.length < checkLength && attempts < limit) {
21+
attempts++;
22+
setTimeout(parse, 500);
23+
return;
24+
}
25+
526
for(var i=0;i<nodes.length; i++){
627
var a = nodes[i];
728
var url = a.getAttribute("src");
@@ -17,5 +38,9 @@ document.addEventListener('DOMContentLoaded', (event) => {
1738
}
1839
}
1940
wai.notifyDataSetChanged();
41+
}
42+
43+
setTimeout(function(){
44+
parse();
2045
}, 500);
2146
});

app/src/main/java/io/github/chiver/BaseActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
6363
return onRefresh(item);
6464
case R.id.mi_share:
6565
return onShare(item);
66+
case R.id.mi_autoplay:
67+
return onAutoplay(item);
6668
case R.id.mi_browse:
6769
return onBrowse(item);
6870
case R.id.mi_info:
@@ -93,4 +95,8 @@ boolean onBrowse(MenuItem item) {
9395
return true;
9496
}
9597

98+
boolean onAutoplay(MenuItem item) {
99+
return true;
100+
}
101+
96102
}

app/src/main/java/io/github/chiver/Chiver.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.chiver;
22

33
import android.app.Application;
4+
import android.content.SharedPreferences;
45
import android.util.Log;
56

67
import com.android.volley.RequestQueue;
@@ -17,10 +18,13 @@
1718
@SuppressWarnings("WeakerAccess")
1819
public class Chiver extends Application {
1920

21+
private static final String PREF_AUTOPLAY = "autoplay";
22+
2023
private SimpleDiskCache simpleDiskCache;
2124
private RequestQueue requestQueue;
2225
private String script;
2326
private String parser;
27+
private SharedPreferences preferences;
2428

2529
public Chiver() {
2630
}
@@ -36,6 +40,7 @@ public void onCreate() {
3640

3741
StringRequest stringRequest = new StringRequest(Constants.REMOTE_SCRIPT_URL, response -> script = response, error -> Log.i(Constants.TAG, "Can't load remote script, using local asset"));
3842
getRequestQueue().add(stringRequest);
43+
preferences = getSharedPreferences(getClass().getSimpleName(), MODE_PRIVATE);
3944
}
4045

4146
public SimpleDiskCache getSimpleDiskCache() {
@@ -66,4 +71,14 @@ public synchronized RequestQueue getRequestQueue() {
6671
return this.requestQueue;
6772

6873
}
74+
75+
public void setAutoplayEnabled(boolean enabled) {
76+
SharedPreferences.Editor editor = preferences.edit();
77+
editor.putBoolean(PREF_AUTOPLAY, enabled);
78+
editor.apply();
79+
}
80+
81+
public boolean isAutoplayEnabled() {
82+
return preferences.getBoolean(PREF_AUTOPLAY, false);
83+
}
6984
}

app/src/main/java/io/github/chiver/GalleryActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected void _onCreate(Bundle savedInstanceState) {
5858
@Override
5959
protected void _onCreateOptionsMenu(Menu menu) {
6060
menu.findItem(R.id.mi_refresh).setVisible(false);
61+
menu.findItem(R.id.mi_autoplay).setVisible(false);
6162
menu.findItem(R.id.mi_share).setVisible(false);
6263
menu.findItem(R.id.mi_browse).setVisible(true);
6364
}

app/src/main/java/io/github/chiver/ItemActivity.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.view.animation.Animation;
99
import android.view.animation.AnimationUtils;
1010
import android.widget.TextView;
11+
import android.widget.Toast;
1112

1213
import androidx.annotation.NonNull;
1314
import androidx.core.app.ShareCompat;
@@ -32,6 +33,7 @@ public class ItemActivity extends BaseActivity {
3233
private String[] urls;
3334
private View ivPrev;
3435
private View ivNext;
36+
private MenuItem miAutoplay;
3537

3638
public ItemActivity() {
3739
super(false);
@@ -118,10 +120,29 @@ public void onAnimationRepeat(Animation animation) {
118120
@Override
119121
protected void _onCreateOptionsMenu(Menu menu) {
120122
menu.findItem(R.id.mi_refresh).setVisible(false);
123+
miAutoplay = menu.findItem(R.id.mi_autoplay);
124+
miAutoplay.setVisible(true);
125+
menu.findItem(R.id.mi_browse).setVisible(true);
126+
127+
if (getChiver().isAutoplayEnabled()) {
128+
miAutoplay.setIcon(R.drawable.ic_disable_autoplay);
129+
}
121130
menu.findItem(R.id.mi_share).setVisible(true);
122131
menu.findItem(R.id.mi_browse).setVisible(false);
123132
}
124133

134+
@Override
135+
protected boolean onAutoplay(MenuItem item) {
136+
137+
boolean enabled = getChiver().isAutoplayEnabled();
138+
139+
Toast.makeText(this, enabled ? R.string.disablingAutoplay : R.string.enablingAutoplay, Toast.LENGTH_SHORT).show();
140+
getChiver().setAutoplayEnabled(!enabled);
141+
miAutoplay.setIcon(enabled ? R.drawable.ic_enable_autoplay : R.drawable.ic_disable_autoplay);
142+
143+
return super.onAutoplay(item);
144+
}
145+
125146
@Override
126147
protected int getContentView() {
127148
return R.layout.activity_item;
@@ -162,7 +183,7 @@ private class ItemAdapter extends FragmentStatePagerAdapter {
162183
@NonNull
163184
@Override
164185
public Fragment getItem(int position) {
165-
return new ItemFragment(urls[position], imageLoader);
186+
return new ItemFragment(urls[position], imageLoader, getChiver().isAutoplayEnabled());
166187
}
167188

168189
@Override

app/src/main/java/io/github/chiver/ItemFragment.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import android.webkit.WebViewClient;
1111
import android.widget.ImageView;
1212

13+
import androidx.core.content.ContextCompat;
14+
import androidx.fragment.app.Fragment;
15+
1316
import com.android.volley.toolbox.ImageLoader;
1417
import com.android.volley.toolbox.NetworkImageView;
1518

16-
import androidx.core.content.ContextCompat;
17-
import androidx.fragment.app.Fragment;
1819
import io.github.chiver.model.GalleryItem;
1920

2021
@SuppressWarnings("WeakerAccess")
@@ -36,11 +37,13 @@ public class ItemFragment extends Fragment {
3637
"</html>";
3738
private final String url;
3839
private final ImageLoader imageLoader;
40+
private final boolean autoplayEnabled;
3941
private View rootView;
4042

41-
public ItemFragment(String url, ImageLoader imageLoader) {
43+
public ItemFragment(String url, ImageLoader imageLoader, boolean autoplayEnabled) {
4244
this.url = url;
4345
this.imageLoader = imageLoader;
46+
this.autoplayEnabled = autoplayEnabled;
4447
}
4548

4649
@Override
@@ -52,8 +55,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5255

5356
if (isAGif(url)) {
5457
ImageView ivPlay = rootView.findViewById(R.id.iv_play);
55-
ivPlay.setVisibility(View.VISIBLE);
56-
ivPlay.setOnClickListener(this::onPlay);
58+
if (autoplayEnabled) {
59+
onPlay(ivPlay);
60+
} else {
61+
ivPlay.setVisibility(View.VISIBLE);
62+
ivPlay.setOnClickListener(this::onPlay);
63+
}
5764
}
5865

5966
return rootView;
@@ -77,7 +84,6 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
7784

7885
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
7986

80-
8187
webView.setBackgroundResource(android.R.color.transparent);
8288
rootView.findViewById(R.id.ll_gallery_item).setVisibility(View.VISIBLE);
8389
String html = String.format(HTML, url);
@@ -95,4 +101,4 @@ private void loadItem(NetworkImageView nivItem) {
95101

96102
}
97103

98-
}
104+
}

app/src/main/java/io/github/chiver/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void onRefresh() {
7777
@Override
7878
protected void _onCreateOptionsMenu(Menu menu) {
7979
menu.findItem(R.id.mi_refresh).setVisible(true);
80+
menu.findItem(R.id.mi_autoplay).setVisible(false);
8081
menu.findItem(R.id.mi_share).setVisible(false);
8182
menu.findItem(R.id.mi_browse).setVisible(false);
8283
}

app/src/main/java/io/github/chiver/OSSActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private boolean isNotBlank(String s) {
9292
@Override
9393
protected void _onCreateOptionsMenu(Menu menu) {
9494
menu.findItem(R.id.mi_refresh).setVisible(false);
95+
menu.findItem(R.id.mi_autoplay).setVisible(false);
9596
menu.findItem(R.id.mi_share).setVisible(false);
9697
menu.findItem(R.id.mi_browse).setVisible(false);
9798
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="#FFFFFF"
7+
android:alpha="0.8">
8+
<path
9+
android:fillColor="@android:color/white"
10+
android:pathData="M22,12c0,5.52 -4.48,10 -10,10S2,17.52 2,12c0,-1.19 0.22,-2.32 0.6,-3.38L4.48,9.3C4.17,10.14 4,11.05 4,12c0,4.41 3.59,8 8,8s8,-3.59 8,-8s-3.59,-8 -8,-8c-0.95,0 -1.85,0.17 -2.69,0.48L8.63,2.59C9.69,2.22 10.82,2 12,2C17.52,2 22,6.48 22,12zM5.5,4C4.67,4 4,4.67 4,5.5S4.67,7 5.5,7S7,6.33 7,5.5S6.33,4 5.5,4zM11,16V8H9v8H11zM15,16V8h-2v8H15z"/>
11+
</vector>

0 commit comments

Comments
 (0)