mirror of
https://github.com/nuclearfog/Shitter.git
synced 2025-01-31 19:34:55 +01:00
added search tablayout
This commit is contained in:
parent
181f991812
commit
2bc50df89d
@ -76,12 +76,6 @@ public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSe
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int reqCode, int returnCode, Intent i) {
|
||||
switch (reqCode) {
|
||||
|
@ -22,7 +22,9 @@ public class HomeTabAdapter extends FragmentPagerAdapter {
|
||||
HOME_TL = new Bundle();
|
||||
MENT_TL = new Bundle();
|
||||
HOME_TL.putInt("mode",HOME);
|
||||
HOME_TL.putBoolean("fix", true);
|
||||
MENT_TL.putInt("mode", MENT);
|
||||
MENT_TL.putBoolean("fix", true);
|
||||
}
|
||||
|
||||
public HomeTabAdapter(FragmentManager fm) {
|
||||
|
@ -1,108 +0,0 @@
|
||||
package org.nuclearfog.twidda.backend;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
|
||||
import org.nuclearfog.twidda.R;
|
||||
import org.nuclearfog.twidda.adapter.TweetAdapter;
|
||||
import org.nuclearfog.twidda.adapter.UserAdapter;
|
||||
import org.nuclearfog.twidda.backend.items.Tweet;
|
||||
import org.nuclearfog.twidda.backend.items.TwitterUser;
|
||||
import org.nuclearfog.twidda.window.SearchPage;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
public class TwitterSearch extends AsyncTask<String, Void, Void> {
|
||||
|
||||
private TweetAdapter searchAdapter;
|
||||
private UserAdapter userAdapter;
|
||||
private List<Tweet> tweets;
|
||||
private List<TwitterUser> users;
|
||||
private TwitterEngine mTwitter;
|
||||
private TwitterException err;
|
||||
private WeakReference<SearchPage> ui;
|
||||
|
||||
public TwitterSearch(@NonNull SearchPage context) {
|
||||
ui = new WeakReference<>(context);
|
||||
mTwitter = TwitterEngine.getInstance(context);
|
||||
|
||||
tweets = new ArrayList<>();
|
||||
users = new ArrayList<>();
|
||||
|
||||
RecyclerView tweetSearch = context.findViewById(R.id.tweet_result);
|
||||
RecyclerView userSearch = context.findViewById(R.id.user_result);
|
||||
searchAdapter = (TweetAdapter) tweetSearch.getAdapter();
|
||||
userAdapter = (UserAdapter) userSearch.getAdapter();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
if (ui.get() == null) return;
|
||||
|
||||
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
|
||||
tweetReload.setRefreshing(true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(String... search) {
|
||||
String strSearch = search[0];
|
||||
long id = 1L;
|
||||
try {
|
||||
if (searchAdapter.getItemCount() > 0)
|
||||
id = searchAdapter.getItemId(0);
|
||||
tweets = mTwitter.searchTweets(strSearch, id);
|
||||
|
||||
if (userAdapter.getItemCount() == 0) {
|
||||
users = mTwitter.searchUsers(strSearch);
|
||||
}
|
||||
} catch (TwitterException err) {
|
||||
this.err = err;
|
||||
} catch (Exception err) {
|
||||
if (err.getMessage() != null)
|
||||
Log.e("Twitter Search", err.getMessage());
|
||||
else
|
||||
Log.e("Twitter Search", err.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
if (ui.get() == null) return;
|
||||
|
||||
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
|
||||
tweetReload.setRefreshing(false);
|
||||
|
||||
if (!tweets.isEmpty()) {
|
||||
tweets.addAll(searchAdapter.getData());
|
||||
searchAdapter.setData(tweets);
|
||||
searchAdapter.notifyDataSetChanged();
|
||||
}
|
||||
if (!users.isEmpty()) {
|
||||
userAdapter.setData(users);
|
||||
userAdapter.notifyDataSetChanged();
|
||||
}
|
||||
if (err != null) {
|
||||
ErrorHandler.printError(ui.get(), err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
if (ui.get() == null) return;
|
||||
|
||||
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
|
||||
tweetReload.setRefreshing(false);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package org.nuclearfog.twidda.fragment;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.AsyncTask.Status;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
@ -12,27 +13,16 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.nuclearfog.twidda.BuildConfig;
|
||||
import org.nuclearfog.twidda.R;
|
||||
import org.nuclearfog.twidda.adapter.OnItemClickListener;
|
||||
import org.nuclearfog.twidda.adapter.TweetAdapter;
|
||||
import org.nuclearfog.twidda.backend.items.Tweet;
|
||||
import org.nuclearfog.twidda.database.GlobalSettings;
|
||||
import org.nuclearfog.twidda.fragment.backend.TweetLoader;
|
||||
import org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode;
|
||||
import org.nuclearfog.twidda.window.TweetDetail;
|
||||
|
||||
import static android.os.AsyncTask.Status.RUNNING;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.DB_ANS;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.DB_FAVORS;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.DB_HOME;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.DB_MENT;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.DB_TWEETS;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.TL_HOME;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.TL_MENT;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.TWEET_ANS;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.TWEET_SEARCH;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.USR_FAVORS;
|
||||
import static org.nuclearfog.twidda.fragment.backend.TweetLoader.Mode.USR_TWEETS;
|
||||
|
||||
|
||||
public class TweetListFragment extends Fragment implements OnRefreshListener, OnItemClickListener {
|
||||
|
||||
@ -52,15 +42,17 @@ public class TweetListFragment extends Fragment implements OnRefreshListener, On
|
||||
private String search;
|
||||
private long id;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle param) {
|
||||
super.onCreateView(inflater, parent, param);
|
||||
|
||||
boolean fixSize;
|
||||
Bundle b = getArguments();
|
||||
if(b != null && b.containsKey("mode")) {
|
||||
mode = b.getInt("mode");
|
||||
id = b.getLong("id", -1);
|
||||
search = b.getString("search", "");
|
||||
fixSize = b.getBoolean("fix", false);
|
||||
} else {
|
||||
throw new AssertionError("Bundle error!");
|
||||
}
|
||||
@ -78,29 +70,15 @@ public class TweetListFragment extends Fragment implements OnRefreshListener, On
|
||||
|
||||
RecyclerView list = v.findViewById(R.id.fragment_list);
|
||||
list.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
|
||||
switch(mode) {
|
||||
default:
|
||||
case USER_TWEET:
|
||||
case USER_FAVOR:
|
||||
case TWEET_ANSR:
|
||||
list.setHasFixedSize(false);
|
||||
break;
|
||||
|
||||
case HOME:
|
||||
case MENT:
|
||||
case SEARCH:
|
||||
list.setHasFixedSize(true);
|
||||
break;
|
||||
}
|
||||
list.setHasFixedSize(fixSize);
|
||||
list.setAdapter(adapter);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View v, Bundle param) {
|
||||
super.onViewCreated(v, param);
|
||||
root = v;
|
||||
}
|
||||
|
||||
@ -111,37 +89,33 @@ public class TweetListFragment extends Fragment implements OnRefreshListener, On
|
||||
if(tweetTask == null) {
|
||||
switch(mode) {
|
||||
case HOME:
|
||||
tweetTask = new TweetLoader(root, DB_HOME);
|
||||
tweetTask = new TweetLoader(root, Mode.DB_HOME);
|
||||
tweetTask.execute();
|
||||
break;
|
||||
|
||||
case MENT:
|
||||
tweetTask = new TweetLoader(root, DB_MENT);
|
||||
tweetTask = new TweetLoader(root, Mode.DB_MENT);
|
||||
tweetTask.execute();
|
||||
break;
|
||||
|
||||
case USER_TWEET:
|
||||
tweetTask = new TweetLoader(root, DB_TWEETS);
|
||||
tweetTask = new TweetLoader(root, Mode.DB_TWEETS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case USER_FAVOR:
|
||||
tweetTask = new TweetLoader(root, DB_FAVORS);
|
||||
tweetTask = new TweetLoader(root, Mode.DB_FAVORS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case TWEET_ANSR:
|
||||
tweetTask = new TweetLoader(root, DB_ANS);
|
||||
tweetTask = new TweetLoader(root, Mode.DB_ANS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case SEARCH:
|
||||
tweetTask = new TweetLoader(root, TWEET_SEARCH);
|
||||
tweetTask.execute(id);
|
||||
tweetTask = new TweetLoader(root, Mode.TWEET_SEARCH);
|
||||
tweetTask.execute(search);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException();
|
||||
if(BuildConfig.DEBUG)
|
||||
throw new AssertionError("mode failure");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,7 +123,7 @@ public class TweetListFragment extends Fragment implements OnRefreshListener, On
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
if(tweetTask != null && tweetTask.getStatus() == RUNNING)
|
||||
if(tweetTask != null && tweetTask.getStatus() == Status.RUNNING)
|
||||
tweetTask.cancel(true);
|
||||
super.onStop();
|
||||
}
|
||||
@ -159,37 +133,33 @@ public class TweetListFragment extends Fragment implements OnRefreshListener, On
|
||||
public void onRefresh() {
|
||||
switch(mode) {
|
||||
case HOME:
|
||||
tweetTask = new TweetLoader(root, TL_HOME);
|
||||
tweetTask = new TweetLoader(root, Mode.TL_HOME);
|
||||
tweetTask.execute();
|
||||
break;
|
||||
|
||||
case MENT:
|
||||
tweetTask = new TweetLoader(root, TL_MENT);
|
||||
tweetTask = new TweetLoader(root, Mode.TL_MENT);
|
||||
tweetTask.execute();
|
||||
break;
|
||||
|
||||
case USER_TWEET:
|
||||
tweetTask = new TweetLoader(root, USR_TWEETS);
|
||||
tweetTask = new TweetLoader(root, Mode.USR_TWEETS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case USER_FAVOR:
|
||||
tweetTask = new TweetLoader(root, USR_FAVORS);
|
||||
tweetTask = new TweetLoader(root, Mode.USR_FAVORS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case TWEET_ANSR:
|
||||
tweetTask = new TweetLoader(root, TWEET_ANS);
|
||||
tweetTask = new TweetLoader(root, Mode.TWEET_ANS);
|
||||
tweetTask.execute(id);
|
||||
break;
|
||||
|
||||
case SEARCH:
|
||||
tweetTask = new TweetLoader(root, TWEET_SEARCH);
|
||||
tweetTask = new TweetLoader(root, Mode.TWEET_SEARCH);
|
||||
tweetTask.execute(search);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException();
|
||||
if(BuildConfig.DEBUG)
|
||||
throw new AssertionError("mode failure");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.nuclearfog.twidda.fragment;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask.Status;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
@ -11,42 +13,131 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.nuclearfog.twidda.BuildConfig;
|
||||
import org.nuclearfog.twidda.R;
|
||||
import org.nuclearfog.twidda.adapter.OnItemClickListener;
|
||||
import org.nuclearfog.twidda.adapter.UserAdapter;
|
||||
import org.nuclearfog.twidda.backend.items.TwitterUser;
|
||||
import org.nuclearfog.twidda.database.GlobalSettings;
|
||||
import org.nuclearfog.twidda.fragment.backend.UserLoader;
|
||||
import org.nuclearfog.twidda.fragment.backend.UserLoader.Mode;
|
||||
import org.nuclearfog.twidda.window.UserProfile;
|
||||
|
||||
|
||||
public class UserListFragment extends Fragment implements OnRefreshListener, OnItemClickListener {
|
||||
|
||||
public static final int FOLLOWS = 0;
|
||||
public static final int FRIENDS = 1;
|
||||
public static final int RETWEET = 2;
|
||||
public static final int FAVORIT = 3;
|
||||
public static final int USEARCH = 4;
|
||||
|
||||
private SwipeRefreshLayout reload;
|
||||
private UserAdapter adapter;
|
||||
private UserLoader userTask;
|
||||
private View root;
|
||||
private String search;
|
||||
private long id;
|
||||
private int mode;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle param) {
|
||||
SwipeRefreshLayout reload = new SwipeRefreshLayout(inflater.getContext());
|
||||
RecyclerView list = new RecyclerView(inflater.getContext());
|
||||
list.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
Bundle b = getArguments();
|
||||
if(b != null && b.containsKey("mode")) {
|
||||
mode = b.getInt("mode");
|
||||
id = b.getLong("id", -1);
|
||||
search = b.getString("search", "");
|
||||
} else if(BuildConfig.DEBUG) {
|
||||
throw new AssertionError("Bundle error!");
|
||||
}
|
||||
GlobalSettings settings = GlobalSettings.getInstance(getContext());
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_list, parent, false);
|
||||
RecyclerView list = v.findViewById(R.id.fragment_list);
|
||||
reload = v.findViewById(R.id.fragment_reload);
|
||||
|
||||
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
|
||||
reload.setOnRefreshListener(this);
|
||||
reload.addView(list);
|
||||
return reload;
|
||||
adapter = new UserAdapter(this);
|
||||
adapter.setColor(settings.getFontColor());
|
||||
adapter.toggleImage(settings.getImageLoad());
|
||||
list.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
list.setHasFixedSize(true);
|
||||
list.setAdapter(adapter);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View v, Bundle param) {
|
||||
root = v;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
if(userTask == null) {
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
if(userTask != null && userTask.getStatus() == Status.RUNNING)
|
||||
userTask.cancel(true);
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onItemClick(RecyclerView rv, int pos) {
|
||||
if(!reload.isRefreshing()) {
|
||||
TwitterUser user = adapter.getData(pos);
|
||||
long userID = user.getId();
|
||||
String username = user.getScreenname();
|
||||
Intent intent = new Intent(getContext(), UserProfile.class);
|
||||
intent.putExtra("userID", userID);
|
||||
intent.putExtra("username", username);
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void load() {
|
||||
switch (mode) {
|
||||
case FOLLOWS:
|
||||
userTask = new UserLoader(root, Mode.FOLLOWS);
|
||||
userTask.execute(id);
|
||||
break;
|
||||
case FRIENDS:
|
||||
userTask = new UserLoader(root, Mode.FRIENDS);
|
||||
userTask.execute(id);
|
||||
break;
|
||||
case RETWEET:
|
||||
userTask = new UserLoader(root, Mode.RETWEET);
|
||||
userTask.execute(id);
|
||||
break;
|
||||
case FAVORIT:
|
||||
userTask = new UserLoader(root, Mode.FAVORIT);
|
||||
userTask.execute(id);
|
||||
break;
|
||||
case USEARCH:
|
||||
userTask = new UserLoader(root, Mode.SEARCH);
|
||||
userTask.execute(search);
|
||||
break;
|
||||
default:
|
||||
if(BuildConfig.DEBUG)
|
||||
throw new AssertionError("mode failure");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,9 +4,9 @@ import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import org.nuclearfog.twidda.R;
|
||||
import org.nuclearfog.twidda.adapter.UserAdapter;
|
||||
import org.nuclearfog.twidda.backend.ErrorHandler;
|
||||
import org.nuclearfog.twidda.backend.TwitterEngine;
|
||||
@ -17,27 +17,29 @@ import java.util.List;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
public class UserLoader extends AsyncTask<Long, Void, Boolean> {
|
||||
public class UserLoader extends AsyncTask<Object, Void, Boolean> {
|
||||
|
||||
public enum Mode {
|
||||
FOLLOWS,
|
||||
FRIENDS,
|
||||
RETWEET,
|
||||
FAVORIT
|
||||
FAVORIT,
|
||||
SEARCH
|
||||
}
|
||||
private Mode mode;
|
||||
private WeakReference<ViewGroup> ui;
|
||||
private WeakReference<View> ui;
|
||||
private TwitterEngine mTwitter;
|
||||
private TwitterException err;
|
||||
private UserAdapter adapter;
|
||||
private List<TwitterUser> users;
|
||||
|
||||
|
||||
public UserLoader(@NonNull ViewGroup root) {
|
||||
public UserLoader(@NonNull View root, Mode mode) {
|
||||
ui = new WeakReference<>(root);
|
||||
mTwitter = TwitterEngine.getInstance(root.getContext());
|
||||
RecyclerView list = (RecyclerView)root.getChildAt(0);
|
||||
RecyclerView list = root.findViewById(R.id.fragment_list);
|
||||
adapter = (UserAdapter) list.getAdapter();
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
|
||||
@ -46,29 +48,33 @@ public class UserLoader extends AsyncTask<Long, Void, Boolean> {
|
||||
if(ui.get() == null)
|
||||
return;
|
||||
|
||||
SwipeRefreshLayout reload = (SwipeRefreshLayout)ui.get();
|
||||
SwipeRefreshLayout reload = ui.get().findViewById(R.id.fragment_reload);
|
||||
reload.setRefreshing(true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Long[] param) {
|
||||
protected Boolean doInBackground(Object[] param) {
|
||||
try {
|
||||
switch(mode) {
|
||||
case FOLLOWS:
|
||||
users = mTwitter.getFollower(param[0], -1);
|
||||
users = mTwitter.getFollower((long)param[0], -1);
|
||||
break;
|
||||
|
||||
case FRIENDS:
|
||||
users = mTwitter.getFollowing(param[0], -1);
|
||||
users = mTwitter.getFollowing((long)param[0], -1);
|
||||
break;
|
||||
|
||||
case RETWEET:
|
||||
users = mTwitter.getRetweeter(param[0], -1);
|
||||
users = mTwitter.getRetweeter((long)param[0], -1);
|
||||
break;
|
||||
|
||||
case FAVORIT:
|
||||
break;
|
||||
|
||||
case SEARCH:
|
||||
users = mTwitter.searchUsers((String)param[0]);
|
||||
break;
|
||||
}
|
||||
} catch(TwitterException err) {
|
||||
this.err = err;
|
||||
@ -92,7 +98,7 @@ public class UserLoader extends AsyncTask<Long, Void, Boolean> {
|
||||
if(err != null)
|
||||
ErrorHandler.printError(ui.get().getContext(), err);
|
||||
}
|
||||
SwipeRefreshLayout reload = (SwipeRefreshLayout) ui.get();
|
||||
SwipeRefreshLayout reload = ui.get().findViewById(R.id.fragment_reload);
|
||||
reload.setRefreshing(false);
|
||||
}
|
||||
|
||||
@ -102,7 +108,7 @@ public class UserLoader extends AsyncTask<Long, Void, Boolean> {
|
||||
if(ui.get() == null)
|
||||
return;
|
||||
|
||||
SwipeRefreshLayout reload = (SwipeRefreshLayout) ui.get();
|
||||
SwipeRefreshLayout reload = ui.get().findViewById(R.id.fragment_reload);
|
||||
reload.setRefreshing(false);
|
||||
}
|
||||
}
|
@ -2,50 +2,28 @@ package org.nuclearfog.twidda.window;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.SearchView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.TranslateAnimation;
|
||||
import android.widget.TabHost;
|
||||
import android.widget.TabHost.OnTabChangeListener;
|
||||
|
||||
import org.nuclearfog.twidda.BuildConfig;
|
||||
import org.nuclearfog.twidda.R;
|
||||
import org.nuclearfog.twidda.adapter.OnItemClickListener;
|
||||
import org.nuclearfog.twidda.adapter.TweetAdapter;
|
||||
import org.nuclearfog.twidda.adapter.UserAdapter;
|
||||
import org.nuclearfog.twidda.backend.TwitterSearch;
|
||||
import org.nuclearfog.twidda.backend.items.Tweet;
|
||||
import org.nuclearfog.twidda.backend.items.TwitterUser;
|
||||
import org.nuclearfog.twidda.adapter.SearchTabAdapter;
|
||||
import org.nuclearfog.twidda.database.GlobalSettings;
|
||||
|
||||
import static android.os.AsyncTask.Status.RUNNING;
|
||||
|
||||
/**
|
||||
* Search Page
|
||||
*
|
||||
* @see TwitterSearch
|
||||
*/
|
||||
public class SearchPage extends AppCompatActivity implements OnRefreshListener,
|
||||
OnTabChangeListener, OnItemClickListener {
|
||||
public class SearchPage extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
|
||||
|
||||
private RecyclerView tweetSearch, userSearch;
|
||||
private TweetAdapter searchAdapter;
|
||||
private UserAdapter userAdapter;
|
||||
private SwipeRefreshLayout tweetReload;
|
||||
private GlobalSettings settings;
|
||||
private TwitterSearch searchAsync;
|
||||
private View lastView, twUnderline, usUnderline;
|
||||
private TabHost tabhost;
|
||||
private static final int[] icons = {R.drawable.search, R.drawable.user};
|
||||
|
||||
private ViewPager pager;
|
||||
private String search = "";
|
||||
private int tabIndex = 0;
|
||||
|
||||
@ -55,6 +33,10 @@ public class SearchPage extends AppCompatActivity implements OnRefreshListener,
|
||||
setContentView(R.layout.page_search);
|
||||
|
||||
Toolbar tool = findViewById(R.id.search_toolbar);
|
||||
View root = findViewById(R.id.search_layout);
|
||||
TabLayout tab = findViewById(R.id.search_tab);
|
||||
pager = findViewById(R.id.search_pager);
|
||||
|
||||
setSupportActionBar(tool);
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setDisplayShowTitleEnabled(false);
|
||||
@ -66,76 +48,27 @@ public class SearchPage extends AppCompatActivity implements OnRefreshListener,
|
||||
search = param.getString("search");
|
||||
}
|
||||
|
||||
View root = findViewById(R.id.search_layout);
|
||||
tweetSearch = findViewById(R.id.tweet_result);
|
||||
userSearch = findViewById(R.id.user_result);
|
||||
tweetReload = findViewById(R.id.searchtweets);
|
||||
tabhost = findViewById(R.id.search_tab);
|
||||
|
||||
settings = GlobalSettings.getInstance(this);
|
||||
GlobalSettings settings = GlobalSettings.getInstance(this);
|
||||
root.setBackgroundColor(settings.getBackgroundColor());
|
||||
SearchTabAdapter adapter = new SearchTabAdapter(getSupportFragmentManager(), search);
|
||||
pager.setAdapter(adapter);
|
||||
tab.setupWithViewPager(pager);
|
||||
tab.addOnTabSelectedListener(this);
|
||||
|
||||
LayoutInflater inflater = LayoutInflater.from(this);
|
||||
View twIndicator = inflater.inflate(R.layout.tab_ts, null);
|
||||
View usIndicator = inflater.inflate(R.layout.tab_us, null);
|
||||
twUnderline = twIndicator.findViewById(R.id.ts_divider);
|
||||
usUnderline = usIndicator.findViewById(R.id.us_divider);
|
||||
tweetReload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
|
||||
|
||||
tabhost.setup();
|
||||
TabHost.TabSpec tab1 = tabhost.newTabSpec("search_result");
|
||||
tab1.setContent(R.id.searchtweets);
|
||||
tab1.setIndicator(twIndicator);
|
||||
tabhost.addTab(tab1);
|
||||
TabHost.TabSpec tab2 = tabhost.newTabSpec("user_result");
|
||||
tab2.setContent(R.id.user_result);
|
||||
tab2.setIndicator(usIndicator);
|
||||
tabhost.addTab(tab2);
|
||||
lastView = tabhost.getCurrentView();
|
||||
setIndicator();
|
||||
|
||||
tweetSearch.setLayoutManager(new LinearLayoutManager(this));
|
||||
userSearch.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
tabhost.setOnTabChangedListener(this);
|
||||
tweetReload.setOnRefreshListener(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (searchAsync == null) {
|
||||
searchAdapter = new TweetAdapter(this);
|
||||
searchAdapter.setColor(settings.getHighlightColor(), settings.getFontColor());
|
||||
searchAdapter.toggleImage(settings.getImageLoad());
|
||||
tweetSearch.setAdapter(searchAdapter);
|
||||
|
||||
userAdapter = new UserAdapter(this);
|
||||
userAdapter.toggleImage(settings.getImageLoad());
|
||||
userAdapter.setColor(settings.getFontColor());
|
||||
userSearch.setAdapter(userAdapter);
|
||||
|
||||
searchAsync = new TwitterSearch(this);
|
||||
searchAsync.execute(search);
|
||||
for(int i = 0 ; i < icons.length ; i++) {
|
||||
TabLayout.Tab t = tab.getTabAt(i);
|
||||
if(t != null)
|
||||
t.setIcon(icons[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
if (searchAsync != null && searchAsync.getStatus() == RUNNING)
|
||||
searchAsync.cancel(true);
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (tabIndex == 1) {
|
||||
tabhost.setCurrentTab(0);
|
||||
} else {
|
||||
if (tabIndex == 0) {
|
||||
super.onBackPressed();
|
||||
} else {
|
||||
pager.setCurrentItem(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -177,84 +110,16 @@ public class SearchPage extends AppCompatActivity implements OnRefreshListener,
|
||||
|
||||
|
||||
@Override
|
||||
public void onItemClick(RecyclerView rv, int position) {
|
||||
if (!tweetReload.isRefreshing()) {
|
||||
switch (rv.getId()) {
|
||||
case R.id.tweet_result:
|
||||
Tweet tweet = searchAdapter.getData(position);
|
||||
Intent tweetdetail = new Intent(this, TweetDetail.class);
|
||||
tweetdetail.putExtra("tweetID", tweet.getId());
|
||||
tweetdetail.putExtra("username", tweet.getUser().getScreenname());
|
||||
startActivity(tweetdetail);
|
||||
break;
|
||||
|
||||
case R.id.user_result:
|
||||
TwitterUser user = userAdapter.getData(position);
|
||||
Intent userprofile = new Intent(this, UserProfile.class);
|
||||
userprofile.putExtra("userID", user.getId());
|
||||
userprofile.putExtra("username", user.getScreenname());
|
||||
startActivity(userprofile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void onTabSelected(TabLayout.Tab tab) {
|
||||
tabIndex = tab.getPosition();
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
if (searchAsync != null && searchAsync.getStatus() == RUNNING)
|
||||
searchAsync.cancel(true);
|
||||
searchAsync = new TwitterSearch(this);
|
||||
searchAsync.execute(search);
|
||||
}
|
||||
public void onTabUnselected(TabLayout.Tab tab) { }
|
||||
|
||||
|
||||
@Override
|
||||
public void onTabChanged(String tabId) {
|
||||
animate();
|
||||
tabIndex = tabhost.getCurrentTab();
|
||||
setIndicator();
|
||||
}
|
||||
|
||||
|
||||
private void setIndicator() {
|
||||
switch (tabIndex) {
|
||||
case 0:
|
||||
twUnderline.setBackgroundColor(settings.getHighlightColor());
|
||||
usUnderline.setBackgroundColor(0);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
usUnderline.setBackgroundColor(settings.getHighlightColor());
|
||||
twUnderline.setBackgroundColor(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void animate() {
|
||||
final int ANIM_DUR = 300;
|
||||
final float LEFT = -1.0f;
|
||||
final float RIGHT = 1.0f;
|
||||
final float NULL = 0.0f;
|
||||
final int DIMENS = Animation.RELATIVE_TO_PARENT;
|
||||
|
||||
View currentView = tabhost.getCurrentView();
|
||||
if (tabhost.getCurrentTab() > tabIndex) {
|
||||
Animation leftOut = new TranslateAnimation(DIMENS, NULL, DIMENS, LEFT, DIMENS, NULL, DIMENS, NULL);
|
||||
Animation rightIn = new TranslateAnimation(DIMENS, RIGHT, DIMENS, NULL, DIMENS, NULL, DIMENS, NULL);
|
||||
leftOut.setDuration(ANIM_DUR);
|
||||
rightIn.setDuration(ANIM_DUR);
|
||||
lastView.setAnimation(leftOut);
|
||||
currentView.setAnimation(rightIn);
|
||||
} else {
|
||||
Animation leftIn = new TranslateAnimation(DIMENS, LEFT, DIMENS, NULL, DIMENS, NULL, DIMENS, NULL);
|
||||
Animation rightOut = new TranslateAnimation(DIMENS, NULL, DIMENS, RIGHT, DIMENS, NULL, DIMENS, NULL);
|
||||
leftIn.setDuration(ANIM_DUR);
|
||||
rightOut.setDuration(ANIM_DUR);
|
||||
lastView.setAnimation(rightOut);
|
||||
currentView.setAnimation(leftIn);
|
||||
}
|
||||
lastView = tabhost.getCurrentView();
|
||||
}
|
||||
public void onTabReselected(TabLayout.Tab tab) { }
|
||||
}
|
@ -10,47 +10,14 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/bar_wide" />
|
||||
|
||||
<TabHost
|
||||
<android.support.design.widget.TabLayout
|
||||
android:id="@+id/search_tab"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TabWidget
|
||||
android:id="@android:id/tabs"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@android:id/tabcontent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v4.widget.SwipeRefreshLayout
|
||||
android:id="@+id/searchtweets"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/tweet_result"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</android.support.v4.widget.SwipeRefreshLayout>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/user_result"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</TabHost>
|
||||
<android.support.v4.view.ViewPager
|
||||
android:id="@+id/search_pager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user