finalized deep link for user profile

This commit is contained in:
nuclearfog 2020-06-04 18:15:41 +02:00
parent 627a7af8c3
commit 53bce045d3
No known key found for this signature in database
GPG Key ID: ED35E22099354A64
12 changed files with 152 additions and 69 deletions

View File

@ -23,8 +23,6 @@ import com.google.android.material.tabs.TabLayout.Tab;
import org.nuclearfog.twidda.R; import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.adapter.FragmentAdapter; import org.nuclearfog.twidda.adapter.FragmentAdapter;
import org.nuclearfog.twidda.backend.LinkContentLoader; import org.nuclearfog.twidda.backend.LinkContentLoader;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.helper.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings; import org.nuclearfog.twidda.database.GlobalSettings;
import static android.view.Window.FEATURE_NO_TITLE; import static android.view.Window.FEATURE_NO_TITLE;
@ -241,9 +239,4 @@ public class MainActivity extends AppCompatActivity implements OnTabSelectedList
else else
loadingCircle.dismiss(); loadingCircle.dismiss();
} }
public void onError(EngineException error) {
ErrorHandler.handleFailure(this, error);
}
} }

View File

@ -164,12 +164,11 @@ public class MediaViewer extends AppCompatActivity implements OnImageClickListen
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
if (type == MEDIAVIEWER_ANGIF) { if (type == MEDIAVIEWER_ANGIF) {
mp.setLooping(true); mp.setLooping(true);
mp.start();
} else { } else {
videoController.show(0); videoController.show(0);
mp.seekTo(videoPos); mp.seekTo(videoPos);
mp.start();
} }
mp.start();
mp.setOnInfoListener(new OnInfoListener() { mp.setOnInfoListener(new OnInfoListener() {
@Override @Override

View File

@ -68,7 +68,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
OnTagClickListener, OnTabSelectedListener { OnTagClickListener, OnTabSelectedListener {
public static final String KEY_PROFILE_ID = "profile_id"; public static final String KEY_PROFILE_ID = "profile_id";
// public static final String KEY_PROFILE_NAME = "profile_name"; // TODO public static final String KEY_PROFILE_NAME = "profile_name";
public static final int RETURN_PROFILE_CHANGED = 2; public static final int RETURN_PROFILE_CHANGED = 2;
private static final int REQUEST_PROFILE_CHANGED = 1; private static final int REQUEST_PROFILE_CHANGED = 1;
private static final int TRANSPARENCY = 0xafffffff; private static final int TRANSPARENCY = 0xafffffff;
@ -157,17 +157,23 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
Bundle param = getIntent().getExtras(); Bundle param = getIntent().getExtras();
if (profileAsync == null && param != null && param.containsKey(KEY_PROFILE_ID)) { if (profileAsync == null && param != null) {
profileAsync = new ProfileLoader(this, LDR_PROFILE);
if (param.containsKey(KEY_PROFILE_ID)) {
long userId = param.getLong(KEY_PROFILE_ID); long userId = param.getLong(KEY_PROFILE_ID);
adapter.setupProfilePage(userId); adapter.setupProfilePage(userId);
profileAsync.execute(userId);
} else {
String username = param.getString(KEY_PROFILE_NAME);
adapter.setupProfilePage(username);
profileAsync.execute(username);
}
Tab tweetTab = tabLayout.getTabAt(0); Tab tweetTab = tabLayout.getTabAt(0);
Tab favorTab = tabLayout.getTabAt(1); Tab favorTab = tabLayout.getTabAt(1);
if (tweetTab != null && favorTab != null) { if (tweetTab != null && favorTab != null) {
tweetTab.setCustomView(tweetTabTxt); tweetTab.setCustomView(tweetTabTxt);
favorTab.setCustomView(favorTabTxt); favorTab.setCustomView(favorTabTxt);
} }
profileAsync = new ProfileLoader(this, LDR_PROFILE);
profileAsync.execute(userId);
} }
} }

View File

@ -98,6 +98,22 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
} }
public void setupProfilePage(String username) {
Bundle usr_tweet = new Bundle();
Bundle usr_favor = new Bundle();
usr_tweet.putString(KEY_FRAG_TWEET_SEARCH, username);
usr_favor.putString(KEY_FRAG_TWEET_SEARCH, username);
usr_tweet.putInt(KEY_FRAG_TWEET_MODE, TWEET_FRAG_TWEETS);
usr_favor.putInt(KEY_FRAG_TWEET_MODE, TWEET_FRAG_FAVORS);
fragments = new Fragment[2];
fragments[0] = new TweetFragment();
fragments[1] = new TweetFragment();
fragments[0].setArguments(usr_tweet);
fragments[1].setArguments(usr_favor);
notifyDataSetChanged();
}
public void setupSearchPage(String search) { public void setupSearchPage(String search) {
Bundle tweetSearch = new Bundle(); Bundle tweetSearch = new Bundle();
Bundle userSearch = new Bundle(); Bundle userSearch = new Bundle();
@ -208,7 +224,7 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
public void notifySettingsChanged() { public void notifySettingsChanged() {
for (Fragment fragment : fragments) { for (Fragment fragment : fragments) {
if (fragment instanceof FragmentChangeObserver) if (fragment instanceof FragmentChangeObserver)
((FragmentChangeObserver) fragment).onSettingsChange(); ((FragmentChangeObserver) fragment).onReset();
} }
} }
@ -217,9 +233,10 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
* @param index tab position of page * @param index tab position of page
*/ */
public void scrollToTop(int index) { public void scrollToTop(int index) {
if (fragments[index] instanceof FragmentChangeObserver) if (fragments[index] instanceof FragmentChangeObserver) {
((FragmentChangeObserver) fragments[index]).onTabChange(); ((FragmentChangeObserver) fragments[index]).onTabChange();
} }
}
public interface FragmentChangeObserver { public interface FragmentChangeObserver {
@ -227,7 +244,7 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
/** /**
* called if settings changed to refresh fragments * called if settings changed to refresh fragments
*/ */
void onSettingsChange(); void onReset();
/** /**
* called when the current tab changes * called when the current tab changes

View File

@ -6,22 +6,17 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.activity.MainActivity; import org.nuclearfog.twidda.activity.MainActivity;
import org.nuclearfog.twidda.activity.SearchPage; import org.nuclearfog.twidda.activity.SearchPage;
import org.nuclearfog.twidda.activity.TweetDetail; import org.nuclearfog.twidda.activity.TweetDetail;
import org.nuclearfog.twidda.activity.UserProfile; import org.nuclearfog.twidda.activity.UserProfile;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.engine.TwitterEngine;
import org.nuclearfog.twidda.backend.items.TwitterUser;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import static org.nuclearfog.twidda.activity.SearchPage.KEY_SEARCH_QUERY; import static org.nuclearfog.twidda.activity.SearchPage.KEY_SEARCH_QUERY;
import static org.nuclearfog.twidda.activity.TweetDetail.KEY_TWEET_ID; import static org.nuclearfog.twidda.activity.TweetDetail.KEY_TWEET_ID;
import static org.nuclearfog.twidda.activity.TweetDetail.KEY_TWEET_NAME; import static org.nuclearfog.twidda.activity.TweetDetail.KEY_TWEET_NAME;
import static org.nuclearfog.twidda.activity.UserProfile.KEY_PROFILE_ID; import static org.nuclearfog.twidda.activity.UserProfile.KEY_PROFILE_NAME;
/** /**
* This class handles deep links and starts activities to show the content * This class handles deep links and starts activities to show the content
@ -33,14 +28,10 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
private static final String TWEET_PATH = "[\\w]+/status/\\d+"; private static final String TWEET_PATH = "[\\w]+/status/\\d+";
private static final String USER_PATH = "[\\w]+/?(\\bwith_replies\\b|\\bmedia\\b|\\blikes\\b)?"; private static final String USER_PATH = "[\\w]+/?(\\bwith_replies\\b|\\bmedia\\b|\\blikes\\b)?";
@Nullable
private EngineException err;
private WeakReference<MainActivity> callback; private WeakReference<MainActivity> callback;
private TwitterEngine mTwitter;
public LinkContentLoader(MainActivity callback) { public LinkContentLoader(MainActivity callback) {
this.callback = new WeakReference<>(callback); this.callback = new WeakReference<>(callback);
mTwitter = TwitterEngine.getInstance(callback);
} }
@Override @Override
@ -65,12 +56,10 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
data.putString(KEY_TWEET_NAME, name); data.putString(KEY_TWEET_NAME, name);
return new DataHolder(data, TweetDetail.class); return new DataHolder(data, TweetDetail.class);
} else if (path.matches(USER_PATH) && link.getQuery() == null) { } else if (path.matches(USER_PATH) && link.getQuery() == null) {
String name = '@' + path; int end = path.indexOf("/");
int end = name.indexOf("/");
if (end > 0) if (end > 0)
name = name.substring(0, end); path = path.substring(0, end);
TwitterUser user = mTwitter.getUser(name); data.putString(KEY_PROFILE_NAME, path);
data.putLong(KEY_PROFILE_ID, user.getId());
return new DataHolder(data, UserProfile.class); return new DataHolder(data, UserProfile.class);
} else if (path.startsWith("search")) { } else if (path.startsWith("search")) {
String search = link.getQueryParameter("q"); String search = link.getQueryParameter("q");
@ -87,8 +76,6 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
} }
} }
} }
} catch (EngineException err) {
this.err = err;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -104,8 +91,6 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
Intent intent = new Intent(callback.get(), result.activity); Intent intent = new Intent(callback.get(), result.activity);
intent.putExtras(result.data); intent.putExtras(result.data);
callback.get().startActivity(intent); callback.get().startActivity(intent);
} else if (err != null) {
callback.get().onError(err);
} }
} }
} }

View File

@ -59,8 +59,9 @@ public class ProfileLoader extends AsyncTask<Object, TwitterUser, UserProperties
case LDR_PROFILE: case LDR_PROFILE:
if (userId > 0) { if (userId > 0) {
user = db.getUser(userId); user = db.getUser(userId);
if (user != null) if (user != null) {
publishProgress(user); publishProgress(user);
}
user = mTwitter.getUser(userId); user = mTwitter.getUser(userId);
} else { } else {
user = mTwitter.getUser(username); user = mTwitter.getUser(username);
@ -68,20 +69,27 @@ public class ProfileLoader extends AsyncTask<Object, TwitterUser, UserProperties
publishProgress(user); publishProgress(user);
db.storeUser(user); db.storeUser(user);
if (userId > 0) {
connection = mTwitter.getConnection(userId); connection = mTwitter.getConnection(userId);
if (!connection.isHome()) } else {
if (connection.isBlocked() || connection.isMuted()) connection = mTwitter.getConnection(username);
}
if (!connection.isHome()) {
if (connection.isBlocked() || connection.isMuted()) {
db.muteUser(userId, true); db.muteUser(userId, true);
else } else {
db.muteUser(userId, false); db.muteUser(userId, false);
}
}
return connection; return connection;
case ACTION_FOLLOW: case ACTION_FOLLOW:
connection = mTwitter.getConnection(userId); connection = mTwitter.getConnection(userId);
if (!connection.isFriend()) if (!connection.isFriend()) {
user = mTwitter.followUser(userId); user = mTwitter.followUser(userId);
else } else {
user = mTwitter.unfollowUser(userId); user = mTwitter.unfollowUser(userId);
}
publishProgress(user); publishProgress(user);
return mTwitter.getConnection(userId); return mTwitter.getConnection(userId);

View File

@ -63,10 +63,14 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
@Override @Override
protected List<Tweet> doInBackground(Object[] param) { protected List<Tweet> doInBackground(Object[] param) {
List<Tweet> tweets = null; List<Tweet> tweets = null;
String search;
int page;
long id;
try { try {
switch (action) { switch (action) {
case TL_HOME: case TL_HOME:
int page = (int) param[0]; page = (int) param[0];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getHomeTimeline(); tweets = db.getHomeTimeline();
if (tweets.isEmpty()) { if (tweets.isEmpty()) {
@ -94,8 +98,9 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
break; break;
case USR_TWEETS: case USR_TWEETS:
long id = (long) param[0];
page = (int) param[1]; page = (int) param[1];
if (param[0] instanceof Long) {
id = (long) param[0];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getUserTweets(id); tweets = db.getUserTweets(id);
if (tweets.isEmpty()) { if (tweets.isEmpty()) {
@ -106,11 +111,17 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
tweets = mTwitter.getUserTweets(id, sinceId, page); tweets = mTwitter.getUserTweets(id, sinceId, page);
db.storeUserTweets(tweets); db.storeUserTweets(tweets);
} }
} else if (param[0] instanceof String) {
search = (String) param[0];
tweets = mTwitter.getUserTweets(search, sinceId, page);
db.storeUserTweets(tweets);
}
break; break;
case USR_FAVORS: case USR_FAVORS:
id = (long) param[0];
page = (int) param[1]; page = (int) param[1];
if (param[0] instanceof Long) {
id = (long) param[0];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getUserFavs(id); tweets = db.getUserFavs(id);
if (tweets.isEmpty()) { if (tweets.isEmpty()) {
@ -121,6 +132,10 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
tweets = mTwitter.getUserFavs(id, page); tweets = mTwitter.getUserFavs(id, page);
db.storeUserFavs(tweets, id); db.storeUserFavs(tweets, id);
} }
} else if (param[0] instanceof String) {
search = (String) param[0];
tweets = mTwitter.getUserFavs(search, page);
}
break; break;
case DB_ANS: case DB_ANS:
@ -130,7 +145,7 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
case TWEET_ANS: case TWEET_ANS:
id = (long) param[0]; id = (long) param[0];
String search = (String) param[1]; search = (String) param[1];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getAnswers(id); tweets = db.getAnswers(id);
if (tweets.isEmpty()) { if (tweets.isEmpty()) {

View File

@ -328,6 +328,26 @@ public class TwitterEngine {
} }
/**
* Get User Tweets
*
* @param username screen name of the user
* @param sinceId minimum tweet ID
* @param page current page
* @return List of User Tweets
* @throws EngineException if access is unavailable
*/
public List<Tweet> getUserTweets(String username, long sinceId, int page) throws EngineException {
try {
int load = settings.getRowLimit();
Paging paging = new Paging(page, load, sinceId);
return convertStatusList(twitter.getUserTimeline(username, paging));
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/** /**
* Get User Favs * Get User Favs
* *
@ -348,6 +368,26 @@ public class TwitterEngine {
} }
/**
* Get User Favs
*
* @param username screen name of the user
* @param page current page
* @return List of User Favs
* @throws EngineException if access is unavailable
*/
public List<Tweet> getUserFavs(String username, int page) throws EngineException {
try {
int load = settings.getRowLimit();
Paging paging = new Paging(page, load);
List<Status> favorits = twitter.getFavorites(username, paging);
return convertStatusList(favorits);
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/** /**
* Get User Context * Get User Context
* *
@ -411,6 +451,22 @@ public class TwitterEngine {
} }
/**
* Efficient Access of Connection Information
*
* @param username screen name of the user
* @return User Properties
* @throws EngineException if Connection is unavailable
*/
public UserProperties getConnection(String username) throws EngineException {
try {
return new UserProperties(twitter.showFriendship(twitter.getScreenName(), username));
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/** /**
* Follow Twitter user * Follow Twitter user
* *

View File

@ -89,10 +89,8 @@ public class TwitterTrend {
public String getSearchString() { public String getSearchString() {
if (trendName.startsWith("#")) if (trendName.startsWith("#"))
return trendName; return trendName;
else {
if (!trendName.startsWith("\"") && !trendName.endsWith("\"")) if (!trendName.startsWith("\"") && !trendName.endsWith("\""))
return "\"" + trendName + "\""; return "\"" + trendName + "\"";
return trendName; return trendName;
} }
}
} }

View File

@ -95,7 +95,7 @@ public class TrendFragment extends Fragment implements OnRefreshListener, TrendC
@Override @Override
public void onSettingsChange() { public void onReset() {
if (getView() != null) { if (getView() != null) {
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor()); reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
list.setAdapter(adapter); // force redrawing list list.setAdapter(adapter); // force redrawing list

View File

@ -127,7 +127,7 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
@Override @Override
public void onSettingsChange() { public void onReset() {
if (getView() != null) { if (getView() != null) {
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor()); reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
list.setAdapter(adapter); // force redrawing list list.setAdapter(adapter); // force redrawing list
@ -222,12 +222,18 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
case TWEET_FRAG_TWEETS: case TWEET_FRAG_TWEETS:
tweetTask = new TweetListLoader(this, Action.USR_TWEETS); tweetTask = new TweetListLoader(this, Action.USR_TWEETS);
if (param.containsKey(KEY_FRAG_TWEET_ID))
tweetTask.execute(id, 1); tweetTask.execute(id, 1);
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH))
tweetTask.execute(search, 1);
break; break;
case TWEET_FRAG_FAVORS: case TWEET_FRAG_FAVORS:
tweetTask = new TweetListLoader(this, Action.USR_FAVORS); tweetTask = new TweetListLoader(this, Action.USR_FAVORS);
if (param.containsKey(KEY_FRAG_TWEET_ID))
tweetTask.execute(id, 1); tweetTask.execute(id, 1);
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH))
tweetTask.execute(search, 1);
break; break;
case TWEET_FRAG_ANSWER: case TWEET_FRAG_ANSWER:

View File

@ -114,7 +114,7 @@ public class UserFragment extends Fragment implements OnRefreshListener, UserCli
@Override @Override
public void onSettingsChange() { public void onReset() {
} }
/** /**