Allow to change column names

This commit is contained in:
stom79 2018-12-28 10:41:40 +01:00
parent 087fd2f2ad
commit 6b4c77f0b8
8 changed files with 140 additions and 42 deletions

View File

@ -2424,8 +2424,18 @@ public abstract class BaseMainActivity extends BaseActivity
bundle.putSerializable("type", typePosition.get(position)); bundle.putSerializable("type", typePosition.get(position));
if( typePosition.get(position) == RetrieveFeedsAsyncTask.Type.TAG){ if( typePosition.get(position) == RetrieveFeedsAsyncTask.Type.TAG){
if( tabLayout.getTabAt(position) != null && tabLayout.getTabAt(position).getText() != null) { if( tabLayout.getTabAt(position) != null && tabLayout.getTabAt(position).getText() != null) {
bundle.putString("tag", tabLayout.getTabAt(position).getText().toString());
tagFragment.put(tabLayout.getTabAt(position).getText().toString(), statusFragment); SQLiteDatabase db = Sqlite.getInstance(BaseMainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<TagTimeline> tagTimelines = new SearchDAO(BaseMainActivity.this, db).getTabInfo(tabLayout.getTabAt(position).getText().toString());
String tag;
if( tagTimelines == null || tagTimelines.size() == 0)
tag = tabLayout.getTabAt(position).getText().toString();
else
tag = tagTimelines.get(0).getName();
bundle.putString("tag", tag);
tagFragment.put(tag, statusFragment);
} }
} }
statusFragment.setArguments(bundle); statusFragment.setArguments(bundle);
@ -2467,40 +2477,45 @@ public abstract class BaseMainActivity extends BaseActivity
tabStrip.getChildAt(position).setOnLongClickListener(new View.OnLongClickListener() { tabStrip.getChildAt(position).setOnLongClickListener(new View.OnLongClickListener() {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
String tag = tabLayout.getTabAt(position).getText().toString().trim(); String tabName = tabLayout.getTabAt(position).getText().toString().trim();
SQLiteDatabase db = Sqlite.getInstance(BaseMainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(BaseMainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<TagTimeline> tagTimelines = new SearchDAO(BaseMainActivity.this, db).getTabInfo(tabName);
String tag;
if( tagTimelines == null || tagTimelines.size() == 0)
tag = tabName;
else
tag = tagTimelines.get(0).getName();
PopupMenu popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(position)); PopupMenu popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(position));
popup.getMenuInflater() popup.getMenuInflater()
.inflate(R.menu.option_tag_timeline, popup.getMenu()); .inflate(R.menu.option_tag_timeline, popup.getMenu());
Menu menu = popup.getMenu(); Menu menu = popup.getMenu();
final MenuItem itemMediaOnly = menu.findItem(R.id.action_show_media_only); final MenuItem itemMediaOnly = menu.findItem(R.id.action_show_media_only);
final MenuItem itemShowNSFW = menu.findItem(R.id.action_show_nsfw); final MenuItem itemShowNSFW = menu.findItem(R.id.action_show_nsfw);
final MenuItem itemAny = menu.findItem(R.id.action_any); final boolean[] changes = {false};
final MenuItem itemAll = menu.findItem(R.id.action_all); final boolean[] mediaOnly = {false};
final MenuItem itemNone = menu.findItem(R.id.action_none); final boolean[] showNSFW = {false};
List<TagTimeline> tagTimelines = new SearchDAO(BaseMainActivity.this, db).getTimelineInfo(tag);
boolean mediaOnly = false;
boolean showNSFW = false;
if( tagTimelines != null && tagTimelines.size() > 0 ) { if( tagTimelines != null && tagTimelines.size() > 0 ) {
mediaOnly = tagTimelines.get(0).isART(); mediaOnly[0] = tagTimelines.get(0).isART();
showNSFW = tagTimelines.get(0).isNSFW(); showNSFW[0] = tagTimelines.get(0).isNSFW();
} }
itemMediaOnly.setChecked(mediaOnly); itemMediaOnly.setChecked(mediaOnly[0]);
itemShowNSFW.setChecked(showNSFW); itemShowNSFW.setChecked(showNSFW[0]);
popup.setOnDismissListener(new PopupMenu.OnDismissListener() { popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override @Override
public void onDismiss(PopupMenu menu) { public void onDismiss(PopupMenu menu) {
if(changes[0]) {
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.detach(tagFragment.get(tag)); fragTransaction.detach(tagFragment.get(tag));
fragTransaction.attach(tagFragment.get(tag)); fragTransaction.attach(tagFragment.get(tag));
fragTransaction.commit(); fragTransaction.commit();
} }
}
}); });
boolean finalMediaOnly = mediaOnly;
boolean finalShowNSFW = showNSFW;
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
@ -2516,22 +2531,25 @@ public abstract class BaseMainActivity extends BaseActivity
return false; return false;
} }
}); });
changes[0] = true;
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_show_media_only: case R.id.action_show_media_only:
TagTimeline tagTimeline = new TagTimeline(); TagTimeline tagTimeline = new TagTimeline();
mediaOnly[0] =!mediaOnly[0];
tagTimeline.setName(tag.trim()); tagTimeline.setName(tag.trim());
tagTimeline.setART(!finalMediaOnly); tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(finalShowNSFW); tagTimeline.setNSFW(showNSFW[0]);
itemMediaOnly.setChecked(!finalMediaOnly); itemMediaOnly.setChecked(mediaOnly[0]);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, null, null); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, null, null);
break; break;
case R.id.action_show_nsfw: case R.id.action_show_nsfw:
showNSFW[0] = !showNSFW[0];
tagTimeline = new TagTimeline(); tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim()); tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly); tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(!finalShowNSFW); tagTimeline.setNSFW(showNSFW[0]);
itemShowNSFW.setChecked(!finalShowNSFW); itemShowNSFW.setChecked(showNSFW[0]);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, null, null); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, null, null);
break; break;
case R.id.action_any: case R.id.action_any:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style);
@ -2550,8 +2568,8 @@ public abstract class BaseMainActivity extends BaseActivity
tagTimeline = new TagTimeline(); tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim()); tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly); tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(finalShowNSFW); tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override @Override
@ -2559,7 +2577,7 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editText.getText().toString().trim().split("\\s+"); String[] values = editText.getText().toString().trim().split("\\s+");
List<String> any = List<String> any =
new ArrayList<>(Arrays.asList(values)); new ArrayList<>(Arrays.asList(values));
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, any, null, null); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline,null, any, null, null);
} }
}); });
AlertDialog alertDialog = dialogBuilder.create(); AlertDialog alertDialog = dialogBuilder.create();
@ -2581,8 +2599,8 @@ public abstract class BaseMainActivity extends BaseActivity
} }
tagTimeline = new TagTimeline(); tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim()); tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly); tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(finalShowNSFW); tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override @Override
@ -2590,7 +2608,7 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editTextAll.getText().toString().trim().split("\\s+"); String[] values = editTextAll.getText().toString().trim().split("\\s+");
List<String> all = List<String> all =
new ArrayList<>(Arrays.asList(values)); new ArrayList<>(Arrays.asList(values));
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, all, null); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, all, null);
} }
}); });
alertDialog = dialogBuilder.create(); alertDialog = dialogBuilder.create();
@ -2612,8 +2630,8 @@ public abstract class BaseMainActivity extends BaseActivity
} }
tagTimeline = new TagTimeline(); tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim()); tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly); tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(finalShowNSFW); tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override @Override
@ -2621,7 +2639,36 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editTextNone.getText().toString().trim().split("\\s+"); String[] values = editTextNone.getText().toString().trim().split("\\s+");
List<String> none = List<String> none =
new ArrayList<>(Arrays.asList(values)); new ArrayList<>(Arrays.asList(values));
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, null, none); new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, null, none);
}
});
alertDialog = dialogBuilder.create();
alertDialog.show();
break;
case R.id.action_displayname:
dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style);
inflater = getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_name, null);
dialogBuilder.setView(dialogView);
final EditText editTextName = dialogView.findViewById(R.id.column_name);
tagInfo = new SearchDAO(BaseMainActivity.this, db).getTimelineInfo(tag);
if( tagInfo != null && tagInfo.size() > 0 && tagInfo.get(0).getDisplayname() != null) {
editTextName.setText(tagInfo.get(0).getDisplayname());
editTextName.setSelection(editTextName.getText().toString().length());
}
tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim());
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String values = editTextName.getText().toString();
if( values.trim().length() == 0)
values = tag;
if( tabLayout.getTabAt(position) != null)
tabLayout.getTabAt(position).setText(values);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, values,null, null, null);
} }
}); });
alertDialog = dialogBuilder.create(); alertDialog = dialogBuilder.create();

View File

@ -205,7 +205,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
else else
apiResponse = api.getPublicTimelineTag(tag, false, max_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); apiResponse = api.getPublicTimelineTag(tag, false, max_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone());
}else{ }else{
apiResponse = api.getPublicTimelineTag(tag, false, max_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); apiResponse = api.getPublicTimelineTag(tag, false, max_id, null, null, null);
} }
break; break;

View File

@ -24,6 +24,7 @@ import java.util.List;
public class TagTimeline { public class TagTimeline {
private String name; private String name;
private String displayname;
private boolean isART; private boolean isART;
private boolean isNSFW; private boolean isNSFW;
private List<String> any; private List<String> any;
@ -77,4 +78,12 @@ public class TagTimeline {
public void setNone(List<String> none) { public void setNone(List<String> none) {
this.none = none; this.none = none;
} }
public String getDisplayname() {
return displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
} }

View File

@ -72,7 +72,7 @@ public class SearchDAO {
* update tag timeline info in database * update tag timeline info in database
* @param tagTimeline TagTimeline * @param tagTimeline TagTimeline
*/ */
public void updateSearch(TagTimeline tagTimeline, List<String> any, List<String> all, List<String> none) { public void updateSearch(TagTimeline tagTimeline, String name, List<String> any, List<String> all, List<String> none) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Sqlite.COL_IS_ART, tagTimeline.isART()?1:0); values.put(Sqlite.COL_IS_ART, tagTimeline.isART()?1:0);
values.put(Sqlite.COL_IS_NSFW, tagTimeline.isNSFW()?1:0); values.put(Sqlite.COL_IS_NSFW, tagTimeline.isNSFW()?1:0);
@ -82,6 +82,8 @@ public class SearchDAO {
values.put(Sqlite.COL_ALL, Helper.arrayToStringStorage(all)); values.put(Sqlite.COL_ALL, Helper.arrayToStringStorage(all));
if( none != null && none.size() > 0) if( none != null && none.size() > 0)
values.put(Sqlite.COL_NONE, Helper.arrayToStringStorage(none)); values.put(Sqlite.COL_NONE, Helper.arrayToStringStorage(none));
if( name != null && name.trim().length() > 0)
values.put(Sqlite.COL_NAME, name);
try{ try{
db.update(Sqlite.TABLE_SEARCH, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_KEYWORDS + " = ?", new String[]{userId, tagTimeline.getName()}); db.update(Sqlite.TABLE_SEARCH, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_KEYWORDS + " = ?", new String[]{userId, tagTimeline.getName()});
}catch (Exception ignored) {} }catch (Exception ignored) {}
@ -153,6 +155,9 @@ public class SearchDAO {
return null; return null;
List<String> searches = new ArrayList<>(); List<String> searches = new ArrayList<>();
while (c.moveToNext() ) { while (c.moveToNext() ) {
if( c.getString(c.getColumnIndex(Sqlite.COL_NAME)) != null)
searches.add(c.getString(c.getColumnIndex(Sqlite.COL_NAME)));
else
searches.add(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS))); searches.add(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS)));
} }
//Close the cursor //Close the cursor
@ -175,6 +180,19 @@ public class SearchDAO {
} }
} }
/**
* Returns TagTimeline information by its keyword in db
* @return info List<TagTimeline>
*/
public List<TagTimeline> getTabInfo(String keyword){
try {
Cursor c = db.query(Sqlite.TABLE_SEARCH, null, Sqlite.COL_NAME + " = \"" + keyword + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId+ "\"", null, null, null, null, null);
return cursorToTagTimelineSearch(c);
} catch (Exception e) {
return null;
}
}
/*** /***
* Method to hydrate stored search from database * Method to hydrate stored search from database
* @param c Cursor * @param c Cursor
@ -197,6 +215,7 @@ public class SearchDAO {
tagTimeline.setNone(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_NONE)))); tagTimeline.setNone(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_NONE))));
}catch (Exception ignored){} }catch (Exception ignored){}
tagTimeline.setName(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS))); tagTimeline.setName(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS)));
tagTimeline.setDisplayname(c.getString(c.getColumnIndex(Sqlite.COL_NAME)));
tagTimeline.setART(c.getInt(c.getColumnIndex(Sqlite.COL_IS_ART))==1); tagTimeline.setART(c.getInt(c.getColumnIndex(Sqlite.COL_IS_ART))==1);
tagTimeline.setNSFW(c.getInt(c.getColumnIndex(Sqlite.COL_IS_NSFW))==1); tagTimeline.setNSFW(c.getInt(c.getColumnIndex(Sqlite.COL_IS_NSFW))==1);
searches.add(tagTimeline); searches.add(tagTimeline);

View File

@ -26,7 +26,7 @@ import android.database.sqlite.SQLiteOpenHelper;
public class Sqlite extends SQLiteOpenHelper { public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 21; public static final int DB_VERSION = 22;
public static final String DB_NAME = "mastodon_etalab_db"; public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db; public static SQLiteDatabase db;
private static Sqlite sInstance; private static Sqlite sInstance;
@ -126,10 +126,11 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_ANY= "ANY_TAG"; static final String COL_ANY= "ANY_TAG";
static final String COL_ALL= "ALL_TAG"; static final String COL_ALL= "ALL_TAG";
static final String COL_NONE = "NONE_TAG"; static final String COL_NONE = "NONE_TAG";
static final String COL_NAME = "NAME";
private final String CREATE_TABLE_SEARCH = "CREATE TABLE " + TABLE_SEARCH + " (" private final String CREATE_TABLE_SEARCH = "CREATE TABLE " + TABLE_SEARCH + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_KEYWORDS + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, " + COL_KEYWORDS + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, "
+ COL_ANY + " TEXT, " + COL_ALL + " TEXT, " + COL_NONE + " TEXT, " + COL_ANY + " TEXT, " + COL_ALL + " TEXT, " + COL_NONE + " TEXT, "+ COL_NAME + " TEXT, "
+ COL_IS_ART + " INTEGER DEFAULT 0, " + COL_IS_NSFW + " INTEGER DEFAULT 0, " + COL_IS_ART + " INTEGER DEFAULT 0, " + COL_IS_NSFW + " INTEGER DEFAULT 0, "
+ COL_DATE_CREATION + " TEXT NOT NULL)"; + COL_DATE_CREATION + " TEXT NOT NULL)";
@ -301,6 +302,10 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_ALL + " TEXT"); db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_ALL + " TEXT");
db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_NONE + " TEXT"); db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_NONE + " TEXT");
} }
case 21:
if( oldVersion > 6) {
db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_NAME + " TEXT");
}
default: default:
break; break;
} }

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/column_name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/change_tag_column"
android:maxLines="1"
/>
</LinearLayout>

View File

@ -2,6 +2,10 @@
<menu xmlns:tools="http://schemas.android.com/tools" <menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_displayname"
app:showAsAction="always"
android:title="@string/change_tag_column" />
<item <item
android:id="@+id/action_show_media_only" android:id="@+id/action_show_media_only"
android:checkable="true" android:checkable="true"

View File

@ -783,6 +783,7 @@
<string name="some_words_any">Any of these words (space-separated)</string> <string name="some_words_any">Any of these words (space-separated)</string>
<string name="some_words_all">All these words (space-separated)</string> <string name="some_words_all">All these words (space-separated)</string>
<string name="some_words_none">None of these words (space-separated)</string> <string name="some_words_none">None of these words (space-separated)</string>
<string name="change_tag_column">Change column name</string>
<!-- end languages --> <!-- end languages -->
</resources> </resources>