From 911d500e4f0b5ee02f1ed61a51246d2fae08ebea Mon Sep 17 00:00:00 2001 From: stom79 Date: Thu, 27 Dec 2018 17:49:50 +0100 Subject: [PATCH] Prepares DB migration + back end --- .../mastodon/activities/BaseMainActivity.java | 54 ++++++++++++++++++- .../fr/gouv/etalab/mastodon/client/API.java | 54 +++++++++++++------ .../mastodon/client/Entities/TagTimeline.java | 31 ++++++++++- .../fragments/DisplayStatusFragment.java | 25 ++++----- .../etalab/mastodon/sqlite/SearchDAO.java | 25 ++++++++- .../gouv/etalab/mastodon/sqlite/Sqlite.java | 12 ++++- app/src/main/res/menu/option_tag_timeline.xml | 12 +++++ app/src/main/res/values/strings.xml | 3 ++ 8 files changed, 180 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index b854beaeb..104bb63b2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -2474,6 +2474,9 @@ public abstract class BaseMainActivity extends BaseActivity Menu menu = popup.getMenu(); final MenuItem itemMediaOnly = menu.findItem(R.id.action_show_media_only); final MenuItem itemShowNSFW = menu.findItem(R.id.action_show_nsfw); + final MenuItem itemAny = menu.findItem(R.id.action_any); + final MenuItem itemAll = menu.findItem(R.id.action_all); + final MenuItem itemNone = menu.findItem(R.id.action_none); List tagTimelines = new SearchDAO(BaseMainActivity.this, db).getTimelineInfo(tag); boolean mediaOnly = false; boolean showNSFW = false; @@ -2526,8 +2529,55 @@ public abstract class BaseMainActivity extends BaseActivity itemShowNSFW.setChecked(!finalShowNSFW); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline); break; - case R.id.action_delete: + case R.id.action_any: AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style); + LayoutInflater inflater = getLayoutInflater(); + @SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.filter_regex, null); + dialogBuilder.setView(dialogView); + final EditText editText = dialogView.findViewById(R.id.filter_regex); + Toast alertRegex = Toasty.warning(BaseMainActivity.this, getString(R.string.alert_regex), Toast.LENGTH_LONG); + editText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + try { + //noinspection ResultOfMethodCallIgnored + Pattern.compile("(" + s.toString() + ")", Pattern.CASE_INSENSITIVE); + }catch (Exception e){ + if( !alertRegex.getView().isShown()){ + alertRegex.show(); + } + } + + } + }); + if( show_filtered != null) { + editText.setText(show_filtered); + editText.setSelection(editText.getText().toString().length()); + } + dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + itemFilter.setTitle(editText.getText().toString().trim()); + if(homeFragment != null && homeFragment.getUserVisibleHint()) + editor.putString(Helper.SET_FILTER_REGEX_HOME, editText.getText().toString().trim()); + if(localFragment != null && localFragment.getUserVisibleHint()) + editor.putString(Helper.SET_FILTER_REGEX_LOCAL, editText.getText().toString().trim()); + if(federatedFragment != null && federatedFragment.getUserVisibleHint()) + editor.putString(Helper.SET_FILTER_REGEX_PUBLIC, editText.getText().toString().trim()); + editor.apply(); + } + }); + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.show(); + break; + case R.id.action_delete: + dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style); dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { @@ -2559,7 +2609,7 @@ public abstract class BaseMainActivity extends BaseActivity } }); dialogBuilder.setMessage(getString(R.string.delete) + ": " + tag); - AlertDialog alertDialog = dialogBuilder.create(); + alertDialog = dialogBuilder.create(); alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index b44c97c3f..730d09da2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -1128,20 +1128,20 @@ public class API { - public APIResponse getCustomArtTimeline(boolean local, String tag, String max_id){ - return getArtTimeline(local, tag, max_id, null); + public APIResponse getCustomArtTimeline(boolean local, String tag, String max_id, List any, List all, List none){ + return getArtTimeline(local, tag, max_id, null, any, all, none); } - public APIResponse getArtTimeline(boolean local, String max_id){ - return getArtTimeline(local, null, max_id, null); + public APIResponse getArtTimeline(boolean local, String max_id, List any, List all, List none){ + return getArtTimeline(local, null, max_id, null, any, all, none); } - public APIResponse getCustomArtTimelineSinceId(boolean local, String tag, String since_id){ - return getArtTimeline(local, tag, null, since_id); + public APIResponse getCustomArtTimelineSinceId(boolean local, String tag, String since_id, List any, List all, List none){ + return getArtTimeline(local, tag, null, since_id, any, all, none); } - public APIResponse getArtTimelineSinceId(boolean local, String since_id){ - return getArtTimeline(local, null, null, since_id); + public APIResponse getArtTimelineSinceId(boolean local, String since_id, List any, List all, List none){ + return getArtTimeline(local, null, null, since_id, any, all, none); } /** * Retrieves art timeline @@ -1149,10 +1149,10 @@ public class API { * @param max_id String id max * @return APIResponse */ - private APIResponse getArtTimeline(boolean local, String tag, String max_id, String since_id){ + private APIResponse getArtTimeline(boolean local, String tag, String max_id, String since_id, List any, List all, List none){ if( tag == null) tag = "mastoart"; - APIResponse apiResponse = getPublicTimelineTag(tag, local, true, max_id, since_id, tootPerPage); + APIResponse apiResponse = getPublicTimelineTag(tag, local, true, max_id, since_id, tootPerPage, any, all, none); APIResponse apiResponseReply = new APIResponse(); if( apiResponse != null){ apiResponseReply.setMax_id(apiResponse.getMax_id()); @@ -1184,8 +1184,8 @@ public class API { * @return APIResponse */ @SuppressWarnings("SameParameterValue") - public APIResponse getPublicTimelineTag(String tag, boolean local, String max_id){ - return getPublicTimelineTag(tag, local, false, max_id, null, tootPerPage); + public APIResponse getPublicTimelineTag(String tag, boolean local, String max_id, List any, List all, List none){ + return getPublicTimelineTag(tag, local, false, max_id, null, tootPerPage, any, all, none); } /** @@ -1196,8 +1196,8 @@ public class API { * @return APIResponse */ @SuppressWarnings("SameParameterValue") - public APIResponse getPublicTimelineTagSinceId(String tag, boolean local, String since_id){ - return getPublicTimelineTag(tag, local, false, null, since_id, tootPerPage); + public APIResponse getPublicTimelineTagSinceId(String tag, boolean local, String since_id, List any, List all, List none){ + return getPublicTimelineTag(tag, local, false, null, since_id, tootPerPage, any, all, none); } /** * Retrieves public tag timeline *synchronously* @@ -1209,7 +1209,7 @@ public class API { * @return APIResponse */ @SuppressWarnings("SameParameterValue") - private APIResponse getPublicTimelineTag(String tag, boolean local, boolean onlymedia, String max_id, String since_id, int limit){ + private APIResponse getPublicTimelineTag(String tag, boolean local, boolean onlymedia, String max_id, String since_id, int limit, List any, List all, List none){ HashMap params = new HashMap<>(); if( local) @@ -1220,6 +1220,30 @@ public class API { params.put("since_id", since_id); if( 0 > limit || limit > 40) limit = 40; + if( onlymedia) + params.put("only_media", Boolean.toString(true)); + + if( any != null && any.size() > 0) { + StringBuilder parameters = new StringBuilder(); + for (String a : any) + parameters.append("any[]=").append(a).append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(6)); + params.put("any[]", parameters.toString()); + } + if( all != null && all.size() > 0) { + StringBuilder parameters = new StringBuilder(); + for (String a : all) + parameters.append("all[]=").append(a).append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(6)); + params.put("all[]", parameters.toString()); + } + if( none != null && none.size() > 0) { + StringBuilder parameters = new StringBuilder(); + for (String a : none) + parameters.append("none[]=").append(a).append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(7)); + params.put("none[]", parameters.toString()); + } params.put("limit",String.valueOf(limit)); statuses = new ArrayList<>(); if( tag == null) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TagTimeline.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TagTimeline.java index 65fc956fe..f1cbdbc93 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TagTimeline.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TagTimeline.java @@ -14,6 +14,8 @@ * see . */ package fr.gouv.etalab.mastodon.client.Entities; +import java.util.List; + /** * Created by Thomas on 15/12/2018. * Manage Tags timeline settings @@ -24,8 +26,9 @@ public class TagTimeline { private String name; private boolean isART; private boolean isNSFW; - - + private List any; + private List all; + private List none; public String getName() { return name; @@ -50,4 +53,28 @@ public class TagTimeline { public void setNSFW(boolean NSFW) { isNSFW = NSFW; } + + public List getAny() { + return any; + } + + public void setAny(List any) { + this.any = any; + } + + public List getAll() { + return all; + } + + public void setAll(List all) { + this.all = all; + } + + public List getNone() { + return none; + } + + public void setNone(List none) { + this.none = none; + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 55094284c..87c0682a9 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -793,21 +793,18 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn } for (int i = statuses.size() - 1; i >= 0; i--) { if( this.statuses != null) { - if (this.statuses.size() == 0){ - if( type != RetrieveFeedsAsyncTask.Type.HOME){ - if( Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) { - inserted++; - this.statuses.add(0, statuses.get(i)); - } - }else { - if( lastReadToot != null && Long.parseLong(statuses.get(i).getId()) > Long.parseLong(lastReadToot)) { - statuses.get(i).setNew(true); - MainActivity.countNewStatus++; - inserted++; - this.statuses.add(0, statuses.get(i)); - } + if( type != RetrieveFeedsAsyncTask.Type.HOME){ + if( Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) { + inserted++; + this.statuses.add(0, statuses.get(i)); + } + }else { + if( lastReadToot != null && Long.parseLong(statuses.get(i).getId()) > Long.parseLong(lastReadToot)) { + statuses.get(i).setNew(true); + MainActivity.countNewStatus++; + inserted++; + this.statuses.add(0, statuses.get(i)); } - } } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/SearchDAO.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/SearchDAO.java index 15dae6751..6213a86b5 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/SearchDAO.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/SearchDAO.java @@ -54,10 +54,16 @@ public class SearchDAO { * Insert a keyword in database * @param keyword String */ - public void insertSearch(String keyword) { + public void insertSearch(String keyword, List any, List all, List none) { ContentValues values = new ContentValues(); values.put(Sqlite.COL_KEYWORDS, keyword); values.put(Sqlite.COL_USER_ID, userId); + if( any != null && any.size() > 0) + values.put(Sqlite.COL_ANY, Helper.arrayToStringStorage(any)); + if( all != null && all.size() > 0) + values.put(Sqlite.COL_ALL, Helper.arrayToStringStorage(all)); + if( none != null && none.size() > 0) + values.put(Sqlite.COL_NONE, Helper.arrayToStringStorage(none)); values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(new Date())); //Inserts search try{ @@ -72,10 +78,16 @@ public class SearchDAO { * update tag timeline info in database * @param tagTimeline TagTimeline */ - public void updateSearch(TagTimeline tagTimeline) { + public void updateSearch(TagTimeline tagTimeline, List any, List all, List none) { ContentValues values = new ContentValues(); values.put(Sqlite.COL_IS_ART, tagTimeline.isART()?1:0); values.put(Sqlite.COL_IS_NSFW, tagTimeline.isNSFW()?1:0); + if( any != null && any.size() > 0) + values.put(Sqlite.COL_ANY, Helper.arrayToStringStorage(any)); + if( all != null && all.size() > 0) + values.put(Sqlite.COL_ALL, Helper.arrayToStringStorage(all)); + if( none != null && none.size() > 0) + values.put(Sqlite.COL_NONE, Helper.arrayToStringStorage(none)); //Inserts search try{ db.update(Sqlite.TABLE_SEARCH, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_KEYWORDS + " = ?", new String[]{userId, tagTimeline.getName()}); @@ -182,6 +194,15 @@ public class SearchDAO { List searches = new ArrayList<>(); while (c.moveToNext() ) { TagTimeline tagTimeline = new TagTimeline(); + try { + tagTimeline.setAny(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_ANY)))); + }catch (Exception ignored){} + try { + tagTimeline.setAll(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_ALL)))); + }catch (Exception ignored){} + try { + tagTimeline.setNone(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_NONE)))); + }catch (Exception ignored){} tagTimeline.setName(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS))); tagTimeline.setART(c.getInt(c.getColumnIndex(Sqlite.COL_IS_ART))==1); tagTimeline.setNSFW(c.getInt(c.getColumnIndex(Sqlite.COL_IS_NSFW))==1); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java index 196a89bc8..017b2e4da 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java @@ -26,7 +26,7 @@ import android.database.sqlite.SQLiteOpenHelper; public class Sqlite extends SQLiteOpenHelper { - public static final int DB_VERSION = 20; + public static final int DB_VERSION = 21; public static final String DB_NAME = "mastodon_etalab_db"; public static SQLiteDatabase db; private static Sqlite sInstance; @@ -123,9 +123,13 @@ public class Sqlite extends SQLiteOpenHelper { static final String COL_KEYWORDS = "KEYWORDS"; static final String COL_IS_ART= "IS_ART"; static final String COL_IS_NSFW= "IS_NSFW"; + static final String COL_ANY= "ANY_TAG"; + static final String COL_ALL= "ALL_TAG"; + static final String COL_NONE = "NONE_TAG"; private final String CREATE_TABLE_SEARCH = "CREATE TABLE " + TABLE_SEARCH + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_KEYWORDS + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, " + + COL_ANY + " TEXT, " + COL_ALL + " TEXT, " + COL_NONE + " TEXT, " + COL_IS_ART + " INTEGER DEFAULT 0, " + COL_IS_NSFW + " INTEGER DEFAULT 0, " + COL_DATE_CREATION + " TEXT NOT NULL)"; @@ -291,6 +295,12 @@ public class Sqlite extends SQLiteOpenHelper { db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_IS_ART + " INTEGER DEFAULT 0"); db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_IS_NSFW + " INTEGER DEFAULT 0"); } + case 20: + if( oldVersion > 6) { + db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_ANY + " TEXT"); + db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_ALL + " TEXT"); + db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_NONE + " TEXT"); + } default: break; } diff --git a/app/src/main/res/menu/option_tag_timeline.xml b/app/src/main/res/menu/option_tag_timeline.xml index ef878cab6..e6666e506 100644 --- a/app/src/main/res/menu/option_tag_timeline.xml +++ b/app/src/main/res/menu/option_tag_timeline.xml @@ -15,6 +15,18 @@ android:title="@string/show_media_nsfw" app:actionViewClass="android.widget.CheckBox" app:showAsAction="always" /> + + + Pixelfed instance Mastodon instance No Pixelfed instances + Any of these + All of these + None of these \ No newline at end of file