2017-02-27 06:21:46 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This file is a part of Tusky.
|
2017-02-27 06:21:46 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +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-02-27 06:21:46 +01:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 02:12:31 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-02-27 06:21:46 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-02-27 06:21:46 +01:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2017-03-01 03:38:47 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-02-27 06:21:46 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v7.app.ActionBar;
|
2017-03-01 03:38:47 +01:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-02-27 06:21:46 +01:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-05-23 21:34:31 +02:00
|
|
|
import android.util.Log;
|
2017-03-10 23:47:04 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2017-02-27 06:21:46 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.ReportAdapter;
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.di.Injectable;
|
2017-03-10 00:20:08 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.util.HtmlUtils;
|
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-02-27 06:21:46 +01:00
|
|
|
|
2017-03-01 03:38:47 +01:00
|
|
|
import java.util.ArrayList;
|
2017-03-09 16:59:18 +01:00
|
|
|
import java.util.Arrays;
|
2017-03-01 03:38:47 +01:00
|
|
|
import java.util.List;
|
2017-02-27 06:21:46 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2017-03-09 16:59:18 +01:00
|
|
|
import okhttp3.ResponseBody;
|
2017-03-09 00:27:37 +01:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-06-22 20:59:12 +02:00
|
|
|
import retrofit2.Response;
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
public class ReportActivity extends BaseActivity implements Injectable {
|
2017-03-10 00:20:08 +01:00
|
|
|
private static final String TAG = "ReportActivity"; // logging tag
|
2017-02-27 06:21:46 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
@Inject
|
|
|
|
public MastodonApi mastodonApi;
|
|
|
|
|
2017-02-27 06:21:46 +01:00
|
|
|
private View anyView; // what Snackbar will use to find the root view
|
2017-03-01 03:38:47 +01:00
|
|
|
private ReportAdapter adapter;
|
2017-03-02 07:05:02 +01:00
|
|
|
private boolean reportAlreadyInFlight;
|
2017-03-10 23:47:04 +01:00
|
|
|
private String accountId;
|
|
|
|
private EditText comment;
|
2017-02-27 06:21:46 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_report);
|
|
|
|
|
|
|
|
Intent intent = getIntent();
|
2017-03-10 23:47:04 +01:00
|
|
|
accountId = intent.getStringExtra("account_id");
|
2017-03-27 05:22:59 +02:00
|
|
|
String accountUsername = intent.getStringExtra("account_username");
|
|
|
|
String statusId = intent.getStringExtra("status_id");
|
|
|
|
String statusContent = intent.getStringExtra("status_content");
|
2017-02-27 06:21:46 +01:00
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
2017-02-27 06:21:46 +01:00
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
ActionBar bar = getSupportActionBar();
|
|
|
|
if (bar != null) {
|
|
|
|
String title = String.format(getString(R.string.report_username_format),
|
|
|
|
accountUsername);
|
|
|
|
bar.setTitle(title);
|
2017-03-10 23:47:04 +01:00
|
|
|
bar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
bar.setDisplayShowHomeEnabled(true);
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
|
|
|
anyView = toolbar;
|
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
final RecyclerView recyclerView = findViewById(R.id.report_recycler_view);
|
2017-03-01 03:38:47 +01:00
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
adapter = new ReportAdapter();
|
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
this, layoutManager.getOrientation());
|
|
|
|
Drawable drawable = ThemeUtils.getDrawable(this, R.attr.report_status_divider_drawable,
|
|
|
|
R.drawable.report_status_divider_dark);
|
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
|
|
|
|
|
|
|
ReportAdapter.ReportStatus reportStatus = new ReportAdapter.ReportStatus(statusId,
|
|
|
|
HtmlUtils.fromHtml(statusContent), true);
|
|
|
|
adapter.addItem(reportStatus);
|
2017-02-27 06:21:46 +01:00
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
comment = findViewById(R.id.report_comment);
|
2017-03-10 23:47:04 +01:00
|
|
|
|
2017-03-02 07:05:02 +01:00
|
|
|
reportAlreadyInFlight = false;
|
2017-03-01 03:38:47 +01:00
|
|
|
|
|
|
|
fetchRecentStatuses(accountId);
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 23:47:04 +01:00
|
|
|
private void onClickSend() {
|
|
|
|
if (reportAlreadyInFlight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] statusIds = adapter.getCheckedStatusIds();
|
|
|
|
|
|
|
|
if (statusIds.length > 0) {
|
|
|
|
reportAlreadyInFlight = true;
|
|
|
|
sendReport(accountId, statusIds, comment.getText().toString());
|
|
|
|
} else {
|
|
|
|
comment.setError(getString(R.string.error_report_too_few_statuses));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 06:21:46 +01:00
|
|
|
private void sendReport(final String accountId, final String[] statusIds,
|
2018-03-27 19:47:00 +02:00
|
|
|
final String comment) {
|
2017-06-22 20:59:12 +02:00
|
|
|
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
|
2017-02-27 06:21:46 +01:00
|
|
|
@Override
|
2017-06-22 20:59:12 +02:00
|
|
|
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
onSendSuccess();
|
|
|
|
} else {
|
|
|
|
onSendFailure(accountId, statusIds, comment);
|
|
|
|
}
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
2017-03-09 16:59:18 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<ResponseBody> call, Throwable t) {
|
|
|
|
onSendFailure(accountId, statusIds, comment);
|
|
|
|
}
|
2017-06-22 20:59:12 +02:00
|
|
|
};
|
|
|
|
mastodonApi.report(accountId, Arrays.asList(statusIds), comment)
|
|
|
|
.enqueue(callback);
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onSendSuccess() {
|
2017-03-09 16:59:18 +01:00
|
|
|
Snackbar bar = Snackbar.make(anyView, getString(R.string.confirmation_reported), Snackbar.LENGTH_SHORT);
|
|
|
|
bar.show();
|
2017-02-27 06:21:46 +01:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSendFailure(final String accountId, final String[] statusIds,
|
2018-03-27 19:47:00 +02:00
|
|
|
final String comment) {
|
2017-04-06 09:09:49 +02:00
|
|
|
Snackbar.make(anyView, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-02-27 06:21:46 +01:00
|
|
|
.setAction(R.string.action_retry, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
sendReport(accountId, statusIds, comment);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2017-03-02 07:05:02 +01:00
|
|
|
reportAlreadyInFlight = false;
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
2017-03-01 03:38:47 +01:00
|
|
|
|
|
|
|
private void fetchRecentStatuses(String accountId) {
|
2017-06-22 20:59:12 +02:00
|
|
|
Callback<List<Status>> callback = new Callback<List<Status>>() {
|
2017-03-09 00:27:37 +01:00
|
|
|
@Override
|
2017-06-22 20:59:12 +02:00
|
|
|
public void onResponse(Call<List<Status>> call, Response<List<Status>> response) {
|
2017-03-14 01:49:12 +01:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
onFetchStatusesFailure(new Exception(response.message()));
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
List<Status> statusList = response.body();
|
|
|
|
List<ReportAdapter.ReportStatus> itemList = new ArrayList<>();
|
|
|
|
for (Status status : statusList) {
|
2018-03-03 13:24:03 +01:00
|
|
|
if (status.getReblog() == null) {
|
2017-03-09 00:27:37 +01:00
|
|
|
ReportAdapter.ReportStatus item = new ReportAdapter.ReportStatus(
|
2018-03-03 13:24:03 +01:00
|
|
|
status.getId(), status.getContent(), false);
|
2017-03-09 00:27:37 +01:00
|
|
|
itemList.add(item);
|
2017-03-01 03:38:47 +01:00
|
|
|
}
|
2017-03-09 00:27:37 +01:00
|
|
|
}
|
|
|
|
adapter.addItems(itemList);
|
|
|
|
}
|
|
|
|
|
2017-03-01 03:38:47 +01:00
|
|
|
@Override
|
2017-03-09 00:27:37 +01:00
|
|
|
public void onFailure(Call<List<Status>> call, Throwable t) {
|
|
|
|
onFetchStatusesFailure((Exception) t);
|
2017-03-01 03:38:47 +01:00
|
|
|
}
|
2017-06-22 20:59:12 +02:00
|
|
|
};
|
2018-06-18 13:26:18 +02:00
|
|
|
mastodonApi.accountStatuses(accountId, null, null, null, null, null)
|
2017-06-22 20:59:12 +02:00
|
|
|
.enqueue(callback);
|
2017-03-01 03:38:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchStatusesFailure(Exception exception) {
|
|
|
|
Log.e(TAG, "Failed to fetch recent statuses to report. " + exception.getMessage());
|
|
|
|
}
|
2017-03-10 23:47:04 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.report_toolbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_report: {
|
|
|
|
onClickSend();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|