Sync the timeline

This commit is contained in:
tom79 2019-04-21 12:09:30 +02:00
parent f59c53f711
commit c4ad9c9132
9 changed files with 330 additions and 18 deletions

View File

@ -114,6 +114,7 @@ import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Filters;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status;
@ -3484,4 +3485,15 @@ public abstract class BaseMainActivity extends BaseActivity
});
}
public void syncTimelines(Context context){
//Sync lists
ArrayList<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA ){
}
}
}

View File

@ -0,0 +1,187 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.TagTimeline;
import fr.gouv.etalab.mastodon.interfaces.OnSyncTimelineInterface;
import fr.gouv.etalab.mastodon.sqlite.InstancesDAO;
import fr.gouv.etalab.mastodon.sqlite.SearchDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.TimelinesDAO;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.DB_NAME;
/**
* Created by Thomas on 21/04/2019.
* Retrieves timelines
*/
public class SyncTimelinesAsyncTask extends AsyncTask<Void, Void, Void> {
private OnSyncTimelineInterface listener;
private WeakReference<Context> contextReference;
private List<ManageTimelines> manageTimelines;
public SyncTimelinesAsyncTask(Context context, OnSyncTimelineInterface onSyncTimelineInterface){
this.contextReference = new WeakReference<>(context);
this.listener = onSyncTimelineInterface;
}
@Override
protected Void doInBackground(Void... params) {
SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), DB_NAME, null, Sqlite.DB_VERSION).open();
manageTimelines = new TimelinesDAO(contextReference.get(), db).getAllTimelines();
//First time that the timeline is created
if( manageTimelines == null || manageTimelines.size() == 0){
//Add home TL
ManageTimelines manageHome = new ManageTimelines();
manageHome.setDisplayed(true);
manageHome.setType(ManageTimelines.Type.HOME);
manageHome.setPosition(0);
manageTimelines.add(manageHome);
//Add Direct notification TL
ManageTimelines manageNotif = new ManageTimelines();
manageNotif.setDisplayed(true);
manageNotif.setType(ManageTimelines.Type.NOTIFICATION);
manageNotif.setPosition(1);
manageTimelines.add(manageNotif);
//Add Direct message TL
ManageTimelines manageDirect = new ManageTimelines();
manageDirect.setDisplayed(true);
manageDirect.setType(ManageTimelines.Type.DIRECT);
manageDirect.setPosition(2);
manageTimelines.add(manageDirect);
//Add Local TL
ManageTimelines manageLocal = new ManageTimelines();
manageLocal.setDisplayed(true);
manageLocal.setType(ManageTimelines.Type.LOCAL);
manageLocal.setPosition(3);
manageTimelines.add(manageLocal);
//Add Public TL
ManageTimelines managePublic = new ManageTimelines();
managePublic.setDisplayed(true);
managePublic.setType(ManageTimelines.Type.PUBLIC);
managePublic.setPosition(4);
manageTimelines.add(managePublic);
//Add Public ART
ManageTimelines manageArt = new ManageTimelines();
manageArt.setDisplayed(true);
manageArt.setType(ManageTimelines.Type.ART);
manageArt.setPosition(5);
manageTimelines.add(manageArt);
//Add Public PEERTUBE
ManageTimelines managePeertube = new ManageTimelines();
managePeertube.setDisplayed(true);
managePeertube.setType(ManageTimelines.Type.ART);
managePeertube.setPosition(6);
manageTimelines.add(managePeertube);
int i = 6;
List<TagTimeline> tagTimelines = new SearchDAO(contextReference.get(), db).getAll();
if( tagTimelines != null && tagTimelines.size() > 0 ){
for(TagTimeline ttl: tagTimelines){
//Add tag timelines
ManageTimelines manageTagTimeline = new ManageTimelines();
manageTagTimeline.setDisplayed(true);
manageTagTimeline.setType(ManageTimelines.Type.TAG);
manageTagTimeline.setPosition(i++);
manageTagTimeline.setTagTimeline(ttl);
manageTimelines.add(manageTagTimeline);
}
}
List<RemoteInstance> instances = new InstancesDAO(contextReference.get(), db).getAllInstances();
if( instances != null && instances.size() > 0 ){
for(RemoteInstance ritl: instances){
//Add remote instances
ManageTimelines manageRemoteTimline = new ManageTimelines();
manageRemoteTimline.setDisplayed(true);
manageRemoteTimline.setType(ManageTimelines.Type.INSTANCE);
manageRemoteTimline.setPosition(i++);
manageRemoteTimline.setRemoteInstance(ritl);
manageTimelines.add(manageRemoteTimline);
}
}
}
APIResponse apiResponse = new API(contextReference.get()).getLists();
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = apiResponse.getLists();
if( lists != null && lists.size() > 0){
//Loop through results
for(fr.gouv.etalab.mastodon.client.Entities.List list: lists){
boolean isInDb = false;
ManageTimelines timelines_tmp = null;
for(ManageTimelines manageTimeline: manageTimelines){
if( manageTimeline.getListTimeline() == null )
continue;
if(manageTimeline.getListTimeline().getId().equals(list.getId())){
isInDb = true;
timelines_tmp = manageTimeline;
break;
}
}
if( !isInDb){
ManageTimelines manageTL = new ManageTimelines();
manageTL.setListTimeline(list);
manageTL.setDisplayed(true);
manageTL.setType(ManageTimelines.Type.LIST);
manageTL.setPosition(manageTimelines.size());
new TimelinesDAO(contextReference.get(), db).insert(manageTL);
}else{
//Update list
timelines_tmp.getListTimeline().setTitle(list.getTitle());
}
}
for(ManageTimelines manageTimelines: manageTimelines){
if( manageTimelines.getListTimeline() == null )
continue;
boolean shouldBeRemoved = true;
for(fr.gouv.etalab.mastodon.client.Entities.List list: lists){
if( list.getId().equals(manageTimelines.getListTimeline().getId())){
shouldBeRemoved = false;
}
}
if( shouldBeRemoved){
new TimelinesDAO(contextReference.get(), db).remove(manageTimelines);
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.syncedTimelines(manageTimelines);
}
}

View File

@ -14,14 +14,16 @@
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.client.Entities;
import java.io.Serializable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Thomas on 13/12/2017.
* Manage List
*/
public class List {
public class List implements Parcelable {
private String id;
private String title;
@ -41,4 +43,35 @@ public class List {
public void setTitle(String title) {
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.title);
}
public List() {
}
protected List(Parcel in) {
this.id = in.readString();
this.title = in.readString();
}
public static final Parcelable.Creator<List> CREATOR = new Parcelable.Creator<List>() {
@Override
public List createFromParcel(Parcel source) {
return new List(source);
}
@Override
public List[] newArray(int size) {
return new List[size];
}
};
}

View File

@ -22,11 +22,11 @@ public class ManageTimelines {
private int id;
private boolean displayed;
private Type type;
private String referencedBy;
private String userId;
private String instance;
private RemoteInstance remoteInstance;
private TagTimeline tagTimeline;
private List listTimeline;
public int getPosition() {
return position;
@ -53,14 +53,6 @@ public class ManageTimelines {
}
public String getReferencedBy() {
return referencedBy;
}
public void setReferencedBy(String referencedBy) {
this.referencedBy = referencedBy;
}
public int getId() {
return id;
}
@ -102,6 +94,14 @@ public class ManageTimelines {
this.userId = userId;
}
public List getListTimeline() {
return listTimeline;
}
public void setListTimeline(List listTimeline) {
this.listTimeline = listTimeline;
}
public enum Type{
HOME,
DIRECT,

View File

@ -177,6 +177,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Card;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Filters;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Status;
@ -2424,6 +2425,35 @@ public class Helper {
}
/**
* Unserialized a
* @param serializedListTimeline String serialized List
* @return List
*/
public static fr.gouv.etalab.mastodon.client.Entities.List restoreListtimelineFromString(String serializedListTimeline){
Gson gson = new Gson();
try {
return gson.fromJson(serializedListTimeline, fr.gouv.etalab.mastodon.client.Entities.List.class);
}catch (Exception e){
return null;
}
}
/**
* Serialized a List class
* @param listTimeline List to serialize
* @return String serialized List
*/
public static String listTimelineToStringStorage(fr.gouv.etalab.mastodon.client.Entities.List listTimeline){
Gson gson = new Gson();
try {
return gson.toJson(listTimeline);
}catch (Exception e){
return null;
}
}
/**
* Unserialized a TagTimeline
* @param serializedTagTimeline String serialized TagTimeline
@ -3871,11 +3901,15 @@ public class Helper {
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Map.Entry<T, E> entry : map.entrySet()) {
if (Objects.equals(value, entry.getValue())) {
return entry.getKey();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Objects.equals(value, entry.getValue())) {
return entry.getKey();
}
}
}
return null;
}
}

View File

@ -0,0 +1,28 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.interfaces;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
/**
* Created by Thomas on 22/04/2019.
* Interface when timelines are retrieved
*/
public interface OnSyncTimelineInterface {
void syncedTimelines(List<ManageTimelines> manageTimelines);
}

View File

@ -183,6 +183,21 @@ public class SearchDAO {
}
}
/**
* Returns TagTimeline information by its keyword in db
* @return info List<TagTimeline>
*/
public List<TagTimeline> getAll(){
try {
Cursor c = db.query(Sqlite.TABLE_SEARCH, null, Sqlite.COL_USER_ID + " = \"" + userId+ "\"", null, null, null, Sqlite.COL_KEYWORDS + " ASC", null);
return cursorToTagTimelineSearch(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns TagTimeline information by its keyword in db
* @return info List<TagTimeline>

View File

@ -259,7 +259,7 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_DOMAIN + " TEXT NOT NULL)";
static final String COL_TYPE = "TYPE";
static final String COL_REFERENCED_BY = "REFERENCED_BY";
static final String COL_LIST_TIMELINE = "LIST_TIMELINE";
static final String COL_DISPLAYED = "DISPLAYED";
static final String COL_POSITION = "POSITION";
static final String COL_REMOTE_INSTANCE = "REMOTE_INSTANCE";
@ -273,7 +273,7 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_REMOTE_INSTANCE + " TEXT, "
+ COL_TAG_TIMELINE + " TEXT, "
+ COL_DISPLAYED + " INTEGER NOT NULL, "
+ COL_REFERENCED_BY + " TEXT)";
+ COL_LIST_TIMELINE + " TEXT)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);

View File

@ -46,7 +46,6 @@ public class TimelinesDAO {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_TYPE, ManageTimelines.typeToDb(timeline.getType()));
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
values.put(Sqlite.COL_REFERENCED_BY, timeline.getReferencedBy());
values.put(Sqlite.COL_POSITION, timeline.getPosition());
values.put(Sqlite.COL_USER_ID, userId);
values.put(Sqlite.COL_INSTANCE, instance);
@ -54,6 +53,8 @@ public class TimelinesDAO {
values.put(Sqlite.COL_TAG_TIMELINE, Helper.tagTimelineToStringStorage(timeline.getTagTimeline()));
if( timeline.getRemoteInstance() != null)
values.put(Sqlite.COL_REMOTE_INSTANCE, Helper.remoteInstanceToStringStorage(timeline.getRemoteInstance()));
if( timeline.getListTimeline() != null)
values.put(Sqlite.COL_LIST_TIMELINE, Helper.listTimelineToStringStorage(timeline.getListTimeline()));
try{
db.insert(Sqlite.TABLE_TIMELINES, null, values);
}catch (Exception ignored) {}
@ -121,7 +122,8 @@ public class TimelinesDAO {
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
if( c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null )
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
manageTimelines.setReferencedBy(c.getString(c.getColumnIndex(Sqlite.COL_REFERENCED_BY)));
if( c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE)) != null )
manageTimelines.setListTimeline(Helper.restoreListtimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE))));
manageTimelines.setType(ManageTimelines.typeFromDb(c.getString(c.getColumnIndex(Sqlite.COL_TYPE))));
//Close the cursor
c.close();
@ -150,7 +152,8 @@ public class TimelinesDAO {
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
if( c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null )
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
manageTimelines.setReferencedBy(c.getString(c.getColumnIndex(Sqlite.COL_REFERENCED_BY)));
if( c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE)) != null )
manageTimelines.setListTimeline(Helper.restoreListtimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE))));
manageTimelines.setType(ManageTimelines.typeFromDb(c.getString(c.getColumnIndex(Sqlite.COL_TYPE))));
remoteInstances.add(manageTimelines);
}