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.adapter.FragmentAdapter;
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 static android.view.Window.FEATURE_NO_TITLE;
@ -241,9 +239,4 @@ public class MainActivity extends AppCompatActivity implements OnTabSelectedList
else
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) {
if (type == MEDIAVIEWER_ANGIF) {
mp.setLooping(true);
mp.start();
} else {
videoController.show(0);
mp.seekTo(videoPos);
mp.start();
}
mp.start();
mp.setOnInfoListener(new OnInfoListener() {
@Override

View File

@ -68,7 +68,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
OnTagClickListener, OnTabSelectedListener {
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;
private static final int REQUEST_PROFILE_CHANGED = 1;
private static final int TRANSPARENCY = 0xafffffff;
@ -157,17 +157,23 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
protected void onStart() {
super.onStart();
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);
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 favorTab = tabLayout.getTabAt(1);
if (tweetTab != null && favorTab != null) {
tweetTab.setCustomView(tweetTabTxt);
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) {
Bundle tweetSearch = new Bundle();
Bundle userSearch = new Bundle();
@ -208,7 +224,7 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
public void notifySettingsChanged() {
for (Fragment fragment : fragments) {
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
*/
public void scrollToTop(int index) {
if (fragments[index] instanceof FragmentChangeObserver)
if (fragments[index] instanceof FragmentChangeObserver) {
((FragmentChangeObserver) fragments[index]).onTabChange();
}
}
public interface FragmentChangeObserver {
@ -227,7 +244,7 @@ public class FragmentAdapter extends FragmentStatePagerAdapter {
/**
* called if settings changed to refresh fragments
*/
void onSettingsChange();
void onReset();
/**
* called when the current tab changes

View File

@ -6,22 +6,17 @@ import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.activity.MainActivity;
import org.nuclearfog.twidda.activity.SearchPage;
import org.nuclearfog.twidda.activity.TweetDetail;
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 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_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
@ -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 USER_PATH = "[\\w]+/?(\\bwith_replies\\b|\\bmedia\\b|\\blikes\\b)?";
@Nullable
private EngineException err;
private WeakReference<MainActivity> callback;
private TwitterEngine mTwitter;
public LinkContentLoader(MainActivity callback) {
this.callback = new WeakReference<>(callback);
mTwitter = TwitterEngine.getInstance(callback);
}
@Override
@ -65,12 +56,10 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
data.putString(KEY_TWEET_NAME, name);
return new DataHolder(data, TweetDetail.class);
} else if (path.matches(USER_PATH) && link.getQuery() == null) {
String name = '@' + path;
int end = name.indexOf("/");
int end = path.indexOf("/");
if (end > 0)
name = name.substring(0, end);
TwitterUser user = mTwitter.getUser(name);
data.putLong(KEY_PROFILE_ID, user.getId());
path = path.substring(0, end);
data.putString(KEY_PROFILE_NAME, path);
return new DataHolder(data, UserProfile.class);
} else if (path.startsWith("search")) {
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) {
e.printStackTrace();
}
@ -104,8 +91,6 @@ public class LinkContentLoader extends AsyncTask<Uri, Void, LinkContentLoader.Da
Intent intent = new Intent(callback.get(), result.activity);
intent.putExtras(result.data);
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:
if (userId > 0) {
user = db.getUser(userId);
if (user != null)
if (user != null) {
publishProgress(user);
}
user = mTwitter.getUser(userId);
} else {
user = mTwitter.getUser(username);
@ -68,20 +69,27 @@ public class ProfileLoader extends AsyncTask<Object, TwitterUser, UserProperties
publishProgress(user);
db.storeUser(user);
if (userId > 0) {
connection = mTwitter.getConnection(userId);
if (!connection.isHome())
if (connection.isBlocked() || connection.isMuted())
} else {
connection = mTwitter.getConnection(username);
}
if (!connection.isHome()) {
if (connection.isBlocked() || connection.isMuted()) {
db.muteUser(userId, true);
else
} else {
db.muteUser(userId, false);
}
}
return connection;
case ACTION_FOLLOW:
connection = mTwitter.getConnection(userId);
if (!connection.isFriend())
if (!connection.isFriend()) {
user = mTwitter.followUser(userId);
else
} else {
user = mTwitter.unfollowUser(userId);
}
publishProgress(user);
return mTwitter.getConnection(userId);

View File

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

View File

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

View File

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

View File

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

View File

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