Merge pull request #783 from kauron/master

Added configurable swipe actions and open browser as a swipe action.
This commit is contained in:
David Luhmer 2019-09-14 14:11:02 +02:00 committed by GitHub
commit cc74a2e429
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 135 additions and 25 deletions

View file

@ -22,11 +22,13 @@
package de.luhmer.owncloudnewsreader;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
@ -70,6 +72,10 @@ import io.reactivex.observers.DisposableObserver;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_STARRED_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION_DEFAULT;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_RIGHT_ACTION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_RIGHT_ACTION_DEFAULT;
/**
* A fragment representing a single NewsReader detail screen. This fragment is
@ -92,8 +98,10 @@ public class NewsReaderDetailFragment extends Fragment {
SwipeRefreshLayout swipeRefresh;
private Long idFeed;
private Drawable markAsReadDrawable;
private Drawable starredDrawable;
private Drawable leftSwipeDrawable;
private Drawable rightSwipeDrawable;
private String prevLeftAction = "";
private String prevRightAction = "";
private int accentColor;
private Parcelable layoutManagerSavedState;
@ -201,6 +209,8 @@ public class NewsReaderDetailFragment extends Fragment {
}
onResumeCount++;
updateSwipeDrawables(false);
super.onResume();
}
@ -413,12 +423,45 @@ public class NewsReaderDetailFragment extends Fragment {
((NewsReaderApplication) getActivity().getApplication()).getAppComponent().injectFragment(this);
TypedArray a = context.obtainStyledAttributes(attrs, new int[]{R.attr.markasreadDrawable, R.attr.starredDrawable, R.attr.colorAccent});
markAsReadDrawable = a.getDrawable(0);
starredDrawable = a.getDrawable(1);
TypedArray styledAttributes = context.obtainStyledAttributes(attrs, new int[]{R.attr.colorAccent});
updateSwipeDrawables(true);
int color = Constants.isNextCloud(mPrefs) ? R.color.nextcloudBlue : R.color.owncloudBlue;
accentColor = a.getColor(2, ContextCompat.getColor(context, color));
a.recycle();
accentColor = styledAttributes.getColor(2, ContextCompat.getColor(context, color));
styledAttributes.recycle();
}
/**
*
* @param forceUpdate force swipe drawables to be reloaded
*/
private void updateSwipeDrawables(boolean forceUpdate) {
String leftAction = mPrefs.getString(SP_SWIPE_LEFT_ACTION, SP_SWIPE_LEFT_ACTION_DEFAULT);
String rightAction = mPrefs.getString(SP_SWIPE_RIGHT_ACTION, SP_SWIPE_RIGHT_ACTION_DEFAULT);
if (!forceUpdate && leftAction.equals(prevLeftAction) && rightAction.equals(prevRightAction)) {
return;
}
prevLeftAction = leftAction;
prevRightAction = rightAction;
int leftId = getLayoutId(leftAction);
int rightId = getLayoutId(rightAction);
TypedArray styledAttributes = getContext().obtainStyledAttributes(new int[]{leftId, rightId});
leftSwipeDrawable = styledAttributes.getDrawable(0);
rightSwipeDrawable = styledAttributes.getDrawable(1);
styledAttributes.recycle();
}
private int getLayoutId(String action) {
switch (action) {
case "0": return R.attr.openinbrowserDrawable;
case "1": return R.attr.starredDrawable;
case "2": return R.attr.markasreadDrawable;
default:
Log.e(TAG, "Invalid option saved to prefs. This should not happen");
return Integer.MAX_VALUE;
}
}
@Override
@ -551,11 +594,27 @@ public class NewsReaderDetailFragment extends Fragment {
public void onSwiped(final RecyclerView.ViewHolder viewHolder, final int direction) {
final NewsListRecyclerAdapter adapter = (NewsListRecyclerAdapter) recyclerView.getAdapter();
if (direction == ItemTouchHelper.LEFT) {
adapter.toggleReadStateOfItem((ViewHolder) viewHolder);
} else if (direction == ItemTouchHelper.RIGHT) {
adapter.toggleStarredStateOfItem((ViewHolder) viewHolder);
//adapter.toggleReadStateOfItem((ViewHolder) viewHolder);
String swipeAction;
if (direction == ItemTouchHelper.LEFT)
swipeAction = mPrefs.getString(SP_SWIPE_LEFT_ACTION, SP_SWIPE_LEFT_ACTION_DEFAULT);
else
swipeAction = mPrefs.getString(SP_SWIPE_RIGHT_ACTION, SP_SWIPE_RIGHT_ACTION_DEFAULT);
switch (swipeAction) {
case "0": // Open link in browser and mark as read
String currentUrl = ((ViewHolder) viewHolder).getRssItem().getLink();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentUrl));
startActivity(browserIntent);
adapter.changeReadStateOfItem((ViewHolder) viewHolder, true);
break;
case "1": // Star
adapter.toggleStarredStateOfItem((ViewHolder) viewHolder);
break;
case "2": // Read
adapter.toggleReadStateOfItem((ViewHolder) viewHolder);
break;
default:
Log.e(TAG, "Swipe preferences has an invalid value");
break;
}
// Hack to reset view, see https://code.google.com/p/android/issues/detail?id=175798
recyclerView.removeView(viewHolder.itemView);
@ -572,10 +631,10 @@ public class NewsReaderDetailFragment extends Fragment {
float fractionMoved = Math.abs(dX / viewHolder.itemView.getMeasuredWidth());
Drawable drawable;
if (dX < 0) {
drawable = markAsReadDrawable;
drawable = leftSwipeDrawable;
viewRect.left = (int) dX + viewRect.right;
} else {
drawable = starredDrawable;
drawable = rightSwipeDrawable;
viewRect.right = (int) dX - viewRect.left;
}

View file

@ -27,10 +27,11 @@ import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.MenuItem;
import javax.inject.Inject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import javax.inject.Inject;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
/**
@ -87,6 +88,10 @@ public class SettingsActivity extends AppCompatActivity {
public static final String SP_SORT_ORDER = "sp_sort_order";
public static final String SP_DISPLAY_BROWSER = "sp_display_browser";
public static final String SP_SEARCH_IN = "sp_search_in";
public static final String SP_SWIPE_RIGHT_ACTION = "sp_swipe_right_action";
public static final String SP_SWIPE_LEFT_ACTION = "sp_swipe_left_action";
public static final String SP_SWIPE_RIGHT_ACTION_DEFAULT = "1";
public static final String SP_SWIPE_LEFT_ACTION_DEFAULT = "2";
public static final String CB_VERSION = "cb_version";

View file

@ -12,9 +12,6 @@ import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import javax.inject.Inject;
import javax.inject.Named;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.CheckBoxPreference;
import androidx.preference.DialogPreference;
@ -22,6 +19,10 @@ import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.TwoStatePreference;
import javax.inject.Inject;
import javax.inject.Named;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
@ -47,6 +48,8 @@ import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_FONT_SIZE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_MAX_CACHE_SIZE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SEARCH_IN;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SORT_ORDER;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_RIGHT_ACTION;
public class SettingsFragment extends PreferenceFragmentCompat {
@ -240,6 +243,8 @@ public class SettingsFragment extends PreferenceFragmentCompat {
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SORT_ORDER));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SEARCH_IN));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SWIPE_RIGHT_ACTION));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SWIPE_LEFT_ACTION));
}
private void bindDataSyncPreferences(final PreferenceFragmentCompat prefFrag)

View file

@ -6,7 +6,7 @@
<item android:state_above_anchor="true"><color android:color="@color/markasreadColor" /></item>
</selector>
</item>
<item android:left="5dp">
<bitmap android:src="@drawable/ic_check_box_white" android:gravity="center_vertical|left" />
<item android:left="5dp" android:right="5dp">
<bitmap android:src="@drawable/ic_check_box_white" android:gravity="center_vertical" />
</item>
</layer-list>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_above_anchor="false"><color android:color="@color/swipeBackground" /></item>
<item android:state_above_anchor="true"><color android:color="@color/colorPrimary" /></item>
</selector>
</item>
<item android:left="5dp" android:right="5dp" >
<bitmap android:src="@drawable/ic_action_open_in_browser" android:gravity="center_vertical" />
</item>
</layer-list>

View file

@ -6,7 +6,7 @@
<item android:state_above_anchor="true"><color android:color="@color/starredColor" /></item>
</selector>
</item>
<item android:right="5dp">
<bitmap android:src="@drawable/ic_action_star_dark" android:gravity="center_vertical|right" />
<item android:right="5dp" android:left="5dp">
<bitmap android:src="@drawable/ic_action_star_dark" android:gravity="center_vertical" />
</item>
</layer-list>

View file

@ -6,7 +6,7 @@
<attr name="starredColor" format="color" />
<attr name="unstarredColor" format="color" />
<attr name="starredDrawable" format="reference" />
<attr name="openinbrowserDrawable" format="reference" />
<attr name="markasreadDrawable" format="reference" />
<attr name="tintColor" format="color" />

View file

@ -133,6 +133,20 @@
<string name="pref_general_sort_order_new_old">New -> Old</string>
<string name="pref_general_sort_order_old_new">Old -> New</string>
<string-array name="pref_general_swipe_action">
<item>@string/action_openInBrowser</item>
<item>@string/action_starred</item>
<item>@string/action_read</item>
</string-array>
<string-array name="pref_general_swipe_action_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string name="pref_rename_action_swipe_right">Action when swiping right</string>
<string name="pref_rename_action_swipe_left">Action when swiping left</string>
<string-array name="pref_general_sort_order" translatable="false">
<item>@string/pref_general_sort_order_new_old</item>
<item>@string/pref_general_sort_order_old_new</item>

View file

@ -39,6 +39,7 @@
<item name="starredColor">@color/starredColor</item>
<item name="unstarredColor">@color/unstarredColor</item>
<item name="markasreadDrawable">@drawable/swipe_markasread</item>
<item name="openinbrowserDrawable">@drawable/swipe_openinbrowser</item>
<item name="starredDrawable">@drawable/swipe_setstarred</item>
<item name="news_detail_background_color">@color/news_detail_background_color</item>

View file

@ -58,7 +58,6 @@
android:title="@string/pref_title_AllowAllSSLCertificates" />
-->
<SwitchPreference
android:key="cb_AutoSyncOnStart"
android:title="@string/pref_title_AutoSyncOnStart"
@ -84,6 +83,21 @@
android:title="@string/pref_title_OpenInBrowserDirectly"
app:iconSpaceReserved="false"/>
<ListPreference
android:defaultValue="1"
android:entries="@array/pref_general_swipe_action"
android:entryValues="@array/pref_general_swipe_action_values"
android:key="sp_swipe_right_action"
android:title="@string/pref_rename_action_swipe_right"
app:iconSpaceReserved="false"/>
<ListPreference
android:defaultValue="2"
android:entries="@array/pref_general_swipe_action"
android:entryValues="@array/pref_general_swipe_action_values"
android:key="sp_swipe_left_action"
android:title="@string/pref_rename_action_swipe_left"
app:iconSpaceReserved="false"/>
<ListPreference
android:defaultValue="1"