Prepares DB migration + back end

This commit is contained in:
stom79 2018-12-27 17:49:50 +01:00
parent 1db4936956
commit 911d500e4f
8 changed files with 180 additions and 36 deletions

View File

@ -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<TagTimeline> 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) {

View File

@ -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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> none){
HashMap<String, String> 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)

View File

@ -14,6 +14,8 @@
* see <http://www.gnu.org/licenses>. */
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<String> any;
private List<String> all;
private List<String> none;
public String getName() {
return name;
@ -50,4 +53,28 @@ public class TagTimeline {
public void setNSFW(boolean NSFW) {
isNSFW = NSFW;
}
public List<String> getAny() {
return any;
}
public void setAny(List<String> any) {
this.any = any;
}
public List<String> getAll() {
return all;
}
public void setAll(List<String> all) {
this.all = all;
}
public List<String> getNone() {
return none;
}
public void setNone(List<String> none) {
this.none = none;
}
}

View File

@ -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));
}
}
}
}

View File

@ -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<String> any, List<String> all, List<String> 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<String> any, List<String> all, List<String> 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<TagTimeline> 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);

View File

@ -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;
}

View File

@ -15,6 +15,18 @@
android:title="@string/show_media_nsfw"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
<item
android:id="@+id/action_any"
app:showAsAction="always"
android:title="@string/any_tags" />
<item
android:id="@+id/action_all"
app:showAsAction="always"
android:title="@string/all_tags" />
<item
android:id="@+id/action_none"
app:showAsAction="always"
android:title="@string/none_tags" />
<item
android:id="@+id/action_delete"
app:showAsAction="always"

View File

@ -777,6 +777,9 @@
<string name="pixelfed_instance">Pixelfed instance</string>
<string name="mastodon_instance">Mastodon instance</string>
<string name="no_pixelfed_instance">No Pixelfed instances</string>
<string name="any_tags">Any of these</string>
<string name="all_tags">All of these</string>
<string name="none_tags">None of these</string>
<!-- end languages -->
</resources>