Prepares db and some layout

This commit is contained in:
tom79 2019-05-25 11:29:58 +02:00
parent 4cc67f3f60
commit 04d1ffa2c1
7 changed files with 223 additions and 3 deletions

View File

@ -44,6 +44,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
import app.fedilab.android.sqlite.InstancesDAO;
import es.dmoral.toasty.Toasty;
import app.fedilab.android.R;
import app.fedilab.android.activities.BaseMainActivity;
@ -392,6 +393,17 @@ public class ManageTimelines {
}
});
}
}else if( tl.getType() == Type.INSTANCE && (tl.getRemoteInstance().getType().equals("MASTODON") || tl.getRemoteInstance().getType().equals("PEERTUBE") || tl.getRemoteInstance().getType().equals("PLEROMA"))) {
if( tabStrip != null && tabStrip.getChildCount() > position) {
int finalPosition = position;
tabStrip.getChildAt(position).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
instanceClick(context, tl, tabStrip, finalPosition);
return true;
}
});
}
}else if (tl.getType() == Type.LIST){
if( tabStrip != null && tabStrip.getChildCount() > position) {
tabStrip.getChildAt(position).setOnLongClickListener(new View.OnLongClickListener() {
@ -940,4 +952,125 @@ public class ManageTimelines {
}
private void instanceClick(Context context, ManageTimelines tl, LinearLayout tabStrip, int position){
PopupMenu popup = new PopupMenu(context, tabStrip.getChildAt(position));
SQLiteDatabase db = Sqlite.getInstance(context, DB_NAME, null, Sqlite.DB_VERSION).open();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
style = R.style.Dialog;
}
remoteInstance = tl.getRemoteInstance();
if( remoteInstance == null)
return;
String currentFilter = remoteInstance.getFilteredWith();
popup.getMenuInflater()
.inflate(R.menu.option_instance_timeline, popup.getMenu());
java.util.List<String> tags = remoteInstance.getTags();
if( tags != null && tags.size() > 0){
java.util.Collections.sort(tags);
for(String tag: tags){
String title = "";
if( currentFilter != null && currentFilter.equals(tag)) {
title = "" + tag;
}else{
title = tag;
}
MenuItem item = popup.getMenu().add(0, 0, Menu.NONE, title);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
}
}
final boolean[] changes = {false};
popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
if(changes[0]) {
FragmentTransaction fragTransaction = ((MainActivity)context).getSupportFragmentManager().beginTransaction();
DisplayStatusFragment displayStatusFragment = (DisplayStatusFragment) mPageReferenceMap.get(tl.getPosition());
if( displayStatusFragment == null)
return;
fragTransaction.detach(displayStatusFragment);
Bundle bundle = new Bundle();
bundle.putString("remote_instance", tl.getRemoteInstance().getHost()!=null?tl.getRemoteInstance().getHost():"");
bundle.putString("instanceType", tl.getRemoteInstance().getType());
bundle.putInt("timelineId", tl.getId());
bundle.putString("currentfilter", tl.getRemoteInstance().getFilteredWith());
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE);
displayStatusFragment.setArguments(bundle);
fragTransaction.attach(displayStatusFragment);
fragTransaction.commit();
}
}
});
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
changes[0] = true;
switch (item.getItemId()) {
case R.id.action_add_tags:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
@SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.tags_instance, null);
dialogBuilder.setView(dialogView);
final EditText editText = dialogView.findViewById(R.id.filter_words);
if(remoteInstance.getTags() != null) {
String valuesTag = "";
for(String val: remoteInstance.getTags())
valuesTag += val+" ";
editText.setText(valuesTag);
editText.setSelection(editText.getText().toString().length());
}
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String[] values = editText.getText().toString().trim().split("\\s+");
java.util.List<String> tags =
new ArrayList<>(Arrays.asList(values));
remoteInstance.setTags(tags);
new InstancesDAO(context, db).updateInstance(remoteInstance);
tl.setRemoteInstance(remoteInstance);
new TimelinesDAO(context, db).updateTag(tl);
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
break;
}
return false;
}
});
popup.show();
}
}

View File

@ -18,6 +18,8 @@ package app.fedilab.android.client.Entities;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.List;
/**
* Created by Thomas on 05/10/2018.
* Manages following instances
@ -29,6 +31,8 @@ public class RemoteInstance implements Parcelable {
private String type;
private String id;
private String dbID;
private List<String> tags;
private String filteredWith;
public RemoteInstance(){}
@ -65,6 +69,22 @@ public class RemoteInstance implements Parcelable {
this.dbID = dbID;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public String getFilteredWith() {
return filteredWith;
}
public void setFilteredWith(String filteredWith) {
this.filteredWith = filteredWith;
}
@Override
public int describeContents() {
return 0;
@ -76,6 +96,8 @@ public class RemoteInstance implements Parcelable {
dest.writeString(this.type);
dest.writeString(this.id);
dest.writeString(this.dbID);
dest.writeStringList(this.tags);
dest.writeString(this.filteredWith);
}
protected RemoteInstance(Parcel in) {
@ -83,9 +105,11 @@ public class RemoteInstance implements Parcelable {
this.type = in.readString();
this.id = in.readString();
this.dbID = in.readString();
this.tags = in.createStringArrayList();
this.filteredWith = in.readString();
}
public static final Parcelable.Creator<RemoteInstance> CREATOR = new Parcelable.Creator<RemoteInstance>() {
public static final Creator<RemoteInstance> CREATOR = new Creator<RemoteInstance>() {
@Override
public RemoteInstance createFromParcel(Parcel source) {
return new RemoteInstance(source);

View File

@ -25,6 +25,7 @@ import java.util.Date;
import java.util.List;
import app.fedilab.android.client.Entities.RemoteInstance;
import app.fedilab.android.client.Entities.TagTimeline;
import app.fedilab.android.helper.Helper;
@ -64,6 +65,28 @@ public class InstancesDAO {
}catch (Exception ignored) {}
}
//------- UPDATES -------
/**
* update instance in database
* @param remoteInstance RemoteInstance
*/
public void updateInstance(RemoteInstance remoteInstance) {
ContentValues values = new ContentValues();
List<String> tags = remoteInstance.getTags();
values.put(Sqlite.COL_FILTERED_WITH, remoteInstance.getFilteredWith());
if( tags != null && tags.size() > 0) {
values.put(Sqlite.COL_TAGS, Helper.arrayToStringStorage(tags));
}
try{
db.update(Sqlite.TABLE_INSTANCES, values, Sqlite.COL_INSTANCE + " = ? ", new String[]{String.valueOf(remoteInstance.getHost())});
}catch (Exception ignored) {ignored.printStackTrace();}
}
public void insertInstance(String instanceName, String type) {
insertInstance(instanceName, "null", type);
}
@ -102,6 +125,7 @@ public class InstancesDAO {
Cursor c = db.query(Sqlite.TABLE_INSTANCES, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
return cursorToListSearch(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@ -136,7 +160,11 @@ public class InstancesDAO {
RemoteInstance remoteInstance = new RemoteInstance();
remoteInstance.setDbID(c.getString(c.getColumnIndex(Sqlite.COL_ID)));
remoteInstance.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
try {
remoteInstance.setTags(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS))));
}catch (Exception ignored){}
remoteInstance.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
remoteInstance.setFilteredWith(c.getString(c.getColumnIndex(Sqlite.COL_FILTERED_WITH)));
remoteInstance.setType(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)) == null?"MASTODON":c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)));
remoteInstances.add(remoteInstance);
}

View File

@ -45,7 +45,7 @@ import app.fedilab.android.R;
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 30;
public static final int DB_VERSION = 31;
public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db;
private static Sqlite sInstance;
@ -89,6 +89,8 @@ public class Sqlite extends SQLiteOpenHelper {
//Table for timelines
public static final String TABLE_TIMELINES = "TIMELINES";
//Table for timelines
public static final String TABLE_REMOTE_INSTANCE_TAGS = "REMOTE_INSTANCE_TAGS";
static final String COL_USER_ID = "USER_ID";
@ -225,9 +227,10 @@ public class Sqlite extends SQLiteOpenHelper {
+ TABLE_STATUSES_CACHE + "(" + COL_INSTANCE +"," + COL_STATUS_ID + ")";
static final String COL_INSTANCE_TYPE = "INSTANCE_TYPE";
static final String COL_FILTERED_WITH = "FILTERED_WITH";
private final String CREATE_TABLE_INSTANCES = "CREATE TABLE " + TABLE_INSTANCES + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_INSTANCE + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, " + COL_INSTANCE_TYPE + " TEXT, " + COL_DATE_CREATION + " TEXT NOT NULL)";
+ COL_INSTANCE + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, " + COL_INSTANCE_TYPE + " TEXT, " + COL_TAGS + " TEXT, " + COL_FILTERED_WITH + " TEXT, "+ COL_DATE_CREATION + " TEXT NOT NULL)";
static final String COL_UUID = "UUID";
@ -290,6 +293,9 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_CACHE + " TEXT NOT NULL, "
+ COL_DATE + " TEXT NOT NULL)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@ -410,6 +416,11 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE_USER_ACCOUNT + " ADD COLUMN " + COL_SENSITIVE + " INTEGER DEFAULT 0");
case 29:
db.execSQL(CREATE_TABLE_TIMELINE_CACHE);
case 30:
if( oldVersion > 11) {
db.execSQL("ALTER TABLE " + TABLE_INSTANCES + " ADD COLUMN " + COL_TAGS + " TEXT");
db.execSQL("ALTER TABLE " + TABLE_INSTANCES + " ADD COLUMN " + COL_FILTERED_WITH + " 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/filter_words"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/some_tags"
android:maxLines="1"
/>
</LinearLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<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_add_tags"
app:showAsAction="always"
android:title="@string/add_tags" />
</menu>

View File

@ -800,6 +800,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="some_tags">Add some words to filter (space-separated)</string>
<string name="change_tag_column">Change column name</string>
<string name="no_misskey_instance">No Misskey instances</string>
<string name="misskey_instance">Misskey instance</string>
@ -946,6 +947,7 @@
<string name="set_blur_sensitive">Blur sensitive media</string>
<string name="set_display_timeline_in_list">Display timelines in a list</string>
<string name="display_timeline">Display timelines</string>
<string name="add_tags">Add tags</string>
<plurals name="number_of_vote">
<item quantity="one">%d vote</item>