fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/ShowConversationActivity.java

432 lines
19 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-05-05 16:36:04 +02:00
2019-01-11 17:57:01 +01:00
import android.content.BroadcastReceiver;
2018-11-16 15:39:53 +01:00
import android.content.Intent;
2019-01-11 17:57:01 +01:00
import android.content.IntentFilter;
2017-05-05 16:36:04 +02:00
import android.content.SharedPreferences;
2017-08-16 15:25:42 +02:00
import android.database.sqlite.SQLiteDatabase;
2017-05-05 16:36:04 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
2017-08-16 15:25:42 +02:00
import android.view.LayoutInflater;
2017-05-05 16:36:04 +02:00
import android.view.MenuItem;
import android.view.View;
2017-08-16 15:25:42 +02:00
import android.view.ViewGroup;
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2017-08-16 15:25:42 +02:00
import android.widget.TextView;
import android.widget.Toast;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Context;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.drawers.ConversationDecoration;
import app.fedilab.android.drawers.StatusListAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveContextAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.android.interfaces.OnRetrieveContextInterface;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 04/05/2017.
* Show conversation activity class
*/
2019-09-06 17:55:14 +02:00
public class ShowConversationActivity extends BaseActivity implements OnRetrieveContextInterface {
2017-05-05 16:36:04 +02:00
private Status initialStatus;
2018-11-15 19:09:44 +01:00
private Status detailsStatus;
private SwipeRefreshLayout swipeRefreshLayout;
2017-10-14 14:36:48 +02:00
private RecyclerView lv_status;
2017-08-16 15:25:42 +02:00
private ImageView pp_actionBar;
private List<Status> statuses;
2017-12-20 13:57:28 +01:00
private StatusListAdapter statusListAdapter;
2018-03-15 18:28:12 +01:00
private boolean expanded;
2019-01-11 17:57:01 +01:00
private BroadcastReceiver receive_action;
2019-02-21 19:06:04 +01:00
private String conversationId;
2019-06-06 14:16:33 +02:00
private boolean spoilerShown, spoilerBehaviour;
2018-03-15 18:28:12 +01:00
2017-05-05 16:36:04 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-05-11 11:28:05 +02:00
case Helper.THEME_LIGHT:
2019-07-20 15:15:47 +02:00
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
2018-05-11 11:28:05 +02:00
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark_NoActionBar);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
2017-06-30 17:09:07 +02:00
}
2018-03-15 18:28:12 +01:00
2017-05-05 16:36:04 +02:00
setContentView(R.layout.activity_show_conversation);
2018-10-25 14:09:41 +02:00
lv_status = findViewById(R.id.lv_status);
Toolbar toolbar = findViewById(R.id.toolbar);
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_BLACK)
toolbar.setBackgroundColor(ContextCompat.getColor(ShowConversationActivity.this, R.color.black));
setSupportActionBar(toolbar);
2019-09-06 17:55:14 +02:00
spoilerShown = spoilerBehaviour = sharedpreferences.getBoolean(Helper.SET_EXPAND_CW, false);
2018-03-15 18:28:12 +01:00
Bundle b = getIntent().getExtras();
2018-11-15 19:09:44 +01:00
statuses = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (b != null) {
2018-11-15 19:09:44 +01:00
detailsStatus = b.getParcelable("status");
2019-09-06 17:55:14 +02:00
expanded = b.getBoolean("expanded", false);
2018-11-16 15:39:53 +01:00
initialStatus = b.getParcelable("initialStatus");
2019-09-06 17:55:14 +02:00
conversationId = b.getString("conversationId", null);
2018-11-16 15:39:53 +01:00
}
2019-09-06 17:55:14 +02:00
if (detailsStatus == null || detailsStatus.getId() == null)
2018-03-15 18:28:12 +01:00
finish();
2019-01-11 17:57:01 +01:00
2019-08-18 17:53:56 +02:00
detailsStatus.setFocused(true);
2019-09-06 17:55:14 +02:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
2019-01-11 17:57:01 +01:00
2019-09-06 17:55:14 +02:00
if (receive_action != null)
2019-01-11 17:57:01 +01:00
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(receive_action);
receive_action = new BroadcastReceiver() {
@Override
public void onReceive(android.content.Context context, Intent intent) {
Bundle b = intent.getExtras();
assert b != null;
Status status = b.getParcelable("status");
2019-09-06 17:55:14 +02:00
if (status != null && statusListAdapter != null) {
2019-03-30 18:48:38 +01:00
statusListAdapter.notifyStatusWithActionChanged(status);
2019-01-11 17:57:01 +01:00
}
}
};
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
}
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null) {
2019-05-18 11:10:30 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
2017-10-27 15:34:53 +02:00
assert inflater != null;
2019-09-06 17:55:14 +02:00
View view = inflater.inflate(R.layout.conversation_action_bar, new LinearLayout(getApplicationContext()), false);
getSupportActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
TextView title = getSupportActionBar().getCustomView().findViewById(R.id.toolbar_title);
pp_actionBar = getSupportActionBar().getCustomView().findViewById(R.id.pp_actionBar);
ImageView action_refresh = getSupportActionBar().getCustomView().findViewById(R.id.action_refresh);
2018-11-16 15:39:53 +01:00
ImageView action_expand = getSupportActionBar().getCustomView().findViewById(R.id.action_expand);
2017-08-16 15:25:42 +02:00
title.setText(R.string.conversation);
ImageView close_conversation = getSupportActionBar().getCustomView().findViewById(R.id.close_conversation);
2019-01-31 18:01:08 +01:00
ImageView action_unhide = getSupportActionBar().getCustomView().findViewById(R.id.action_unhide);
2019-09-06 17:55:14 +02:00
if (expanded)
2018-11-16 15:39:53 +01:00
action_expand.setImageResource(R.drawable.ic_expand_less);
else
action_expand.setImageResource(R.drawable.ic_expand_more);
2017-12-26 10:06:22 +01:00
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2019-09-06 17:55:14 +02:00
if (lv_status != null) {
2017-12-26 10:06:22 +01:00
lv_status.setAdapter(statusListAdapter);
}
}
});
2019-09-06 17:55:14 +02:00
if (close_conversation != null) {
2017-08-16 15:25:42 +02:00
close_conversation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
action_refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-11-16 15:39:53 +01:00
Intent intent = new Intent(ShowConversationActivity.this, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putParcelable("status", detailsStatus);
b.putBoolean("expanded", expanded);
2019-09-06 17:55:14 +02:00
if (expanded && statuses != null && statuses.size() > 0)
2018-11-16 15:39:53 +01:00
b.putParcelable("initialStatus", statuses.get(0));
intent.putExtras(b);
finish();
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
2019-01-31 18:01:08 +01:00
action_unhide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0) {
2019-06-06 14:16:33 +02:00
spoilerShown = !spoilerShown;
2019-01-31 18:01:08 +01:00
for (Status status : statuses) {
2019-09-06 17:55:14 +02:00
if (spoilerBehaviour && !status.isSpoilerShown()) {
2019-06-06 14:16:33 +02:00
status.setAutoHiddenCW(true);
2019-09-06 17:55:14 +02:00
} else {
2019-06-06 14:16:33 +02:00
status.setAutoHiddenCW(false);
}
status.setSpoilerShown(spoilerShown);
status.setShowSpoiler(spoilerShown);
2019-01-31 18:01:08 +01:00
}
statusListAdapter.notifyItemRangeChanged(0, statuses.size());
}
2019-06-06 14:16:33 +02:00
2019-01-31 18:01:08 +01:00
}
});
2018-03-15 18:28:12 +01:00
action_expand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expanded = !expanded;
2018-11-16 15:39:53 +01:00
Intent intent = new Intent(ShowConversationActivity.this, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putParcelable("status", detailsStatus);
b.putBoolean("expanded", expanded);
2019-09-06 17:55:14 +02:00
if (expanded && statuses != null && statuses.size() > 0)
2018-11-16 15:39:53 +01:00
b.putParcelable("initialStatus", statuses.get(0));
intent.putExtras(b);
finish();
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
2018-03-15 18:28:12 +01:00
}
});
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_LIGHT) {
2018-11-03 12:06:44 +01:00
Helper.colorizeToolbar(getSupportActionBar().getCustomView().findViewById(R.id.toolbar), R.color.black, ShowConversationActivity.this);
}
2019-09-06 17:55:14 +02:00
} else {
2017-08-16 15:25:42 +02:00
setTitle(R.string.conversation);
}
2017-12-26 10:06:22 +01:00
2017-08-16 15:25:42 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2019-06-05 14:35:42 +02:00
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
2019-09-06 17:55:14 +02:00
Account account = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
if (account.getAvatar() == null) {
Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show();
finish();
}
2019-08-02 13:41:08 +02:00
Helper.loadGiF(getApplicationContext(), account.getAvatar(), pp_actionBar);
2018-03-15 18:28:12 +01:00
2017-08-16 15:25:42 +02:00
2017-10-21 12:32:27 +02:00
swipeRefreshLayout = findViewById(R.id.swipeContainer);
2018-11-15 19:09:44 +01:00
boolean isOnWifi = Helper.isOnWIFI(getApplicationContext());
2019-09-06 17:55:14 +02:00
if (initialStatus != null)
2018-11-16 15:39:53 +01:00
statuses.add(initialStatus);
else
statuses.add(detailsStatus);
2019-08-18 17:17:47 +02:00
statusListAdapter = new StatusListAdapter(0, null, isOnWifi, statuses);
2018-11-15 19:09:44 +01:00
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
lv_status.setLayoutManager(mLayoutManager);
2019-02-13 18:36:29 +01:00
lv_status.addItemDecoration(new ConversationDecoration(ShowConversationActivity.this, theme));
2018-11-15 19:09:44 +01:00
lv_status.setAdapter(statusListAdapter);
2018-11-16 16:35:25 +01:00
String statusIdToFetch = null;
2019-09-06 17:55:14 +02:00
if (initialStatus != null)
2018-11-16 15:39:53 +01:00
statusIdToFetch = initialStatus.getId();
2019-09-06 17:55:14 +02:00
else if (detailsStatus != null)
2018-11-16 15:39:53 +01:00
statusIdToFetch = detailsStatus.getId();
2019-09-06 17:55:14 +02:00
if (statusIdToFetch == null)
2018-11-16 16:35:25 +01:00
finish();
2019-09-06 17:55:14 +02:00
if (conversationId != null)
2019-02-21 19:06:04 +01:00
statusIdToFetch = conversationId;
2019-09-06 17:55:14 +02:00
new RetrieveContextAsyncTask(getApplicationContext(), expanded, detailsStatus.getVisibility().equals("direct"), statusIdToFetch, ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
switch (theme) {
2018-05-11 16:33:16 +02:00
case Helper.THEME_LIGHT:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(ShowConversationActivity.this, R.color.white));
break;
case Helper.THEME_DARK:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4__,
R.color.mastodonC4,
R.color.mastodonC4);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(ShowConversationActivity.this, R.color.mastodonC1_));
break;
case Helper.THEME_BLACK:
swipeRefreshLayout.setColorSchemeResources(R.color.dark_icon,
R.color.mastodonC2,
R.color.mastodonC3);
2018-05-12 12:06:43 +02:00
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(ShowConversationActivity.this, R.color.black_3));
2018-05-11 16:33:16 +02:00
break;
}
2019-07-07 18:08:52 +02:00
swipeRefreshLayout.setDistanceToTriggerSync(500);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
2018-11-16 15:39:53 +01:00
Intent intent = new Intent(ShowConversationActivity.this, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putParcelable("status", detailsStatus);
b.putBoolean("expanded", expanded);
2019-09-06 17:55:14 +02:00
if (expanded && statuses != null && statuses.size() > 0)
2018-11-16 15:39:53 +01:00
b.putParcelable("initialStatus", statuses.get(0));
b.putParcelable("status", detailsStatus);
intent.putExtras(b);
finish();
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
2018-10-25 14:09:41 +02:00
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
public void addStatuses(Status status) {
if (status != null && status.getIn_reply_to_id() != null && this.statuses != null) {
int position = 0;
2019-09-06 17:55:14 +02:00
for (Status s : this.statuses) {
if (status.getIn_reply_to_id().equals(s.getId())) {
this.statuses.add(position + 1, status);
statusListAdapter.notifyItemInserted(position + 1);
break;
}
position++;
}
}
}
@Override
public void onStop() {
super.onStop();
if (statusListAdapter != null) {
statusListAdapter.storeToot();
}
}
2017-05-05 16:36:04 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2019-01-11 17:57:01 +01:00
@Override
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (receive_action != null)
2019-01-11 17:57:01 +01:00
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(receive_action);
}
2017-05-05 16:36:04 +02:00
@Override
2018-11-16 15:39:53 +01:00
public void onRetrieveContext(Context context, Error error) {
swipeRefreshLayout.setRefreshing(false);
2019-09-06 17:55:14 +02:00
if (error != null) {
Toasty.error(getApplicationContext(), error.getError(), Toast.LENGTH_LONG).show();
return;
}
2019-09-06 17:55:14 +02:00
if (context.getAncestors() == null) {
2018-11-17 14:31:03 +01:00
return;
}
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
statusListAdapter.setConversationPosition(context.getAncestors().size());
if (!expanded) {
2019-02-21 19:06:04 +01:00
if (context.getAncestors() != null && context.getAncestors().size() > 0) {
statuses.addAll(0, context.getAncestors());
statusListAdapter.notifyItemRangeInserted(0, context.getAncestors().size());
}
if (context.getDescendants() != null && context.getDescendants().size() > 0) {
statuses.addAll(context.getAncestors().size() + 1, context.getDescendants());
2019-09-06 17:55:14 +02:00
statusListAdapter.notifyItemRangeChanged(context.getAncestors().size() + 1, context.getDescendants().size());
2019-02-21 19:06:04 +01:00
}
2019-09-06 17:55:14 +02:00
} else {
2019-02-21 19:06:04 +01:00
List<Status> statusesTemp = context.getDescendants();
int i = 1;
int position = 0;
2019-09-06 17:55:14 +02:00
for (Status status : statusesTemp) {
2019-02-21 19:06:04 +01:00
statuses.add(status);
2019-09-06 17:55:14 +02:00
if (status.getId().equals(detailsStatus.getId())) {
2019-02-21 19:06:04 +01:00
statusListAdapter.setConversationPosition(i);
detailsStatus = status;
position = i;
}
i++;
}
2019-09-06 17:55:14 +02:00
statusListAdapter.notifyItemRangeChanged(1, context.getDescendants().size());
2019-02-21 19:06:04 +01:00
lv_status.scrollToPosition(position);
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
} else {
2019-02-21 19:06:04 +01:00
int i = 0;
2019-09-06 17:55:14 +02:00
if (context.getAncestors() != null && context.getAncestors().size() > 1) {
2019-02-21 19:06:04 +01:00
statuses = new ArrayList<>();
statuses.clear();
2019-09-06 17:55:14 +02:00
for (Status status : context.getAncestors()) {
if (detailsStatus.equals(status)) {
2019-02-21 19:06:04 +01:00
break;
}
i++;
}
boolean isOnWifi = Helper.isOnWIFI(getApplicationContext());
2019-09-06 17:55:14 +02:00
for (Status status : context.getAncestors()) {
statuses.add(0, status);
2019-02-21 19:06:04 +01:00
}
2019-09-06 17:55:14 +02:00
statusListAdapter = new StatusListAdapter((statuses.size() - 1 - i), null, isOnWifi, statuses);
statusListAdapter.setConversationPosition((statuses.size() - 1 - i));
2019-02-21 19:06:04 +01:00
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
lv_status.setLayoutManager(mLayoutManager);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2019-02-21 19:06:04 +01:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2019-02-21 19:06:04 +01:00
case Helper.THEME_LIGHT:
2019-07-20 15:15:47 +02:00
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
2019-02-21 19:06:04 +01:00
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark_NoActionBar);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
2017-12-22 14:46:27 +01:00
}
2019-02-21 19:06:04 +01:00
lv_status.addItemDecoration(new ConversationDecoration(ShowConversationActivity.this, theme));
lv_status.setAdapter(statusListAdapter);
2017-12-20 13:57:28 +01:00
}
}
2019-02-21 19:06:04 +01:00
2017-12-20 13:57:28 +01:00
}
2018-11-16 15:39:53 +01:00
2017-05-05 16:36:04 +02:00
}