Skip to content

Commit

Permalink
#384 Timeline replaced TimelineType and other fields in TimelineListP…
Browse files Browse the repository at this point in the history
…arameters
  • Loading branch information
yvolk committed Jun 5, 2016
1 parent 1ab5165 commit b752537
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void onClick(View v) {

private void showDefaultAccountCheckbox() {
MyAccount ma = state.getAccount();
boolean isDefaultAccount = ma.getUserId() == MyContextHolder.get().persistentAccounts().getDefaultAccountUserId();
boolean isDefaultAccount = ma.equals(MyContextHolder.get().persistentAccounts().getDefaultAccount());
CheckBox checkBox= (CheckBox) findFragmentViewById(R.id.is_default_account);
if (checkBox != null) {
checkBox.setVisibility(state.builder.isPersistent() ? View.VISIBLE : View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public MyAccount fromAccountName(String accountNameIn) {
return myAccount;
}


/**
* Get instance of current MyAccount (MyAccount selected by the user). The account isPersistent.
* As a side effect the function changes current account if old value is not valid.
Expand All @@ -205,8 +204,6 @@ public MyAccount getCurrentAccount() {
ma = fromAccountName(defaultAccountName);
if (!ma.isValid()) {
defaultAccountName = "";
}
if (!ma.isValid()) {
for (MyAccount myAccount : mAccounts.values()) {
if (myAccount.isValid()) {
ma = myAccount;
Expand Down Expand Up @@ -515,7 +512,7 @@ private void initializeMyFriends() {
myFriends = friends;
}

public long getDefaultAccountUserId() {
return fromAccountName(getDefaultAccountName()).getUserId();
public MyAccount getDefaultAccount() {
return fromAccountName(defaultAccountName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public final class MyContextImpl implements MyContext {
private volatile DatabaseHolder mDb;
private final PersistentAccounts mPersistentAccounts = PersistentAccounts.getEmpty();
private final PersistentOrigins mPersistentOrigins = PersistentOrigins.getEmpty();
private final PersistentTimelines persistentTimelines = PersistentTimelines.getEmpty();
private final PersistentTimelines persistentTimelines = PersistentTimelines.newEmpty(this);

private volatile boolean mExpired = false;

Expand Down Expand Up @@ -111,7 +111,7 @@ public MyContext newInitialized(Context context, String initializerName) {
myContext.mDb = newDb;
myContext.mPersistentOrigins.initialize(myContext);
myContext.mPersistentAccounts.initialize(myContext);
myContext.persistentTimelines.initialize(myContext);
myContext.persistentTimelines.initialize();
MyImageCache.initialize(myContext.context());
break;
default:
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/andstatus/app/data/ParsedUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.andstatus.app.data;

import android.net.Uri;
import android.support.annotation.NonNull;

import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.user.UserListType;
Expand Down Expand Up @@ -44,6 +45,14 @@ public String toString() {
return "Uri:'" + uri + "'; matched:" + matched();
}

public Uri getUri() {
return uri;
}

public boolean isEmpty() {
return uri == Uri.EMPTY;
}

public long getAccountUserId() {
long accountUserId = 0;
try {
Expand Down Expand Up @@ -183,6 +192,7 @@ public long getMessageId() {
return messageId;
}

@NonNull
public String getSearchQuery() {
String searchString = "";
try {
Expand Down
48 changes: 39 additions & 9 deletions app/src/main/java/org/andstatus/app/msg/PersistentTimelines.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import org.andstatus.app.account.MyAccount;
import org.andstatus.app.context.MyContext;
import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.context.MyPreferences;
import org.andstatus.app.data.DbUtils;
import org.andstatus.app.data.MyQuery;
import org.andstatus.app.data.ParsedUri;
import org.andstatus.app.data.TimelineType;
import org.andstatus.app.database.TimelineTable;
import org.andstatus.app.origin.Origin;
import org.andstatus.app.util.MyLog;
Expand All @@ -42,20 +44,17 @@
*/
public class PersistentTimelines {
private final List<Timeline> timelines = new ArrayList<>();
private MyContext myContext;

public static PersistentTimelines getEmpty() {
return new PersistentTimelines();
public static PersistentTimelines newEmpty(MyContext myContext) {
return new PersistentTimelines(myContext);
}

private PersistentTimelines() {
// Empty
private PersistentTimelines(MyContext myContext) {
this.myContext = myContext;
}

public PersistentTimelines initialize() {
return initialize(MyContextHolder.get());
}

public PersistentTimelines initialize(MyContext myContext) {
final String method = "initialize";
Context context = myContext.context();
timelines.clear();
Expand Down Expand Up @@ -100,6 +99,26 @@ public Timeline fromId(long id) {
return timelineFound;
}

@NonNull
public Timeline getDefaultForCurrentAccount() {
return fromTypeAndAccount(
MyPreferences.getDefaultTimeline(),
myContext.persistentAccounts().getCurrentAccount());
}

@NonNull
public Timeline fromTypeAndAccount(TimelineType timelineType, MyAccount myAccount) {
Timeline timelineFound = new Timeline(timelineType);
timelineFound.setAccount(myAccount);
for (Timeline timeline : timelines) {
if (timeline.getTimelineType() == timelineType && timeline.getAccount().equals(myAccount)) {
timelineFound = timeline;
break;
}
}
return timelineFound;
}

public List<Timeline> getList() {
return timelines;
}
Expand Down Expand Up @@ -177,4 +196,15 @@ public void onAccountDelete(MyAccount ma) {
}
timelines.removeAll(toRemove);
}

@NonNull
public Timeline fromParsedUri(ParsedUri parsedUri, String searchQuery) {
Timeline timelineNew = Timeline.fromParsedUri(myContext, parsedUri, searchQuery);
for (Timeline timeline : getList()) {
if (timeline.equals(timelineNew)) {
return timeline;
}
}
return timelineNew;
}
}
78 changes: 78 additions & 0 deletions app/src/main/java/org/andstatus/app/msg/Timeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.andstatus.app.account.MyAccount;
import org.andstatus.app.context.MyContext;
import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.data.DbUtils;
import org.andstatus.app.data.ParsedUri;
import org.andstatus.app.data.TimelineType;
import org.andstatus.app.database.TimelineTable;
import org.andstatus.app.origin.Origin;
Expand All @@ -41,12 +43,23 @@ public class Timeline implements Comparable<Timeline> {
private long id;
private String name;
private String description = "";

private final TimelineType timelineType;

private boolean allOrigins = false;
private Origin origin = Origin.Builder.buildUnknown();
private MyAccount account = MyAccount.getEmpty(MyContextHolder.get(), "(empty)");
/**
* Selected User for the {@link TimelineType#USER} timeline.
* This is either User Id of current account OR user id of any other selected user.
* So it's never == 0 for the {@link TimelineType#USER} timeline
*/
private long userId = 0;
/**
* The string is not empty if this timeline is filtered using query string
* ("Mentions" are not counted here because they have the separate TimelineType)
*/
@NonNull
private String searchQuery = "";

private boolean synced = true;
Expand Down Expand Up @@ -170,6 +183,10 @@ static Timeline getEmpty() {
return new Timeline(TimelineType.UNKNOWN);
}

public boolean isEmpty() {
return timelineType == TimelineType.UNKNOWN;
}

public boolean isValid() {
return timelineType != TimelineType.UNKNOWN && id != 0;
}
Expand Down Expand Up @@ -205,6 +222,12 @@ public MyAccount getAccount() {
return account;
}

public void setAccount(MyAccount account) {
if (id == 0 && account != null && account.isValid()) {
this.account = account;
}
}

public boolean isDisplayedInSelector() {
return displayedInSelector;
}
Expand Down Expand Up @@ -256,4 +279,59 @@ public String toString() {
", type=" + timelineType.save() +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Timeline)) return false;

Timeline timeline = (Timeline) o;

if (id != timeline.id) return false;
if (allOrigins != timeline.allOrigins) return false;
if (userId != timeline.userId) return false;
if (timelineType != timeline.timelineType) return false;
if (!origin.equals(timeline.origin)) return false;
if (!account.equals(timeline.account)) return false;
return searchQuery.equals(timeline.searchQuery);
}

@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + timelineType.hashCode();
result = 31 * result + (allOrigins ? 1 : 0);
result = 31 * result + origin.hashCode();
result = 31 * result + account.hashCode();
result = 31 * result + (int) (userId ^ (userId >>> 32));
result = 31 * result + searchQuery.hashCode();
return result;
}

public long getUserId() {
return userId;
}

public static Timeline fromParsedUri(MyContext myContext, ParsedUri parsedUri, String searchQuery) {
Timeline timeline = new Timeline(parsedUri.getTimelineType());
if (timeline.getTimelineType() == TimelineType.UNKNOWN ||
parsedUri.getAccountUserId() == 0) {
MyLog.e(Timeline.class,"fromParsedUri; uri:" + parsedUri.getUri()
+ ", " + timeline.getTimelineType()
+ ", accountId:" + parsedUri.getAccountUserId() );
return timeline;
}
timeline.account = myContext.persistentAccounts().fromUserId(parsedUri.getAccountUserId());
timeline.userId = parsedUri.getUserId();
timeline.searchQuery = parsedUri.getSearchQuery();
if (TextUtils.isEmpty(timeline.searchQuery) && !TextUtils.isEmpty(searchQuery)) {
timeline.searchQuery = searchQuery;
}
return timeline;
}

@NonNull
public String getSearchQuery() {
return searchQuery;
}
}

0 comments on commit b752537

Please sign in to comment.