Skip to content

Commit

Permalink
database fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Feb 6, 2024
1 parent a52f5cc commit d57ed18
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,35 @@ protected Result doInBackground(@NonNull Param param) {
return new Result(statuses, param.pos, null);

case Param.USER:
case Param.USER_ALL:
boolean withReplies = param.type == Param.USER_ALL;
if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) {
statuses = db.getUserTimeline(param.id);
if (statuses.isEmpty()) {
statuses = connection.getUserTimeline(param.id, 0L, 0L, withReplies);
statuses = connection.getUserTimeline(param.id, 0L, 0L, false);
db.saveUserTimeline(statuses);
}
} else {
statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, withReplies);
statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, false);
if (param.maxId == Param.NO_ID) {
db.saveUserTimeline(statuses);
}
}
return new Result(statuses, param.pos, null);

case Param.USER_REPLIES:
if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) {
statuses = db.getUserReplies(param.id);
if (statuses.isEmpty()) {
statuses = connection.getUserTimeline(param.id, 0L, 0L, true);
db.saveUserReplies(statuses);
}
} else {
statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, true);
if (param.maxId == Param.NO_ID) {
db.saveUserReplies(statuses);
}
}
return new Result(statuses, param.pos, null);

case Param.FAVORIT:
if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) {
statuses = db.getUserFavorites(param.id);
Expand Down Expand Up @@ -150,7 +163,7 @@ public static class Param {

public static final int HOME = 1;
public static final int USER = 2;
public static final int USER_ALL = 3;
public static final int USER_REPLIES = 3;
public static final int FAVORIT = 4;
public static final int REPLIES = 5;
public static final int REPLIES_LOCAL = 6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class AppDatabase {
+ " LIMIT ?;";

/**
* SQL query to get status of an user
* SQL query to get timeline of an user
*/
private static final String USER_STATUS_QUERY = "SELECT * FROM(" + STATUS_SUBQUERY + ")"
+ " WHERE " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.FLAGS + "&" + StatusPropertiesTable.MASK_STATUS_USER_TIMELINE + " IS NOT 0"
Expand All @@ -117,6 +117,17 @@ public class AppDatabase {
+ " ORDER BY " + StatusTable.ID + " DESC"
+ " LIMIT ?;";

/**
* SQL query to get timeline of an user with replies
*/
private static final String USER_REPLY_QUERY = "SELECT * FROM(" + STATUS_SUBQUERY + ")"
+ " WHERE " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.FLAGS + "&" + StatusPropertiesTable.MASK_STATUS_USER_REPLY + " IS NOT 0"
+ " AND " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.OWNER + "=?"
+ " AND " + UserPropertiesTable.TABLE + "." + UserPropertiesTable.OWNER + "=?"
+ " AND " + StatusTable.TABLE + "." + StatusTable.USER + "=?"
+ " ORDER BY " + StatusTable.ID + " DESC"
+ " LIMIT ?;";

/**
* SQL query to get status favored by an user
*/
Expand Down Expand Up @@ -359,6 +370,22 @@ public void saveUserTimeline(Statuses statuses) {
}
}

/**
* save user reply timeline
*
* @param statuses user timeline
*/
public void saveUserReplies(Statuses statuses) {
synchronized (adapter) {
if (!statuses.isEmpty()) {
SQLiteDatabase db = adapter.getDbWrite();
for (Status status : statuses)
saveStatus(status, db, StatusPropertiesTable.MASK_STATUS_USER_REPLY);
adapter.commit();
}
}
}

/**
* save user favorite timeline
*
Expand Down Expand Up @@ -693,6 +720,23 @@ public Statuses getUserTimeline(long userID) {
}
}

/**
* load user reply timeline
*
* @param userId user ID
* @return user timeline
*/
public Statuses getUserReplies(long userId) {
synchronized (adapter) {
String homeStr = Long.toString(settings.getLogin().getId());
String[] args = {homeStr, homeStr, Long.toString(userId), Integer.toString(settings.getListSize())};

SQLiteDatabase db = adapter.getDbRead();
Cursor cursor = db.rawQuery(USER_REPLY_QUERY, args);
return getStatuses(cursor, db);
}
}

/**
* load favorite timeline
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,11 @@ public interface StatusPropertiesTable {
* status is pinned to profile
*/
int MASK_STATUS_PINNED = 1 << 13;

/**
* status is from an user timeline (replies included)
*/
int MASK_STATUS_USER_REPLY = 1 << 14;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void load(long sinceId, long maxId, int index) {
break;

case MODE_USER_ALL:
request = new StatusLoader.Param(StatusLoader.Param.USER_ALL, id, sinceId, maxId, index, search);
request = new StatusLoader.Param(StatusLoader.Param.USER_REPLIES, id, sinceId, maxId, index, search);
break;

case MODE_FAVORIT:
Expand Down

0 comments on commit d57ed18

Please sign in to comment.