2019-11-15 15:06:02 +01:00
|
|
|
/* Copyright 2019 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Fedilab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Fedilab 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 Fedilab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package app.fedilab.android.asynctasks;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.interfaces.OnSyncBookmarksInterface;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import app.fedilab.android.sqlite.StatusCacheDAO;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 15/11/2019.
|
|
|
|
* Sync bookmarks
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
private List<app.fedilab.android.client.Entities.Status> statusList;
|
|
|
|
private OnSyncBookmarksInterface listener;
|
|
|
|
private WeakReference<Context> contextReference;
|
|
|
|
private sync type;
|
2020-03-08 10:29:06 +01:00
|
|
|
|
2019-11-15 15:06:02 +01:00
|
|
|
public SyncBookmarksAsyncTask(Context context, sync type, OnSyncBookmarksInterface onSyncBookmarksInterface) {
|
|
|
|
this.contextReference = new WeakReference<>(context);
|
|
|
|
this.type = type;
|
|
|
|
this.listener = onSyncBookmarksInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
2020-04-09 18:57:12 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-11-15 16:32:25 +01:00
|
|
|
if (type == sync.IMPORT) {
|
2019-11-15 15:06:02 +01:00
|
|
|
String max_id = null;
|
|
|
|
do {
|
|
|
|
APIResponse apiResponse = new API(contextReference.get()).getBookmarks(max_id);
|
|
|
|
max_id = apiResponse.getMax_id();
|
|
|
|
List<app.fedilab.android.client.Entities.Status> statuses = apiResponse.getStatuses();
|
|
|
|
for (app.fedilab.android.client.Entities.Status tmpStatus : statuses) {
|
|
|
|
app.fedilab.android.client.Entities.Status status = new StatusCacheDAO(contextReference.get(), db).getStatus(StatusCacheDAO.BOOKMARK_CACHE, tmpStatus.getId());
|
2019-11-15 16:32:25 +01:00
|
|
|
if (status == null) {
|
2019-11-15 15:06:02 +01:00
|
|
|
new StatusCacheDAO(contextReference.get(), db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, tmpStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Thread.sleep(200);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
SystemClock.sleep(200);
|
|
|
|
}
|
|
|
|
} while (max_id != null);
|
2019-11-15 16:32:25 +01:00
|
|
|
} else {
|
2019-11-15 15:06:02 +01:00
|
|
|
List<app.fedilab.android.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
|
2019-11-15 16:32:25 +01:00
|
|
|
if (statuses != null) {
|
2019-11-15 15:06:02 +01:00
|
|
|
for (app.fedilab.android.client.Entities.Status tmpStatus : statuses) {
|
|
|
|
new API(contextReference.get()).postAction(API.StatusAction.BOOKMARK, tmpStatus.getId());
|
|
|
|
try {
|
|
|
|
Thread.sleep(200);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
SystemClock.sleep(200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusList = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
listener.onRetrieveBookmarks(statusList);
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
public enum sync {
|
|
|
|
EXPORT,
|
|
|
|
IMPORT
|
|
|
|
}
|
|
|
|
|
2019-11-15 15:06:02 +01:00
|
|
|
}
|