This commit is contained in:
NudeDude 2019-03-31 15:55:22 +02:00
parent bd0892dbe0
commit 2fd7414d1f
3 changed files with 169 additions and 114 deletions

View File

@ -22,6 +22,7 @@ import org.nuclearfog.twidda.backend.items.Tweet;
import org.nuclearfog.twidda.database.DatabaseAdapter;
import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.window.TweetDetail;
import org.nuclearfog.twidda.window.UserProfile;
import java.lang.ref.WeakReference;
import java.text.NumberFormat;
@ -56,61 +57,61 @@ public class StatusLoader extends AsyncTask<Long, Void, Void> {
private Tweet tweet;
private int highlight, font_color;
private boolean toggleImg, toggleAns;
private long homeId;
public StatusLoader(@NonNull TweetDetail context, Mode mode) {
mTwitter = TwitterEngine.getInstance(context);
GlobalSettings settings = GlobalSettings.getInstance(context);
RecyclerView replyList = context.findViewById(R.id.answer_list);
answerAdapter = (TimelineAdapter) replyList.getAdapter();
database = new DatabaseAdapter(context);
sdf = settings.getDateFormatter();
formatter = NumberFormat.getIntegerInstance();
font_color = settings.getFontColor();
highlight = settings.getHighlightColor();
toggleImg = settings.getImageLoad();
toggleAns = settings.getAnswerLoad();
homeId = settings.getUserId();
ui = new WeakReference<>(context);
answers = new ArrayList<>();
this.mode = mode;
RecyclerView replyList = context.findViewById(R.id.answer_list);
answerAdapter = (TimelineAdapter) replyList.getAdapter();
database = new DatabaseAdapter(context);
}
@Override
protected void onPreExecute() {
if (ui.get() == null) return;
if (toggleAns) {
if (mode == Mode.LOAD && toggleAns) {
SwipeRefreshLayout ansReload = ui.get().findViewById(R.id.answer_reload);
ansReload.setRefreshing(true);
}
}
/**
* @param data [0] TWEET ID , [1] Mode
* @param data [0] TWEET ID
*/
@Override
protected Void doInBackground(Long... data) {
final long TWEETID = data[0];
long sinceId = TWEETID;
try {
switch (mode) {
case LOAD:
case ANS:
if (database.containStatus(TWEETID) && answerAdapter.getItemCount() == 0) {
tweet = database.getStatus(TWEETID);
tweet = database.getStatus(TWEETID);
if(tweet != null) {
answers = database.getAnswers(TWEETID);
publishProgress();
}
case ANS:
tweet = mTwitter.getStatus(TWEETID);
if (toggleAns || mode == Mode.ANS) {
if (mode == Mode.ANS || toggleAns) {
if (answerAdapter.getItemCount() > 0)
sinceId = answerAdapter.getItemId(0);
answers = mTwitter.getAnswers(tweet.getUser().getScreenname(), TWEETID, sinceId);
} else {
answers = new ArrayList<>();
}
publishProgress();
if (database.containStatus(TWEETID)) {
database.updateStatus(tweet);
if (!answers.isEmpty())
@ -126,6 +127,7 @@ public class StatusLoader extends AsyncTask<Long, Void, Void> {
case RETWEET:
tweet = mTwitter.retweet(TWEETID);
publishProgress();
database.updateStatus(tweet);
if (!tweet.retweeted())
database.removeRetweet(TWEETID);
@ -155,12 +157,131 @@ public class StatusLoader extends AsyncTask<Long, Void, Void> {
return null;
}
@Override
protected void onProgressUpdate(Void... v) {
if (ui.get() == null) return;
if(mode == Mode.LOAD) {
TextView tweetText = ui.get().findViewById(R.id.tweet_detailed);
TextView date = ui.get().findViewById(R.id.timedetail);
TextView used_api = ui.get().findViewById(R.id.used_api);
View tweet_verify = ui.get().findViewById(R.id.tweet_verify);
View tweet_locked = ui.get().findViewById(R.id.tweet_locked);
Spannable sTweet = Tagger.makeText(tweet.getTweet(), highlight, ui.get());
tweetText.setMovementMethod(LinkMovementMethod.getInstance());
tweetText.setText(sTweet);
tweetText.setTextColor(font_color);
date.setText(sdf.format(tweet.getTime()));
date.setTextColor(font_color);
used_api.setText(R.string.sent_from);
used_api.append(tweet.getSource());
used_api.setTextColor(font_color);
if (tweet.getUser().isVerified()) {
tweet_verify.setVisibility(View.VISIBLE);
}
if (tweet.getUser().isLocked()) {
tweet_locked.setVisibility(View.VISIBLE);
}
if (tweet.getMediaLinks() != null && tweet.getMediaLinks().length != 0) {
View mediaButton = ui.get().findViewById(R.id.image_attach);
mediaButton.setVisibility(View.VISIBLE);
if(!mediaButton.isClickable()) {
mediaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ui.get().imageClick(tweet.getMediaLinks());
}
});
}
}
}
TextView username = ui.get().findViewById(R.id.usernamedetail);
TextView scrName = ui.get().findViewById(R.id.scrnamedetail);
TextView replyName = ui.get().findViewById(R.id.answer_reference_detail);
TextView txtRet = ui.get().findViewById(R.id.no_rt_detail);
TextView txtFav = ui.get().findViewById(R.id.no_fav_detail);
TextView txtAns = ui.get().findViewById(R.id.no_ans_detail);
ImageView profile_img = ui.get().findViewById(R.id.profileimage_detail);
ImageView retweetButton = ui.get().findViewById(R.id.rt_button_detail);
ImageView favoriteButton = ui.get().findViewById(R.id.fav_button_detail);
username.setText(tweet.getUser().getUsername());
username.setTextColor(font_color);
scrName.setText(tweet.getUser().getScreenname());
scrName.setTextColor(font_color);
txtFav.setText(formatter.format(tweet.getFavorCount()));
txtRet.setText(formatter.format(tweet.getRetweetCount()));
txtAns.setText(formatter.format(answerAdapter.getItemCount() + answers.size()));
if (tweet.getReplyId() > 1) {
String reply = ui.get().getString(R.string.answering);
reply += tweet.getReplyName();
replyName.setText(reply);
replyName.setVisibility(View.VISIBLE);
if(!replyName.isClickable()) {
replyName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ui.get(), TweetDetail.class);
intent.putExtra("tweetID", tweet.getReplyId());
intent.putExtra("userID", tweet.getReplyUserId());
intent.putExtra("username", tweet.getReplyName());
ui.get().startActivity(intent);
}
});
}
}
if(!profile_img.isClickable()) {
profile_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profile = new Intent(ui.get(), UserProfile.class);
profile.putExtra("userID", tweet.getUser().getId());
profile.putExtra("username", tweet.getUser().getUsername());
ui.get().startActivity(profile);
}
});
}
if (toggleImg) {
Picasso.get().load(tweet.getUser().getImageLink() + "_bigger").into(profile_img);
}
if (tweet.retweeted()) {
retweetButton.setImageResource(R.drawable.retweet_enabled);
} else {
retweetButton.setImageResource(R.drawable.retweet);
}
if (tweet.favored()) {
favoriteButton.setImageResource(R.drawable.favorite_enabled);
} else {
favoriteButton.setImageResource(R.drawable.favorite);
}
if(tweet.getUser().getId() == homeId) {
ui.get().setOptionsmenu();
}
if(!answers.isEmpty()) {
SwipeRefreshLayout ansReload = ui.get().findViewById(R.id.answer_reload);
ansReload.setRefreshing(false);
answerAdapter.setData(answers);
answerAdapter.notifyDataSetChanged();
}
}
@Override
protected void onPostExecute(Void v) {
if (ui.get() == null) return;
SwipeRefreshLayout ansReload = ui.get().findViewById(R.id.answer_reload);
ansReload.setRefreshing(false);
if(answers.isEmpty()) {
SwipeRefreshLayout ansReload = ui.get().findViewById(R.id.answer_reload);
ansReload.setRefreshing(false);
}
if (!failure) {
if (mode == Mode.DELETE) {
@ -178,89 +299,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Void> {
}
@Override
protected void onProgressUpdate(Void... v) {
if (ui.get() == null) return;
TextView tweetText = ui.get().findViewById(R.id.tweet_detailed);
TextView username = ui.get().findViewById(R.id.usernamedetail);
TextView scrName = ui.get().findViewById(R.id.scrnamedetail);
TextView date = ui.get().findViewById(R.id.timedetail);
TextView replyName = ui.get().findViewById(R.id.answer_reference_detail);
TextView used_api = ui.get().findViewById(R.id.used_api);
TextView txtRet = ui.get().findViewById(R.id.no_rt_detail);
TextView txtFav = ui.get().findViewById(R.id.no_fav_detail);
TextView txtAns = ui.get().findViewById(R.id.no_ans_detail);
ImageView profile_img = ui.get().findViewById(R.id.profileimage_detail);
ImageView retweetButton = ui.get().findViewById(R.id.rt_button_detail);
ImageView favoriteButton = ui.get().findViewById(R.id.fav_button_detail);
View mediaButton = ui.get().findViewById(R.id.image_attach);
View tweet_verify = ui.get().findViewById(R.id.tweet_verify);
Spannable sTweet = Tagger.makeText(tweet.getTweet(), highlight, ui.get());
tweetText.setMovementMethod(LinkMovementMethod.getInstance());
tweetText.setText(sTweet);
tweetText.setTextColor(font_color);
username.setText(tweet.getUser().getUsername());
username.setTextColor(font_color);
scrName.setText(tweet.getUser().getScreenname());
scrName.setTextColor(font_color);
date.setText(sdf.format(tweet.getTime()));
date.setTextColor(font_color);
used_api.setText(R.string.sent_from);
used_api.append(tweet.getSource());
used_api.setTextColor(font_color);
txtFav.setText(formatter.format(tweet.getFavorCount()));
txtRet.setText(formatter.format(tweet.getRetweetCount()));
txtAns.setText(formatter.format(answerAdapter.getItemCount() + answers.size()));
if (tweet.getReplyId() > 1) {
String reply = ui.get().getString(R.string.answering);
reply += tweet.getReplyName();
replyName.setText(reply);
replyName.setVisibility(View.VISIBLE);
replyName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ui.get(), TweetDetail.class);
intent.putExtra("tweetID", tweet.getReplyId());
intent.putExtra("userID", tweet.getReplyUserId());
intent.putExtra("username", tweet.getReplyName());
ui.get().startActivity(intent);
}
});
}
if (tweet.getMediaLinks() != null && tweet.getMediaLinks().length != 0) {
mediaButton.setVisibility(View.VISIBLE);
mediaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ui.get().imageClick(tweet.getMediaLinks());
}
});
}
if (tweet.getUser().isVerified()) {
tweet_verify.setVisibility(View.VISIBLE);
}
if (toggleImg) {
Picasso.get().load(tweet.getUser().getImageLink() + "_bigger").into(profile_img);
}
if (tweet.retweeted()) {
retweetButton.setImageResource(R.drawable.retweet_enabled);
} else {
retweetButton.setImageResource(R.drawable.retweet);
}
if (tweet.favored()) {
favoriteButton.setImageResource(R.drawable.favorite_enabled);
} else {
favoriteButton.setImageResource(R.drawable.favorite);
}
answerAdapter.setData(answers);
answerAdapter.notifyDataSetChanged();
}
@Override
protected void onCancelled() {
if (ui.get() == null) return;

View File

@ -57,7 +57,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
private ConnectivityManager mConnect;
private String username = "";
private boolean isHome;
private long userID = 0;
private long tweetID = 0;
@ -69,7 +68,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
Bundle param = getIntent().getExtras();
if (param != null) {
tweetID = param.getLong("tweetID");
userID = param.getLong("userID");
username = param.getString("username");
}
@ -79,7 +77,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
getSupportActionBar().setDisplayShowTitleEnabled(false);
settings = GlobalSettings.getInstance(this);
isHome = userID == settings.getUserId();
mConnect = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
View root = findViewById(R.id.tweet_layout);
@ -87,7 +84,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
View favorite = findViewById(R.id.fav_button_detail);
View txtRt = findViewById(R.id.no_rt_detail);
View txtFav = findViewById(R.id.no_fav_detail);
View profile_img = findViewById(R.id.profileimage_detail);
View answer = findViewById(R.id.answer_button);
TextView tweetTxt = findViewById(R.id.tweet_detailed);
answerReload = findViewById(R.id.answer_reload);
@ -102,7 +98,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
answerReload.setOnRefreshListener(this);
txtFav.setOnClickListener(this);
txtRt.setOnClickListener(this);
profile_img.setOnClickListener(this);
answer.setOnClickListener(this);
}
@ -140,9 +135,15 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override
public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.tweet, m);
return super.onCreateOptionsMenu(m);
}
@Override
public boolean onPrepareOptionsMenu(Menu m) {
if (isHome)
m.findItem(R.id.delete_tweet).setVisible(true);
return super.onCreateOptionsMenu(m);
return super.onPrepareOptionsMenu(m);
}
@ -224,13 +225,6 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
startActivity(favorit);
break;
case R.id.profileimage_detail:
Intent profile = new Intent(this, UserProfile.class);
profile.putExtra("userID", userID);
profile.putExtra("username", username);
startActivity(profile);
break;
case R.id.answer_button:
Intent tweet = new Intent(this, TweetPopup.class);
tweet.putExtra("TweetID", tweetID);
@ -274,4 +268,10 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
image.putExtra("link", mediaLinks);
startActivity(image);
}
public void setOptionsmenu() {
isHome = true;
invalidateOptionsMenu();
}
}

View File

@ -82,11 +82,28 @@
android:singleLine="true" />
</LinearLayout>
<TextView
android:id="@+id/scrnamedetail"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/tweet_locked"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_marginEnd="@dimen/icon_margin"
android:layout_marginRight="@dimen/icon_margin"
android:layout_weight="1"
android:contentDescription="@string/verify"
android:visibility="gone"
app:srcCompat="@drawable/lock" />
<TextView
android:id="@+id/scrnamedetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
<TextView
android:id="@+id/timedetail"