Optimizations
This commit is contained in:
NudeDude 2018-03-03 18:26:54 +01:00
parent 4bd98e4fa4
commit f4d9ab0a60
10 changed files with 127 additions and 92 deletions

View File

@ -67,12 +67,12 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
switch (MODE) { switch (MODE) {
case HOME: case HOME:
timelineAdapter = (TimelineAdapter) timelineList.getAdapter(); timelineAdapter = (TimelineAdapter) timelineList.getAdapter();
if(timelineAdapter.getCount() == 0) { if(timelineAdapter != null && timelineAdapter.getCount() != 0) {
TweetDatabase mTweets = new TweetDatabase(mTwitter.getHome(page,id), context,TweetDatabase.HOME_TL,0);
timelineAdapter = new TimelineAdapter(context,mTweets);
} else {
id = timelineAdapter.getItemId(0); id = timelineAdapter.getItemId(0);
timelineAdapter.getData().add(mTwitter.getHome(page,id)); timelineAdapter.getData().add(mTwitter.getHome(page,id));
} else {
TweetDatabase mTweets = new TweetDatabase(mTwitter.getHome(page,id), context,TweetDatabase.HOME_TL,0);
timelineAdapter = new TimelineAdapter(context,mTweets);
} }
break; break;
@ -82,12 +82,12 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
case MENT: case MENT:
mentionAdapter = (TimelineAdapter) mentionList.getAdapter(); mentionAdapter = (TimelineAdapter) mentionList.getAdapter();
if(mentionAdapter.getCount() == 0) { if(mentionAdapter != null && mentionAdapter.getCount() != 0) {
TweetDatabase mention = new TweetDatabase(mTwitter.getMention(page,id), context,TweetDatabase.GET_MENT,0);
mentionAdapter = new TimelineAdapter(context,mention);
} else {
id = mentionAdapter.getItemId(0); id = mentionAdapter.getItemId(0);
mentionAdapter.getData().add(mTwitter.getMention(page,id)); mentionAdapter.getData().add(mTwitter.getMention(page,id));
} else {
TweetDatabase mention = new TweetDatabase(mTwitter.getMention(page,id), context,TweetDatabase.GET_MENT,0);
mentionAdapter = new TimelineAdapter(context,mention);
} }
break; break;
} }

View File

@ -117,12 +117,12 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
else if(MODE == GET_TWEETS) else if(MODE == GET_TWEETS)
{ {
homeTl = (TimelineAdapter) profileTweets.getAdapter(); homeTl = (TimelineAdapter) profileTweets.getAdapter();
if(homeTl != null) { if(homeTl == null || homeTl.getCount() == 0) {
id = homeTl.getItemId(0);
homeTl.getData().add(mTwitter.getUserTweets(userId,args[2],id));
} else {
TweetDatabase hTweets = new TweetDatabase(mTwitter.getUserTweets(userId,args[2],id),context,TweetDatabase.USER_TL,userId); TweetDatabase hTweets = new TweetDatabase(mTwitter.getUserTweets(userId,args[2],id),context,TweetDatabase.USER_TL,userId);
homeTl = new TimelineAdapter(context,hTweets); homeTl = new TimelineAdapter(context,hTweets);
} else {
id = homeTl.getItemId(0);
homeTl.getData().add(mTwitter.getUserTweets(userId,args[2],id));
} }
} }
else if(MODE == GET_FAVS) else if(MODE == GET_FAVS)

View File

@ -12,9 +12,9 @@ public class AppDatabase extends SQLiteOpenHelper
"location TEXT, link TEXT, verify INTEGER);"; "location TEXT, link TEXT, verify INTEGER);";
private static final String tQuery = "CREATE TABLE IF NOT EXISTS tweet (" + private static final String tQuery = "CREATE TABLE IF NOT EXISTS tweet (" +
"tweetID INTEGER PRIMARY KEY, userID INTEGER," + "tweetID INTEGER PRIMARY KEY, userID INTEGER, retweeter TEXT," +
"time INTEGER, tweet TEXT, retweet INTEGER, favorite INTEGER," + "time INTEGER, tweet TEXT, retweet INTEGER, favorite INTEGER," +
"answers INTEGER, FOREIGN KEY (userID) REFERENCES user(userID));"; "FOREIGN KEY (userID) REFERENCES user(userID));";
private static final String trQuery = "CREATE TABLE IF NOT EXISTS trend (" + private static final String trQuery = "CREATE TABLE IF NOT EXISTS trend (" +
"trendpos INTEGER PRIMARY KEY, trendname TEXT, trendlink TEXT);"; "trendpos INTEGER PRIMARY KEY, trendname TEXT, trendlink TEXT);";

View File

@ -15,17 +15,16 @@ import twitter4j.Status;
import twitter4j.User; import twitter4j.User;
public class TweetDatabase { public class TweetDatabase {
public static final int HOME_TL = 0; // GET HOME TIMELINE public static final int HOME_TL = 0;
public static final int FAV_TL = 1; // GET FAVORITE TL public static final int FAV_TL = 1;
public static final int USER_TL = 2; // GET USERS TWEET TL @userID public static final int USER_TL = 2;
public static final int GET_TWEET = 3; // GET TWEET @ userID public static final int GET_TWEET = 3;
public static final int GET_MENT = 4; // GET MENTION TL public static final int GET_MENT = 4;
private AppDatabase dataHelper; private AppDatabase dataHelper;
private List<String> user,scrname,tweet,pbLink; private List<String> user,scrname,tweet,pbLink,retweeter;
private List<Long> userId,tweetId,timeMillis; private List<Long> userId,tweetId,timeMillis;
private List<Integer> noRT,noFav,noAns, verify; private List<Integer> noRT,noFav, verify;
private SharedPreferences settings;
private boolean toggleImg; private boolean toggleImg;
private int size = 0; private int size = 0;
private int mode = 0; private int mode = 0;
@ -110,6 +109,15 @@ public class TweetDatabase {
for(int pos = 0; pos < stats.size(); pos++) { for(int pos = 0; pos < stats.size(); pos++) {
Status stat = stats.get(pos); Status stat = stats.get(pos);
User usr = stat.getUser(); User usr = stat.getUser();
Status rtStat = stat.getRetweetedStatus();
if(rtStat != null) {
tweet.put("retweeter",usr.getScreenName());
stat = rtStat;
usr = rtStat.getUser();
} else {
tweet.put("retweeter","\0");
}
user.put("userID",usr.getId()); user.put("userID",usr.getId());
user.put("username", usr.getName()); user.put("username", usr.getName());
@ -126,7 +134,6 @@ public class TweetDatabase {
tweet.put("tweet", stat.getText()); tweet.put("tweet", stat.getText());
tweet.put("retweet", stat.getRetweetCount()); tweet.put("retweet", stat.getRetweetCount());
tweet.put("favorite", stat.getFavoriteCount()); tweet.put("favorite", stat.getFavoriteCount());
tweet.put("answers", 0);
home.put("tweetID", stat.getId()); home.put("tweetID", stat.getId());
fav.put("tweetID", stat.getId()); fav.put("tweetID", stat.getId());
@ -182,28 +189,28 @@ public class TweetDatabase {
if(cursor.moveToFirst()) { if(cursor.moveToFirst()) {
do { do {
index = cursor.getColumnIndex("time"); // time index = cursor.getColumnIndex("time");
timeMillis.add(cursor.getLong(index)); timeMillis.add(cursor.getLong(index));
index = cursor.getColumnIndex("tweet"); // tweet index = cursor.getColumnIndex("tweet");
tweet.add( cursor.getString(index) ); tweet.add( cursor.getString(index));
index = cursor.getColumnIndex("retweet"); // retweet index = cursor.getColumnIndex("retweet");
noRT.add( cursor.getInt(index) ); noRT.add( cursor.getInt(index));
index = cursor.getColumnIndex("favorite"); // fav index = cursor.getColumnIndex("favorite");
noFav.add( cursor.getInt(index) ); noFav.add( cursor.getInt(index));
index = cursor.getColumnIndex("answers"); // answers index = cursor.getColumnIndex("username");
noAns.add(cursor.getInt(index)); user.add(cursor.getString(index));
index = cursor.getColumnIndex("username"); // user index = cursor.getColumnIndex("scrname");
user.add(cursor.getString(index) ); scrname.add(cursor.getString(index));
index = cursor.getColumnIndex("scrname"); // username index = cursor.getColumnIndex("verify");
scrname.add(cursor.getString(index) );
index = cursor.getColumnIndex("verify"); // VERIFIED
verify.add(cursor.getInt(index)); verify.add(cursor.getInt(index));
index = cursor.getColumnIndex("pbLink"); // image index = cursor.getColumnIndex("pbLink");
pbLink.add(cursor.getString(index) ); pbLink.add(cursor.getString(index));
index = cursor.getColumnIndex("userID"); // UserID index = cursor.getColumnIndex("userID");
userId.add(cursor.getLong(index) ); userId.add(cursor.getLong(index));
index = cursor.getColumnIndex("tweetID"); // tweetID index = cursor.getColumnIndex("tweetID");
tweetId.add(cursor.getLong(index) ); tweetId.add(cursor.getLong(index));
index = cursor.getColumnIndex("retweeter");
retweeter.add(cursor.getString(index));
size++; size++;
} while(cursor.moveToNext() && size < limit); } while(cursor.moveToNext() && size < limit);
} }
@ -216,7 +223,6 @@ public class TweetDatabase {
} }
public int getRetweet(int pos){return noRT.get(pos);} public int getRetweet(int pos){return noRT.get(pos);}
public int getFavorite(int pos){return noFav.get(pos);} public int getFavorite(int pos){return noFav.get(pos);}
public int getAnswer(int pos){return noAns.get(pos);}
public long getUserID(int pos){return userId.get(pos);} public long getUserID(int pos){return userId.get(pos);}
public long getTweetId(int pos){return tweetId.get(pos);} public long getTweetId(int pos){return tweetId.get(pos);}
public long getTime(int pos){return timeMillis.get(pos);} public long getTime(int pos){return timeMillis.get(pos);}
@ -224,9 +230,13 @@ public class TweetDatabase {
public String getScreenname(int pos){return scrname.get(pos);} public String getScreenname(int pos){return scrname.get(pos);}
public String getTweet(int pos){return tweet.get(pos);} public String getTweet(int pos){return tweet.get(pos);}
public String getDate(int pos){return timeToString(getTime(pos));} public String getDate(int pos){return timeToString(getTime(pos));}
public String getPbLink (int pos){return pbLink.get(pos);} public String getPbLink(int pos){return pbLink.get(pos);}
public boolean loadImages(){return toggleImg;} public boolean loadImages(){return toggleImg;}
public boolean isVerified(int pos){return verify.get(pos) == 1;} public boolean isVerified(int pos){return verify.get(pos) == 1;}
public String getRetweeter(int pos) {
if(retweeter.get(pos).trim().isEmpty()) return "";
else return " RT @"+retweeter.get(pos);
}
/** /**
* Convert Time to String * Convert Time to String
@ -260,7 +270,7 @@ public class TweetDatabase {
private void initialize(Context c) { private void initialize(Context c) {
dataHelper = AppDatabase.getInstance(c); dataHelper = AppDatabase.getInstance(c);
settings = c.getSharedPreferences("settings", 0); SharedPreferences settings = c.getSharedPreferences("settings", 0);
limit = settings.getInt("limit", 200); limit = settings.getInt("limit", 200);
toggleImg = settings.getBoolean("image_load", true); toggleImg = settings.getBoolean("image_load", true);
initArray(); initArray();
@ -268,13 +278,20 @@ public class TweetDatabase {
private void insert(List<Status> stats) { private void insert(List<Status> stats) {
for(Status stat: stats) { for(Status stat: stats) {
Status rtStat = stat.getRetweetedStatus();
User usr = stat.getUser(); User usr = stat.getUser();
if(rtStat != null) {
retweeter.add(usr.getScreenName());
stat = rtStat;
usr = rtStat.getUser();
} else {
retweeter.add(" ");
}
user.add(usr.getName()); user.add(usr.getName());
scrname.add('@'+usr.getScreenName()); scrname.add('@'+usr.getScreenName());
tweet.add(stat.getText()); tweet.add(stat.getText());
noRT.add(stat.getRetweetCount()); noRT.add(stat.getRetweetCount());
noFav.add(stat.getFavoriteCount()); noFav.add(stat.getFavoriteCount());
noAns.add(0); // TODO
userId.add(usr.getId()); userId.add(usr.getId());
pbLink.add(usr.getMiniProfileImageURL()); pbLink.add(usr.getMiniProfileImageURL());
tweetId.add(stat.getId()); tweetId.add(stat.getId());
@ -287,13 +304,20 @@ public class TweetDatabase {
private void insertNew(List<Status> stats) { private void insertNew(List<Status> stats) {
for(int index = stats.size()-1 ; index >=0 ; index--) { for(int index = stats.size()-1 ; index >=0 ; index--) {
Status stat = stats.get(index); Status stat = stats.get(index);
Status rtStat = stat.getRetweetedStatus();
User usr = stat.getUser(); User usr = stat.getUser();
if(rtStat != null) {
retweeter.add(usr.getScreenName());
stat = rtStat;
usr = rtStat.getUser();
} else {
retweeter.add(0,"\0");
}
user.add(0,usr.getName()); user.add(0,usr.getName());
scrname.add(0,'@'+usr.getScreenName()); scrname.add(0,'@'+usr.getScreenName());
tweet.add(0,stat.getText()); tweet.add(0,stat.getText());
noRT.add(0,stat.getRetweetCount()); noRT.add(0,stat.getRetweetCount());
noFav.add(0,stat.getFavoriteCount()); noFav.add(0,stat.getFavoriteCount());
noAns.add(0,0); // TODO
userId.add(0,usr.getId()); userId.add(0,usr.getId());
pbLink.add(0,usr.getMiniProfileImageURL()); pbLink.add(0,usr.getMiniProfileImageURL());
tweetId.add(0,stat.getId()); tweetId.add(0,stat.getId());
@ -309,11 +333,11 @@ public class TweetDatabase {
tweet = new ArrayList<>(); tweet = new ArrayList<>();
noRT = new ArrayList<>(); noRT = new ArrayList<>();
noFav = new ArrayList<>(); noFav = new ArrayList<>();
noAns = new ArrayList<>();
userId = new ArrayList<>(); userId = new ArrayList<>();
pbLink = new ArrayList<>(); pbLink = new ArrayList<>();
tweetId = new ArrayList<>(); tweetId = new ArrayList<>();
verify = new ArrayList<>(); verify = new ArrayList<>();
retweeter = new ArrayList<>();
timeMillis = new ArrayList<>(); timeMillis = new ArrayList<>();
} }
} }

View File

@ -1,6 +1,7 @@
package org.nuclearfog.twidda.viewadapter; package org.nuclearfog.twidda.viewadapter;
import android.content.Context; import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
@ -71,13 +72,15 @@ public class TimelineAdapter extends ArrayAdapter implements View.OnClickListene
((TextView) v.findViewById(R.id.tweettext)).setText(highlight(mTweets.getTweet(position))); ((TextView) v.findViewById(R.id.tweettext)).setText(highlight(mTweets.getTweet(position)));
((TextView) v.findViewById(R.id.retweet_number)).setText(retweetStr); ((TextView) v.findViewById(R.id.retweet_number)).setText(retweetStr);
((TextView) v.findViewById(R.id.favorite_number)).setText(favoriteStr); ((TextView) v.findViewById(R.id.favorite_number)).setText(favoriteStr);
((TextView) v.findViewById(R.id.retweeter)).setText(mTweets.getRetweeter(position));
((TextView) v.findViewById(R.id.time)).setText(mTweets.getDate(position)); ((TextView) v.findViewById(R.id.time)).setText(mTweets.getDate(position));
((TextView) v.findViewById(R.id.tweettext)).setTextColor(textColor); ((TextView) v.findViewById(R.id.tweettext)).setTextColor(textColor);
ImageView pb = v.findViewById(R.id.tweetPb); ImageView pb = v.findViewById(R.id.tweetPb);
ImageView verify = v.findViewById(R.id.list_verify);
if(mTweets.isVerified(position)) { if(mTweets.isVerified(position)) {
v.findViewById(R.id.list_verify).setVisibility(View.VISIBLE); verify.setVisibility(ImageView.VISIBLE);
} else { } else {
v.findViewById(R.id.list_verify).setVisibility(View.INVISIBLE); verify.setVisibility(ImageView.INVISIBLE);
} }
if(mTweets.loadImages()) { if(mTweets.loadImages()) {
Picasso.with(context).load(mTweets.getPbLink(position)).into(pb); Picasso.with(context).load(mTweets.getPbLink(position)).into(pb);

View File

@ -190,5 +190,6 @@ public class AppSettings extends AppCompatActivity implements View.OnClickListen
edit.putBoolean("image_load", imgEnabled); edit.putBoolean("image_load", imgEnabled);
edit.apply(); edit.apply();
mColor.commit(); mColor.commit();
Toast.makeText(getApplicationContext(), "Gespeichert", Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -2,7 +2,6 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/twitterBlau"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView

View File

@ -3,7 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/twitterBlau"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
@ -21,18 +20,10 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:contentDescription="@string/profile_image" /> android:contentDescription="@string/profile_image" />
<ImageView
android:id="@+id/list_verify"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_margin="5dp"
android:contentDescription="@string/verify"
android:visibility="invisible"
app:srcCompat="@drawable/verify" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
@ -40,20 +31,31 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<ImageView
android:id="@+id/list_verify"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="@drawable/verify"
android:contentDescription="@string/verify" />
<TextView <TextView
android:id="@+id/username" android:id="@+id/username"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" /> android:layout_weight="1"
android:singleLine="true" />
<TextView <TextView
android:id="@+id/time" android:id="@+id/time"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="end" android:layout_gravity="end"
android:layout_weight="1" android:layout_weight="1"
android:gravity="end" android:gravity="end"
android:textAlignment="gravity" /> android:textAlignment="gravity"
android:textSize="12sp" />
</LinearLayout> </LinearLayout>
<TextView <TextView
@ -85,6 +87,13 @@
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView
android:id="@+id/retweeter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:textSize="12sp" />
<Button <Button
android:id="@+id/retweet" android:id="@+id/retweet"
android:layout_width="16dp" android:layout_width="16dp"
@ -96,7 +105,7 @@
android:layout_width="64dp" android:layout_width="64dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginStart="5dp" android:layout_marginStart="5dp"
android:ems="10" /> android:textSize="12sp" />
<Button <Button
android:id="@+id/favorite" android:id="@+id/favorite"
@ -110,7 +119,7 @@
android:layout_width="64dp" android:layout_width="64dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginStart="5dp" android:layout_marginStart="5dp"
android:ems="10" /> android:textSize="12sp" />
</LinearLayout> </LinearLayout>

View File

@ -24,28 +24,27 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_margin="5dp"
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/rt_info" android:id="@+id/rt_info"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"
android:layout_weight="1"
android:textSize="12sp" /> android:textSize="12sp" />
<TextView <TextView
android:id="@+id/answer_reference_detail" android:id="@+id/answer_reference_detail"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12sp" android:textSize="12sp"
android:visibility="invisible" /> android:visibility="invisible" />
</LinearLayout> </LinearLayout>
@ -112,8 +111,8 @@
<Button <Button
android:id="@+id/image_attach" android:id="@+id/image_attach"
android:layout_width="32dp" android:layout_width="24dp"
android:layout_height="32dp" android:layout_height="24dp"
android:background="@drawable/preview" android:background="@drawable/preview"
android:visibility="invisible" /> android:visibility="invisible" />

View File

@ -29,9 +29,9 @@
android:layout_width="16dp" android:layout_width="16dp"
android:layout_height="16dp" android:layout_height="16dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/verify"
android:contentDescription="@string/verify" android:contentDescription="@string/verify"
android:visibility="invisible" android:visibility="invisible" />
app:srcCompat="@drawable/verify" />
<TextView <TextView
android:id="@+id/username_detail" android:id="@+id/username_detail"
@ -50,9 +50,9 @@
android:layout_width="16dp" android:layout_width="16dp"
android:layout_height="16dp" android:layout_height="16dp"
android:layout_margin="2dp" android:layout_margin="2dp"
android:background="@drawable/lock"
android:contentDescription="@string/profile_locked" android:contentDescription="@string/profile_locked"
android:visibility="invisible" android:visibility="invisible" />
app:srcCompat="@drawable/lock" />
<TextView <TextView
android:id="@+id/screenname_detail" android:id="@+id/screenname_detail"