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));
if( typePosition.get(position) == RetrieveFeedsAsyncTask.Type.TAG){
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);
@ -2467,40 +2477,45 @@ public abstract class BaseMainActivity extends BaseActivity
tabStrip.getChildAt(position).setOnLongClickListener(new View.OnLongClickListener() {
@Override
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();
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));
popup.getMenuInflater()
.inflate(R.menu.option_tag_timeline, popup.getMenu());
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;
final boolean[] changes = {false};
final boolean[] mediaOnly = {false};
final boolean[] showNSFW = {false};
if( tagTimelines != null && tagTimelines.size() > 0 ) {
mediaOnly = tagTimelines.get(0).isART();
showNSFW = tagTimelines.get(0).isNSFW();
mediaOnly[0] = tagTimelines.get(0).isART();
showNSFW[0] = tagTimelines.get(0).isNSFW();
}
itemMediaOnly.setChecked(mediaOnly);
itemShowNSFW.setChecked(showNSFW);
itemMediaOnly.setChecked(mediaOnly[0]);
itemShowNSFW.setChecked(showNSFW[0]);
popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.detach(tagFragment.get(tag));
fragTransaction.attach(tagFragment.get(tag));
fragTransaction.commit();
if(changes[0]) {
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.detach(tagFragment.get(tag));
fragTransaction.attach(tagFragment.get(tag));
fragTransaction.commit();
}
}
});
boolean finalMediaOnly = mediaOnly;
boolean finalShowNSFW = showNSFW;
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
@ -2516,22 +2531,25 @@ public abstract class BaseMainActivity extends BaseActivity
return false;
}
});
changes[0] = true;
switch (item.getItemId()) {
case R.id.action_show_media_only:
TagTimeline tagTimeline = new TagTimeline();
mediaOnly[0] =!mediaOnly[0];
tagTimeline.setName(tag.trim());
tagTimeline.setART(!finalMediaOnly);
tagTimeline.setNSFW(finalShowNSFW);
itemMediaOnly.setChecked(!finalMediaOnly);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, null, null);
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
itemMediaOnly.setChecked(mediaOnly[0]);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, null, null);
break;
case R.id.action_show_nsfw:
showNSFW[0] = !showNSFW[0];
tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly);
tagTimeline.setNSFW(!finalShowNSFW);
itemShowNSFW.setChecked(!finalShowNSFW);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null, null, null);
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
itemShowNSFW.setChecked(showNSFW[0]);
new SearchDAO(BaseMainActivity.this, db).updateSearch(tagTimeline, null,null, null, null);
break;
case R.id.action_any:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BaseMainActivity.this, style);
@ -2550,8 +2568,8 @@ public abstract class BaseMainActivity extends BaseActivity
tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly);
tagTimeline.setNSFW(finalShowNSFW);
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
@ -2559,7 +2577,7 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editText.getText().toString().trim().split("\\s+");
List<String> any =
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();
@ -2581,8 +2599,8 @@ public abstract class BaseMainActivity extends BaseActivity
}
tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly);
tagTimeline.setNSFW(finalShowNSFW);
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
@ -2590,7 +2608,7 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editTextAll.getText().toString().trim().split("\\s+");
List<String> all =
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();
@ -2612,8 +2630,8 @@ public abstract class BaseMainActivity extends BaseActivity
}
tagTimeline = new TagTimeline();
tagTimeline.setName(tag.trim());
tagTimeline.setART(finalMediaOnly);
tagTimeline.setNSFW(finalShowNSFW);
tagTimeline.setART(mediaOnly[0]);
tagTimeline.setNSFW(showNSFW[0]);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
@ -2621,7 +2639,36 @@ public abstract class BaseMainActivity extends BaseActivity
String[] values = editTextNone.getText().toString().trim().split("\\s+");
List<String> none =
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();

View File

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

View File

@ -24,6 +24,7 @@ import java.util.List;
public class TagTimeline {
private String name;
private String displayname;
private boolean isART;
private boolean isNSFW;
private List<String> any;
@ -77,4 +78,12 @@ public class TagTimeline {
public void setNone(List<String> 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
* @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();
values.put(Sqlite.COL_IS_ART, tagTimeline.isART()?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));
if( none != null && none.size() > 0)
values.put(Sqlite.COL_NONE, Helper.arrayToStringStorage(none));
if( name != null && name.trim().length() > 0)
values.put(Sqlite.COL_NAME, name);
try{
db.update(Sqlite.TABLE_SEARCH, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_KEYWORDS + " = ?", new String[]{userId, tagTimeline.getName()});
}catch (Exception ignored) {}
@ -153,7 +155,10 @@ public class SearchDAO {
return null;
List<String> searches = new ArrayList<>();
while (c.moveToNext() ) {
searches.add(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS)));
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)));
}
//Close the cursor
c.close();
@ -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
* @param c Cursor
@ -197,6 +215,7 @@ public class SearchDAO {
tagTimeline.setNone(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_NONE))));
}catch (Exception ignored){}
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.setNSFW(c.getInt(c.getColumnIndex(Sqlite.COL_IS_NSFW))==1);
searches.add(tagTimeline);

View File

@ -26,7 +26,7 @@ import android.database.sqlite.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 SQLiteDatabase db;
private static Sqlite sInstance;
@ -126,10 +126,11 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_ANY= "ANY_TAG";
static final String COL_ALL= "ALL_TAG";
static final String COL_NONE = "NONE_TAG";
static final String COL_NAME = "NAME";
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_ANY + " TEXT, " + COL_ALL + " TEXT, " + COL_NONE + " TEXT, "+ COL_NAME + " TEXT, "
+ COL_IS_ART + " INTEGER DEFAULT 0, " + COL_IS_NSFW + " INTEGER DEFAULT 0, "
+ 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_NONE + " TEXT");
}
case 21:
if( oldVersion > 6) {
db.execSQL("ALTER TABLE " + TABLE_SEARCH + " ADD COLUMN " + COL_NAME + " TEXT");
}
default:
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"
xmlns:android="http://schemas.android.com/apk/res/android"
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
android:id="@+id/action_show_media_only"
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_all">All 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 -->
</resources>