Browse Source

Sync unread state of offline articles if they're read in online mode.

Eduardo M KALINOWSKI 5 months ago
parent
commit
499f9c8bdc

+ 16 - 8
org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java

@@ -29,14 +29,12 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.EditText;
 import android.widget.TextView;
-
-import com.google.gson.Gson;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.reflect.TypeToken;
+import androidx.appcompat.view.ActionMode;
+import androidx.appcompat.widget.Toolbar;
 
 import org.fox.ttrss.offline.OfflineActivity;
 import org.fox.ttrss.offline.OfflineDownloadService;
+import org.fox.ttrss.offline.OfflineState;
 import org.fox.ttrss.offline.OfflineUploadService;
 import org.fox.ttrss.share.SubscribeActivity;
 import org.fox.ttrss.types.Article;
@@ -49,8 +47,10 @@ import java.lang.reflect.Type;
 import java.util.HashMap;
 import java.util.List;
 
-import androidx.appcompat.view.ActionMode;
-import androidx.appcompat.widget.Toolbar;
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.reflect.TypeToken;
 
 public class OnlineActivity extends CommonActivity {
 	private final String TAG = this.getClass().getSimpleName();
@@ -68,6 +68,8 @@ public class OnlineActivity extends CommonActivity {
 	private String m_lastImageHitTestUrl;
 	private ConnectivityManager m_cmgr;
 
+	private OfflineState m_offlineState;
+
 	//protected PullToRefreshAttacher m_pullToRefreshAttacher;
 
 	protected abstract class OnLoginFinishedListener {
@@ -156,6 +158,8 @@ public class OnlineActivity extends CommonActivity {
 
 		m_cmgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 
+		m_offlineState = new OfflineState(this);
+
 		Intent intent = getIntent();
 
         Log.d(TAG, "intent action=" + intent.getAction());
@@ -928,6 +932,7 @@ public class OnlineActivity extends CommonActivity {
 				}
 			};
 			req.execute(map);
+			m_offlineState.syncUnreadState(articles);
 		}
 	}
 
@@ -1161,6 +1166,8 @@ public class OnlineActivity extends CommonActivity {
 		};
 
 		req.execute(map);
+
+		m_offlineState.syncUnreadState(article);
 	}
 
 	public void saveArticleMarked(final Article article) {
@@ -1237,7 +1244,7 @@ public class OnlineActivity extends CommonActivity {
 
 		return tmp.replaceAll(",$", "");
 	}
-	
+
 	public void shareArticle(Article article) {
 		if (article != null) {
 			shareText(article.link, article.title);
@@ -1334,6 +1341,7 @@ public class OnlineActivity extends CommonActivity {
 		};
 
 		req.execute(map);
+		m_offlineState.syncUnreadState(articles);
 		//refresh();
 	}
 

+ 38 - 0
org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineState.java

@@ -0,0 +1,38 @@
+package org.fox.ttrss.offline;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteStatement;
+import android.provider.BaseColumns;
+
+import org.fox.ttrss.types.Article;
+import org.fox.ttrss.types.ArticleList;
+import org.fox.ttrss.util.DatabaseHelper;
+
+public class OfflineState {
+    private DatabaseHelper m_databaseHelper;
+
+    public OfflineState(Context context) {
+        m_databaseHelper = DatabaseHelper.getInstance(context);
+    }
+
+    public void syncUnreadState(Article article) {
+        String val = article.unread ? "1" : "0";
+        SQLiteStatement stmt = getDatabase().compileStatement(
+                "UPDATE articles SET unread = " + val + " WHERE " + BaseColumns._ID + " = ?");
+
+        stmt.bindLong(1, article.id);
+        stmt.execute();
+        stmt.close();
+    }
+
+    public void syncUnreadState(ArticleList articles) {
+        for (Article article: articles) {
+            syncUnreadState(article);
+        }
+    }
+
+    private SQLiteDatabase getDatabase() {
+        return m_databaseHelper.getWritableDatabase();
+    }
+}