2017-01-23 16:19:30 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v7.widget.PopupMenu;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-02-27 16:21:46 +11:00
|
|
|
import android.text.Spanned;
|
2017-01-23 16:19:30 +11:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
|
2017-03-10 02:59:18 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Relationship;
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-10 02:59:18 +11:00
|
|
|
import okhttp3.ResponseBody;
|
2017-03-09 10:46:13 +11:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
/* Note from Andrew on Jan. 22, 2017: This class is a design problem for me, so I left it with an
|
|
|
|
* awkward name. TimelineFragment and NotificationFragment have significant overlap but the nature
|
|
|
|
* of that is complicated by how they're coupled with Status and Notification and the corresponding
|
|
|
|
* adapters. I feel like the profile pages and thread viewer, which I haven't made yet, will also
|
|
|
|
* overlap functionality. So, I'm momentarily leaving it and hopefully working on those will clear
|
|
|
|
* up what needs to be where. */
|
|
|
|
public class SFragment extends Fragment {
|
|
|
|
protected String loggedInAccountId;
|
|
|
|
protected String loggedInUsername;
|
2017-03-09 10:46:13 +11:00
|
|
|
private MastodonAPI api;
|
2017-01-23 16:19:30 +11:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
SharedPreferences preferences = getContext().getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
2017-01-28 14:33:43 +11:00
|
|
|
loggedInAccountId = preferences.getString("loggedInAccountId", null);
|
|
|
|
loggedInUsername = preferences.getString("loggedInAccountUsername", null);
|
2017-03-09 10:46:13 +11:00
|
|
|
api = ((BaseActivity) getActivity()).mastodonAPI;
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void reply(Status status) {
|
2017-03-09 10:27:37 +11:00
|
|
|
String inReplyToId = status.getActionableId();
|
|
|
|
Status.Mention[] mentions = status.mentions;
|
2017-01-23 16:19:30 +11:00
|
|
|
List<String> mentionedUsernames = new ArrayList<>();
|
2017-01-31 15:51:02 +11:00
|
|
|
for (Status.Mention mention : mentions) {
|
2017-03-09 10:27:37 +11:00
|
|
|
mentionedUsernames.add(mention.username);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
2017-03-09 10:27:37 +11:00
|
|
|
mentionedUsernames.add(status.account.username);
|
2017-01-23 16:19:30 +11:00
|
|
|
mentionedUsernames.remove(loggedInUsername);
|
|
|
|
Intent intent = new Intent(getContext(), ComposeActivity.class);
|
|
|
|
intent.putExtra("in_reply_to_id", inReplyToId);
|
2017-03-09 10:27:37 +11:00
|
|
|
intent.putExtra("reply_visibility", status.visibility.toString().toLowerCase());
|
2017-01-23 16:19:30 +11:00
|
|
|
intent.putExtra("mentioned_usernames", mentionedUsernames.toArray(new String[0]));
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void reblog(final Status status, final boolean reblog,
|
|
|
|
final RecyclerView.Adapter adapter, final int position) {
|
2017-03-09 10:27:37 +11:00
|
|
|
String id = status.getActionableId();
|
2017-03-09 10:46:13 +11:00
|
|
|
|
|
|
|
Callback<Status> cb = new Callback<Status>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
|
|
|
status.reblogged = reblog;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Status> call, Throwable t) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
if (reblog) {
|
2017-03-09 10:46:13 +11:00
|
|
|
api.reblogStatus(id).enqueue(cb);
|
2017-01-23 16:19:30 +11:00
|
|
|
} else {
|
2017-03-09 10:46:13 +11:00
|
|
|
api.unreblogStatus(id).enqueue(cb);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void favourite(final Status status, final boolean favourite,
|
|
|
|
final RecyclerView.Adapter adapter, final int position) {
|
2017-03-09 10:27:37 +11:00
|
|
|
String id = status.getActionableId();
|
2017-03-09 10:46:13 +11:00
|
|
|
|
|
|
|
Callback<Status> cb = new Callback<Status>() {
|
2017-01-23 16:19:30 +11:00
|
|
|
@Override
|
2017-03-09 10:46:13 +11:00
|
|
|
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
2017-03-09 10:27:37 +11:00
|
|
|
status.favourited = favourite;
|
2017-01-23 16:19:30 +11:00
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}
|
2017-03-09 10:46:13 +11:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Status> call, Throwable t) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (favourite) {
|
|
|
|
api.favouriteStatus(id).enqueue(cb);
|
|
|
|
} else {
|
|
|
|
api.unfavouriteStatus(id).enqueue(cb);
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private void block(String id) {
|
2017-03-10 02:59:18 +11:00
|
|
|
api.blockAccount(id).enqueue(new Callback<Relationship>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<Relationship> call, retrofit2.Response<Relationship> response) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Relationship> call, Throwable t) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private void delete(String id) {
|
2017-03-10 02:59:18 +11:00
|
|
|
api.deleteStatus(id).enqueue(new Callback<ResponseBody>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<ResponseBody> call, Throwable t) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void more(Status status, View view, final AdapterItemRemover adapter,
|
|
|
|
final int position) {
|
2017-03-09 10:27:37 +11:00
|
|
|
final String id = status.getActionableId();
|
|
|
|
final String accountId = status.getActionableStatus().account.id;
|
|
|
|
final String accountUsename = status.getActionableStatus().account.username;
|
|
|
|
final Spanned content = status.getActionableStatus().content;
|
2017-03-10 03:37:24 +11:00
|
|
|
final String statusUrl = status.getActionableStatus().url;
|
2017-01-23 16:19:30 +11:00
|
|
|
PopupMenu popup = new PopupMenu(getContext(), view);
|
|
|
|
// Give a different menu depending on whether this is the user's own toot or not.
|
|
|
|
if (loggedInAccountId == null || !loggedInAccountId.equals(accountId)) {
|
|
|
|
popup.inflate(R.menu.status_more);
|
|
|
|
} else {
|
|
|
|
popup.inflate(R.menu.status_more_for_user);
|
|
|
|
}
|
|
|
|
popup.setOnMenuItemClickListener(
|
|
|
|
new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2017-03-10 03:37:24 +11:00
|
|
|
case R.id.status_share: {
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
|
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, statusUrl);
|
|
|
|
sendIntent.setType("text/plain");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_status_to)));
|
2017-01-23 16:19:30 +11:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_block: {
|
|
|
|
block(accountId);
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-27 16:21:46 +11:00
|
|
|
case R.id.status_report: {
|
|
|
|
openReportPage(accountId, accountUsename, id, content);
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
case R.id.status_delete: {
|
|
|
|
delete(id);
|
|
|
|
adapter.removeItem(position);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popup.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void viewMedia(String url, Status.MediaAttachment.Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case IMAGE: {
|
2017-03-09 10:27:37 +11:00
|
|
|
Fragment newFragment = ViewMediaFragment.newInstance(url);
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
FragmentManager manager = getFragmentManager();
|
|
|
|
manager.beginTransaction()
|
|
|
|
.add(R.id.overlay_fragment_container, newFragment)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 07:21:55 +11:00
|
|
|
case GIFV:
|
2017-01-23 16:19:30 +11:00
|
|
|
case VIDEO: {
|
|
|
|
Intent intent = new Intent(getContext(), ViewVideoActivity.class);
|
|
|
|
intent.putExtra("url", url);
|
|
|
|
startActivity(intent);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-06 11:58:36 +11:00
|
|
|
case UNKNOWN: {
|
|
|
|
/* Intentionally do nothing. This case is here is to handle when new attachment
|
|
|
|
* types are added to the API before code is added here to handle them. So, the
|
|
|
|
* best fallback is to just show the preview and ignore requests to view them. */
|
|
|
|
break;
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void viewThread(Status status) {
|
|
|
|
Intent intent = new Intent(getContext(), ViewThreadActivity.class);
|
2017-03-09 10:27:37 +11:00
|
|
|
intent.putExtra("id", status.id);
|
2017-01-23 16:19:30 +11:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-01-27 11:34:32 +11:00
|
|
|
|
|
|
|
protected void viewTag(String tag) {
|
|
|
|
Intent intent = new Intent(getContext(), ViewTagActivity.class);
|
|
|
|
intent.putExtra("hashtag", tag);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-03-03 11:25:35 +11:00
|
|
|
protected void viewAccount(String id) {
|
2017-01-28 14:33:43 +11:00
|
|
|
Intent intent = new Intent(getContext(), AccountActivity.class);
|
|
|
|
intent.putExtra("id", id);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-02-27 16:21:46 +11:00
|
|
|
|
2017-03-07 23:05:51 +11:00
|
|
|
@Override
|
|
|
|
public void startActivity(Intent intent) {
|
|
|
|
super.startActivity(intent);
|
|
|
|
getActivity().overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
|
|
|
|
}
|
|
|
|
|
2017-03-03 11:25:35 +11:00
|
|
|
protected void openReportPage(String accountId, String accountUsername, String statusId,
|
2017-03-07 23:05:51 +11:00
|
|
|
Spanned statusContent) {
|
2017-02-27 16:21:46 +11:00
|
|
|
Intent intent = new Intent(getContext(), ReportActivity.class);
|
|
|
|
intent.putExtra("account_id", accountId);
|
2017-03-03 11:25:35 +11:00
|
|
|
intent.putExtra("account_username", accountUsername);
|
2017-02-27 16:21:46 +11:00
|
|
|
intent.putExtra("status_id", statusId);
|
|
|
|
intent.putExtra("status_content", HtmlUtils.toHtml(statusContent));
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|