fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowConversationActivity.java

374 lines
17 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
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.
*
2017-07-10 10:33:24 +02:00
* Mastalab 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.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
2017-10-27 15:34:53 +02:00
import android.annotation.SuppressLint;
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;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
2017-05-05 16:36:04 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
2018-03-15 18:28:12 +01:00
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
2017-08-16 15:25:42 +02:00
import android.support.v7.app.ActionBar;
2017-10-14 14:36:48 +02:00
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.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;
2017-05-05 16:36:04 +02:00
import android.widget.RelativeLayout;
2017-08-16 15:25:42 +02:00
import android.widget.TextView;
import android.widget.Toast;
2017-05-05 16:36:04 +02:00
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.asynctasks.RetrieveContextAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
2017-06-03 18:18:27 +02:00
import fr.gouv.etalab.mastodon.client.APIResponse;
2017-08-16 15:25:42 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Account;
2017-12-20 13:57:28 +01:00
import fr.gouv.etalab.mastodon.client.Entities.Card;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Context;
import fr.gouv.etalab.mastodon.client.Entities.Error;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Status;
2018-09-08 11:30:58 +02:00
import fr.gouv.etalab.mastodon.drawers.ConversationDecoration;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
2017-12-20 13:57:28 +01:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveCardInterface;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveContextInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
2017-08-16 15:25:42 +02:00
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
2017-05-05 16:36:04 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK;
2018-11-03 12:06:44 +01:00
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 04/05/2017.
* Show conversation activity class
*/
2017-12-20 13:57:28 +01:00
public class ShowConversationActivity extends BaseActivity implements OnRetrieveFeedsInterface, OnRetrieveContextInterface, OnRetrieveCardInterface {
2017-05-05 16:36:04 +02:00
private String statusId;
private Status initialStatus;
private SwipeRefreshLayout swipeRefreshLayout;
2017-10-14 14:36:48 +02:00
private RecyclerView lv_status;
2017-07-08 11:32:38 +02:00
private boolean isRefreshed;
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;
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
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2018-05-11 11:28:05 +02:00
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme_NoActionBar);
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
expanded = false;
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);
if( theme == THEME_BLACK)
toolbar.setBackgroundColor(ContextCompat.getColor(ShowConversationActivity.this, R.color.black));
setSupportActionBar(toolbar);
2018-03-15 18:28:12 +01:00
Bundle b = getIntent().getExtras();
if(b != null)
statusId = b.getString("statusId", null);
if( statusId == null)
finish();
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if( getSupportActionBar() != null) {
2017-08-16 15:25:42 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
2017-10-27 15:34:53 +02:00
assert inflater != null;
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.conversation_action_bar, null);
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-03-15 18:28:12 +01:00
final 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);
2017-12-26 10:06:22 +01:00
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( lv_status != null) {
lv_status.setAdapter(statusListAdapter);
}
}
});
2017-08-16 15:25:42 +02:00
if( close_conversation != null){
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) {
if( statuses != null) {
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
isRefreshed = true;
statusId = statuses.get(statuses.size() - 1).getId();
new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.ONESTATUS, statusId, null, false, false, ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}, 1000);
}
}
});
2018-03-15 18:28:12 +01:00
action_expand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expanded = !expanded;
if( expanded)
action_expand.setImageResource(R.drawable.ic_expand_less);
else
action_expand.setImageResource(R.drawable.ic_expand_more);
new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.ONESTATUS, statusId,null, false,false, ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
2018-11-03 12:06:44 +01:00
if (theme == THEME_LIGHT){
Helper.colorizeToolbar(getSupportActionBar().getCustomView().findViewById(R.id.toolbar), R.color.black, ShowConversationActivity.this);
}
2017-08-16 15:25:42 +02:00
}else{
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);
Account account = new AccountDAO(getApplicationContext(),db).getAccountByID(userId);
if( account.getAvatar() == null){
Toast.makeText(ShowConversationActivity.this,R.string.toast_error, Toast.LENGTH_LONG).show();
finish();
}
2017-08-16 15:25:42 +02:00
String url = account.getAvatar();
if( url.startsWith("/") ){
2018-01-24 15:56:33 +01:00
url = Helper.getLiveInstanceWithProtocol(getApplicationContext()) + account.getAvatar();
2017-08-16 15:25:42 +02:00
}
2017-12-02 11:02:25 +01:00
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
2018-03-15 18:28:12 +01:00
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2017-12-02 11:02:25 +01:00
BitmapDrawable ppDrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(resource, (int) Helper.convertDpToPixel(25, getApplicationContext()), (int) Helper.convertDpToPixel(25, getApplicationContext()), true));
if( pp_actionBar != null){
pp_actionBar.setImageDrawable(ppDrawable);
} else if( getSupportActionBar() != null){
getSupportActionBar().setIcon(ppDrawable);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
});
2017-08-16 15:25:42 +02:00
2018-03-15 18:28:12 +01:00
2017-07-08 11:32:38 +02:00
isRefreshed = false;
2017-08-16 15:25:42 +02:00
2017-10-21 12:32:27 +02:00
swipeRefreshLayout = findViewById(R.id.swipeContainer);
new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.ONESTATUS, statusId,null, false,false, ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2018-05-11 16:33:16 +02:00
switch (theme){
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;
}
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
2017-07-08 11:32:38 +02:00
isRefreshed = true;
new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.ONESTATUS, statusId,null, false,false, ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
2018-10-25 14:09:41 +02:00
2017-05-05 16:36:04 +02:00
}
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);
}
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
2017-06-03 18:18:27 +02:00
if( apiResponse.getError() != null){
2018-09-09 13:58:29 +02:00
Toast.makeText(getApplicationContext(), apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
return;
}
2017-06-03 18:18:27 +02:00
List<Status> statuses = apiResponse.getStatuses();
2017-05-05 16:36:04 +02:00
if( statuses != null && statuses.size() > 0 ){
initialStatus = statuses.get(0);
2018-03-15 18:28:12 +01:00
new RetrieveContextAsyncTask(getApplicationContext(), expanded, initialStatus.getId(), ShowConversationActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-05 16:36:04 +02:00
}
}
@Override
2017-10-21 14:35:36 +02:00
public void onRetrieveContext(Context context, Status statusFirst, Error error) {
swipeRefreshLayout.setRefreshing(false);
2017-10-29 07:32:52 +01:00
RelativeLayout loader = findViewById(R.id.loader);
if( error != null){
2018-09-09 13:58:29 +02:00
Toast.makeText(getApplicationContext(), error.getError(),Toast.LENGTH_LONG).show();
return;
}
2018-10-25 14:09:41 +02:00
2017-05-05 16:36:04 +02:00
boolean isOnWifi = Helper.isOnWIFI(getApplicationContext());
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
2017-08-25 16:08:40 +02:00
int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
2018-10-25 14:09:41 +02:00
int position = 0;
2017-10-21 12:32:27 +02:00
boolean positionFound = false;
statuses = new ArrayList<>();
2018-03-15 18:28:12 +01:00
if( expanded) {
if (statusFirst != null)
statuses.add(0, statusFirst);
if (context.getAncestors() != null && context.getAncestors().size() > 0) {
for (Status status : context.getAncestors()) {
statuses.add(status);
if (!positionFound)
position++;
if (status.getId().equals(initialStatus.getId()))
positionFound = true;
2017-10-21 14:35:36 +02:00
2018-03-15 18:28:12 +01:00
}
} else if (statusFirst == null) {
statuses.add(0, initialStatus);
positionFound = true;
2017-05-05 16:36:04 +02:00
}
2018-03-15 18:28:12 +01:00
if (context.getDescendants() != null && context.getDescendants().size() > 0) {
for (Status status : context.getDescendants()) {
statuses.add(status);
if (!positionFound)
position++;
if (status.getId().equals(initialStatus.getId()))
positionFound = true;
2017-10-21 14:35:36 +02:00
2018-03-15 18:28:12 +01:00
}
}
}else {
position = 0;
if (context.getAncestors() != null && context.getAncestors().size() > 0) {
statuses.addAll(context.getAncestors());
position = context.getAncestors().size();
}
statuses.add(initialStatus);
if (context.getDescendants() != null && context.getDescendants().size() > 0) {
statuses.addAll(context.getDescendants());
2017-05-05 16:36:04 +02:00
}
}
2018-10-25 14:09:41 +02:00
statusListAdapter = new StatusListAdapter(ShowConversationActivity.this, position, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses);
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
lv_status.setLayoutManager(mLayoutManager);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
boolean compactMode = sharedpreferences.getBoolean(Helper.SET_COMPACT_MODE, false);
lv_status.addItemDecoration(new ConversationDecoration(ShowConversationActivity.this, theme, compactMode));
2018-10-25 14:09:41 +02:00
lv_status.setAdapter(statusListAdapter);
2017-07-08 11:32:38 +02:00
if( isRefreshed){
position = statuses.size()-1;
2017-10-14 14:36:48 +02:00
lv_status.scrollToPosition(position);
2017-07-08 11:32:38 +02:00
}else {
lv_status.smoothScrollToPosition(position);
}
2018-10-25 14:09:41 +02:00
2018-09-08 11:30:58 +02:00
statusListAdapter.notifyDataSetChanged();
loader.setVisibility(View.GONE);
lv_status.setVisibility(View.VISIBLE);
2017-05-05 16:36:04 +02:00
}
2017-08-16 18:08:16 +02:00
2017-12-20 13:57:28 +01:00
@Override
public void onRetrieveAccount(Card card) {
int position = 0;
for(Status status: this.statuses) {
if( initialStatus.getId().equals(status.getId())) {
2017-12-22 14:46:27 +01:00
if( card != null) {
this.statuses.get(position).setCard(card);
initialStatus.setCard(card);
statusListAdapter.notifyItemChanged(position);
}
2017-12-20 13:57:28 +01:00
return;
}
position++;
}
}
2017-05-05 16:36:04 +02:00
}