1
0
mirror of https://github.com/nuclearfog/Shitter.git synced 2025-01-28 09:59:32 +01:00

asyncTask optimization

This commit is contained in:
NudeDude 2018-08-17 22:06:02 +02:00
parent 9b731fa84b
commit 853be30fe3
7 changed files with 57 additions and 71 deletions

@ -37,10 +37,9 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
private TimelineRecycler timelineAdapter, mentionAdapter;
private TrendRecycler trendsAdapter;
private DatabaseAdapter tweetDb;
private TrendDatabase trendDb;
private ErrorLog errorLog;
private int woeId;
private int highlight, font;
private boolean image;
private String errMsg = "E: Main Page, ";
private int returnCode = 0;
@ -52,12 +51,13 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
ui = new WeakReference<>((MainActivity)context);
mTwitter = TwitterEngine.getInstance(context);
GlobalSettings settings = GlobalSettings.getInstance(context);
tweetDb = new DatabaseAdapter(ui.get());
tweetDb = new DatabaseAdapter(context);
trendDb = new TrendDatabase(context);
errorLog = new ErrorLog(context);
woeId = settings.getWoeId();
highlight = settings.getHighlightColor();
font = settings.getFontColor();
image = settings.loadImages();
int highlight = settings.getHighlightColor();
int font = settings.getFontColor();
boolean image = settings.loadImages();
RecyclerView timelineList = ui.get().findViewById(R.id.tl_list);
RecyclerView trendList = ui.get().findViewById(R.id.tr_list);
@ -69,14 +69,19 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
if(timelineAdapter == null) {
timelineAdapter = new TimelineRecycler(ui.get());
timelineList.setAdapter(timelineAdapter);
timelineAdapter.setColor(highlight, font);
timelineAdapter.toggleImage(image);
}
if(trendsAdapter == null) {
trendsAdapter = new TrendRecycler(ui.get());
trendList.setAdapter(trendsAdapter);
trendsAdapter.setColor(font);
}
if(mentionAdapter == null) {
mentionAdapter = new TimelineRecycler(ui.get());
mentionList.setAdapter(mentionAdapter);
mentionAdapter.setColor(highlight, font);
mentionAdapter.toggleImage(image);
}
}
@ -91,31 +96,24 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
long id = 1L;
List<Tweet> tweets;
try {
TrendDatabase trendDb = new TrendDatabase(ui.get());
switch (MODE) {
case HOME:
if(timelineAdapter.getItemCount() > 0) {
id = timelineAdapter.getItemId(0);
tweets = mTwitter.getHome(page,id);
tweetDb.storeHomeTimeline(tweets);
tweets.addAll(timelineAdapter.getData());
timelineAdapter.addNew(tweets);
} else {
tweets = mTwitter.getHome(page,id);
tweetDb.storeHomeTimeline(tweets);
timelineAdapter.setData(tweets);
}
timelineAdapter.setData(tweets);
timelineAdapter.setColor(highlight, font);
timelineAdapter.toggleImage(image);
tweetDb.storeHomeTimeline(tweets);
break;
case H_LOAD:
DatabaseAdapter tweetDeck = new DatabaseAdapter(ui.get());
tweets = tweetDeck.getHomeTimeline();
tweets = tweetDb.getHomeTimeline();
timelineAdapter.setData(tweets);
timelineAdapter.setColor(highlight, font);
timelineAdapter.toggleImage(image);
break;
case TRND:
@ -123,13 +121,11 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
List<Trend> trends = mTwitter.getTrends(woeId);
trendDb.store(trends, woeId);
trendsAdapter.setData(trends);
trendsAdapter.setColor(font);
break;
case T_LOAD:
trendsAdapter.setData(trendDb.load(woeId));
trendsAdapter.setColor(font);
break;
case MENT:
@ -138,24 +134,18 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
if(mentionAdapter.getItemCount() != 0) {
id = mentionAdapter.getItemId(0);
mention = mTwitter.getMention(page,id);
tweetDb.storeMentions(mention);
mention.addAll(mentionAdapter.getData());
mentionAdapter.addNew(mention);
} else {
mention = mTwitter.getMention(page,id);
tweetDb.storeMentions(mention);
mentionAdapter.setData(mention);
}
mentionAdapter.setData(mention);
mentionAdapter.setColor(highlight, font);
mentionAdapter.toggleImage(image);
tweetDb.storeMentions(mention);
break;
case M_LOAD:
DatabaseAdapter mentDeck = new DatabaseAdapter(ui.get());
mention = mentDeck.getMentions();
mention = tweetDb.getMentions();
mentionAdapter.setData(mention);
mentionAdapter.setColor(highlight, font);
mentionAdapter.toggleImage(image);
break;
}
} catch(TwitterException e) {

@ -75,9 +75,9 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
ui = new WeakReference<>((UserProfile)context);
mTwitter = TwitterEngine.getInstance(context);
GlobalSettings settings = GlobalSettings.getInstance(context);
database = new DatabaseAdapter(ui.get());
sdf = settings.getDateFormatter();
database = new DatabaseAdapter(context);
errorLog = new ErrorLog(context);
sdf = settings.getDateFormatter();
int font = settings.getFontColor();
int highlight = settings.getHighlightColor();
imgEnabled = settings.loadImages();
@ -141,50 +141,47 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
dateString = sdf.format(time);
description = description.replace('\n', ' ');
if (MODE == GET_TWEETS && !isLocked)
if (MODE == GET_TWEETS && (!isLocked || isFollowed))
{
List<Tweet> tweets;
if(homeTl.getItemCount() > 0) {
id = homeTl.getItemId(0);
tweets = mTwitter.getUserTweets(userId,args[2],id);
homeTl.addNew(tweets);
database.storeUserTweets(tweets);
tweets.addAll(homeTl.getData());
} else {
tweets = database.getUserTweets(userId);
if(tweets.size() == 0 && !isLocked) {
if (tweets.size() == 0) {
tweets = mTwitter.getUserTweets(userId,args[2],id);
homeTl.setData(tweets);
database.storeUserTweets(tweets);
}
} else
homeTl.setData(tweets);
}
homeTl.setData(tweets);
} else if (MODE == GET_FAVORS && !isLocked)
} else if (MODE == GET_FAVORS && (!isLocked || isFollowed))
{
List<Tweet> favorits;
if(homeFav.getItemCount() > 0) {
id = homeFav.getItemId(0);
favorits = mTwitter.getUserFavs(userId,args[2],id);
homeFav.addNew(favorits);
database.storeUserFavs(favorits,userId);
favorits.addAll(homeFav.getData());
} else {
favorits = database.getUserFavs(userId);
if(favorits.size() == 0 && !isLocked) {
if (favorits.size() == 0) {
favorits = mTwitter.getUserFavs(userId,args[2],id);
homeFav.setData(favorits);
database.storeUserFavs(favorits,userId);
}
} else
homeFav.setData(favorits);
}
homeFav.setData(favorits);
}
else if(MODE == ACTION_FOLLOW)
{
} else if (MODE == ACTION_FOLLOW) {
isFollowing = !isFollowing;
mTwitter.followAction(userId, isFollowing);
} else if (MODE == ACTION_BLOCK) {
isBlocked = !isBlocked;
mTwitter.blockAction(userId, isBlocked);
}
else if(MODE == ACTION_MUTE)
{
} else if (MODE == ACTION_MUTE) {
isMuted = !isMuted;
mTwitter.muteAction(userId, isMuted);
}

@ -53,10 +53,10 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
private String repliedUsername, apiName, profile_pb;
private String medialinks[];
private String prefix;
private int highlight;
private boolean retweeted, favorited, toggleImg, verified;
private long tweetReplyID, replyUserId;
private int rtCount, favCount;
private int highlight, font;
private String errorMessage = "Status load: ";
private int returnCode = 0;
@ -66,7 +66,7 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
mTwitter = TwitterEngine.getInstance(context);
GlobalSettings settings = GlobalSettings.getInstance(context);
sdf = settings.getDateFormatter();
font = settings.getFontColor();
int font = settings.getFontColor();
highlight = settings.getHighlightColor();
toggleImg = settings.loadImages();
ui = new WeakReference<>((TweetDetail)context);
@ -78,6 +78,8 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
if(answerAdapter == null) {
answerAdapter = new TimelineRecycler(ui.get());
replyList.setAdapter(answerAdapter);
answerAdapter.toggleImage(toggleImg);
answerAdapter.setColor(highlight, font);
}
}
@ -101,7 +103,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
tweet = database.getStatus(tweetID);
List<Tweet> answers = database.getAnswers(tweetID);
answerAdapter.setData(answers);
answerAdapter.setColor(highlight, font);
if(tweet == null)
return IGNORE; // NOT FOUND
} else {
@ -158,13 +159,11 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
if(answerAdapter.getItemCount() > 0) {
long sinceId = answerAdapter.getItemId(0);
answers = mTwitter.getAnswers(scrNameStr, tweetID, sinceId);
answers.addAll(answerAdapter.getData());
answerAdapter.addNew(answers);
} else {
answers = mTwitter.getAnswers(scrNameStr, tweetID, tweetID);
answerAdapter.setData(answers);
}
answerAdapter.setData(answers);
answerAdapter.toggleImage(toggleImg);
answerAdapter.setColor(highlight, font);
if(answers.size() > 0 && database.containStatus(tweetID))
database.storeReplies(answers);
}

@ -26,8 +26,6 @@ public class TwitterSearch extends AsyncTask<String, Void, Boolean> {
private TwitterEngine mTwitter;
private ErrorLog errorLog;
private WeakReference<SearchPage> ui;
private int highlight, font_color;
private boolean imageLoad;
private String errorMessage = "E: Twitter search, ";
private int returnCode = 0;
@ -37,9 +35,9 @@ public class TwitterSearch extends AsyncTask<String, Void, Boolean> {
errorLog = new ErrorLog(context);
GlobalSettings settings = GlobalSettings.getInstance(context);
font_color = settings.getFontColor();
highlight = settings.getHighlightColor();
imageLoad = settings.loadImages();
int font_color = settings.getFontColor();
int highlight = settings.getHighlightColor();
boolean imageLoad = settings.loadImages();
RecyclerView tweetSearch = ui.get().findViewById(R.id.tweet_result);
RecyclerView userSearch = ui.get().findViewById(R.id.user_result);
@ -49,10 +47,13 @@ public class TwitterSearch extends AsyncTask<String, Void, Boolean> {
if(searchAdapter == null) {
searchAdapter = new TimelineRecycler(ui.get());
tweetSearch.setAdapter(searchAdapter);
searchAdapter.setColor(highlight, font_color);
searchAdapter.toggleImage(imageLoad);
}
if(userAdapter == null) {
userAdapter = new UserRecycler(ui.get());
userSearch.setAdapter(userAdapter);
userAdapter.toggleImage(imageLoad);
}
}
@ -65,8 +66,7 @@ public class TwitterSearch extends AsyncTask<String, Void, Boolean> {
if(searchAdapter.getItemCount() > 0) {
id = searchAdapter.getItemId(0);
List<Tweet> tweets = mTwitter.searchTweets(strSearch,id);
tweets.addAll(searchAdapter.getData());
searchAdapter.setData(tweets);
searchAdapter.addNew(tweets);
} else {
List<Tweet> tweets = mTwitter.searchTweets(strSearch,id);
searchAdapter.setData(tweets);
@ -75,10 +75,6 @@ public class TwitterSearch extends AsyncTask<String, Void, Boolean> {
List<TwitterUser> user = mTwitter.searchUsers(strSearch);
userAdapter.setData(user);
}
searchAdapter.setColor(highlight,font_color);
searchAdapter.toggleImage(imageLoad);
userAdapter.toggleImage(imageLoad);
return true;
} catch (TwitterException err) {

@ -35,7 +35,6 @@ public class UserLists extends AsyncTask <Long, Void, Boolean> {
private UserRecycler usrAdp;
private ErrorLog errorLog;
private Dialog popup;
private boolean imageLoad;
private String errorMessage = "E: Userlist, ";
private int returnCode = 0;
@ -44,7 +43,7 @@ public class UserLists extends AsyncTask <Long, Void, Boolean> {
*/
public UserLists(Context context) {
GlobalSettings settings = GlobalSettings.getInstance(context);
imageLoad = settings.loadImages();
boolean imageLoad = settings.loadImages();
errorLog = new ErrorLog(context);
inflater = LayoutInflater.from(context);
popup = new Dialog(context);
@ -54,6 +53,7 @@ public class UserLists extends AsyncTask <Long, Void, Boolean> {
RecyclerView userList = ui.get().findViewById(R.id.userlist);
usrAdp = new UserRecycler(ui.get());
usrAdp.toggleImage(imageLoad);
userList.setAdapter(usrAdp);
}
@ -106,7 +106,6 @@ public class UserLists extends AsyncTask <Long, Void, Boolean> {
user = mTwitter.getRetweeter(id,cursor);
usrAdp.setData(user);
}
usrAdp.toggleImage(imageLoad);
return true;
}
catch(TwitterException err) {

@ -61,6 +61,12 @@ public class TimelineRecycler extends Adapter<TimelineRecycler.ItemHolder> imple
}
public void addNew(List<Tweet> newTweets) {
newTweets.addAll(tweets);
tweets = newTweets;
}
@Override
public long getItemId(int pos) {
return tweets.get(pos).tweetID;

@ -220,10 +220,9 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
txtTw.setTextColor(fontColor);
new StatusLoader(this).execute(tweetID, StatusLoader.LOAD_DB);
mStat = new StatusLoader(this);
answerReload.setRefreshing(true);
mReply = new StatusLoader(this);
mStat.execute(tweetID, StatusLoader.LOAD_TWEET);
answerReload.setRefreshing(true);
mReply.execute(tweetID, StatusLoader.LOAD_REPLY);
}
}