2017-01-28 14:33:43 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-01-28 14:33:43 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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-01-28 14:33:43 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-28 14:33:43 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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-01-28 14:33:43 +11:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
import android.app.AlertDialog;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.content.Context;
|
2017-05-01 11:55:33 +10:00
|
|
|
import android.content.DialogInterface;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2017-05-26 03:10:49 +10:00
|
|
|
import android.graphics.Color;
|
2017-03-03 11:25:35 +11:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.os.Bundle;
|
2017-05-05 08:55:34 +10:00
|
|
|
import android.preference.PreferenceManager;
|
2017-03-07 18:28:12 +11:00
|
|
|
import android.support.annotation.AttrRes;
|
2017-08-09 15:16:00 +10:00
|
|
|
import android.support.annotation.NonNull;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2017-03-07 18:28:12 +11:00
|
|
|
import android.support.design.widget.AppBarLayout;
|
2017-03-07 06:48:31 +11:00
|
|
|
import android.support.design.widget.CollapsingToolbarLayout;
|
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-01-31 15:51:02 +11:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-05-11 19:11:15 +10:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2017-03-07 18:28:12 +11:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-05-24 05:34:31 +10:00
|
|
|
import android.util.Log;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2017-01-31 15:51:02 +11:00
|
|
|
import android.view.View;
|
2017-08-04 18:53:38 +10:00
|
|
|
import android.widget.Button;
|
2017-03-07 06:48:31 +11:00
|
|
|
import android.widget.ImageView;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2017-03-09 09:19:03 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Account;
|
2017-03-09 11:01:45 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Relationship;
|
2017-08-04 19:44:10 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.LinkListener;
|
|
|
|
import com.keylesspalace.tusky.pager.AccountPagerAdapter;
|
2017-05-29 20:14:09 +10:00
|
|
|
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.LinkHelper;
|
|
|
|
import com.keylesspalace.tusky.util.Assert;
|
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-03-07 06:48:31 +11:00
|
|
|
import com.pkmmte.view.CircularImageView;
|
|
|
|
import com.squareup.picasso.Picasso;
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-04-05 06:36:42 +10:00
|
|
|
import java.text.NumberFormat;
|
2017-03-09 11:01:45 +11:00
|
|
|
import java.util.ArrayList;
|
2017-11-02 06:59:29 +11:00
|
|
|
import java.util.Collections;
|
2017-03-09 11:01:45 +11:00
|
|
|
import java.util.List;
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-03-09 09:19:03 +11:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-03-10 03:51:44 +11:00
|
|
|
import retrofit2.Response;
|
2017-03-09 09:19:03 +11:00
|
|
|
|
2017-08-04 19:44:10 +10:00
|
|
|
public class AccountActivity extends BaseActivity implements ActionButtonActivity {
|
2017-03-10 10:20:08 +11:00
|
|
|
private static final String TAG = "AccountActivity"; // logging tag
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
private enum FollowState {
|
|
|
|
NOT_FOLLOWING,
|
|
|
|
FOLLOWING,
|
|
|
|
REQUESTED,
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private String accountId;
|
2017-05-01 11:55:33 +10:00
|
|
|
private FollowState followState;
|
|
|
|
private boolean blocking;
|
|
|
|
private boolean muting;
|
2017-02-20 11:27:15 +11:00
|
|
|
private boolean isSelf;
|
2017-03-11 09:37:02 +11:00
|
|
|
private Account loadedAccount;
|
2017-06-23 04:59:12 +10:00
|
|
|
private CircularImageView avatar;
|
|
|
|
private ImageView header;
|
|
|
|
private FloatingActionButton floatingBtn;
|
2017-08-04 18:53:38 +10:00
|
|
|
private Button followBtn;
|
2017-08-05 18:09:17 +10:00
|
|
|
private TextView followsYouView;
|
2017-06-23 04:59:12 +10:00
|
|
|
private TabLayout tabLayout;
|
|
|
|
private ImageView accountLockedView;
|
|
|
|
private View container;
|
2017-08-04 19:44:10 +10:00
|
|
|
private boolean hideFab;
|
|
|
|
private int oldOffset;
|
2017-03-15 01:27:22 +11:00
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_account);
|
2017-06-23 04:59:12 +10:00
|
|
|
|
2017-08-09 15:16:00 +10:00
|
|
|
avatar = findViewById(R.id.account_avatar);
|
|
|
|
header = findViewById(R.id.account_header);
|
|
|
|
floatingBtn = findViewById(R.id.floating_btn);
|
|
|
|
followBtn = findViewById(R.id.follow_btn);
|
|
|
|
followsYouView = findViewById(R.id.account_follows_you);
|
|
|
|
tabLayout = findViewById(R.id.tab_layout);
|
|
|
|
accountLockedView = findViewById(R.id.account_locked);
|
2017-06-23 04:59:12 +10:00
|
|
|
container = findViewById(R.id.activity_account);
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-04-05 06:36:42 +10:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
accountId = savedInstanceState.getString("accountId");
|
2017-05-01 11:55:33 +10:00
|
|
|
followState = (FollowState) savedInstanceState.getSerializable("followState");
|
|
|
|
blocking = savedInstanceState.getBoolean("blocking");
|
|
|
|
muting = savedInstanceState.getBoolean("muting");
|
2017-04-05 06:36:42 +10:00
|
|
|
} else {
|
|
|
|
Intent intent = getIntent();
|
|
|
|
accountId = intent.getStringExtra("id");
|
2017-05-01 11:55:33 +10:00
|
|
|
followState = FollowState.NOT_FOLLOWING;
|
|
|
|
blocking = false;
|
|
|
|
muting = false;
|
2017-04-05 06:36:42 +10:00
|
|
|
}
|
2017-05-01 11:55:33 +10:00
|
|
|
loadedAccount = null;
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-04-22 13:16:05 +10:00
|
|
|
SharedPreferences preferences = getPrivatePreferences();
|
2017-01-31 15:51:02 +11:00
|
|
|
String loggedInAccountId = preferences.getString("loggedInAccountId", null);
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
// Setup the toolbar.
|
2017-08-09 15:16:00 +10:00
|
|
|
final Toolbar toolbar = findViewById(R.id.toolbar);
|
2017-01-28 14:33:43 +11:00
|
|
|
setSupportActionBar(toolbar);
|
2017-03-07 06:48:31 +11:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(null);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:44:10 +10:00
|
|
|
hideFab = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fabHide", false);
|
|
|
|
|
2017-03-07 18:28:12 +11:00
|
|
|
// Add a listener to change the toolbar icon color when it enters/exits its collapsed state.
|
2017-08-09 15:16:00 +10:00
|
|
|
AppBarLayout appBarLayout = findViewById(R.id.account_app_bar_layout);
|
|
|
|
final CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
|
2017-03-07 18:28:12 +11:00
|
|
|
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
|
|
|
|
@AttrRes int priorAttribute = R.attr.account_toolbar_icon_tint_uncollapsed;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
|
|
|
|
@AttrRes int attribute;
|
|
|
|
if (collapsingToolbar.getHeight() + verticalOffset
|
|
|
|
< 2 * ViewCompat.getMinimumHeight(collapsingToolbar)) {
|
2017-05-26 03:10:49 +10:00
|
|
|
|
2017-05-02 13:39:06 +10:00
|
|
|
toolbar.setTitleTextColor(ThemeUtils.getColor(AccountActivity.this,
|
2017-05-02 13:31:31 +10:00
|
|
|
android.R.attr.textColorPrimary));
|
2017-05-02 13:39:06 +10:00
|
|
|
toolbar.setSubtitleTextColor(ThemeUtils.getColor(AccountActivity.this,
|
2017-05-02 13:31:31 +10:00
|
|
|
android.R.attr.textColorSecondary));
|
2017-05-26 03:10:49 +10:00
|
|
|
|
2017-03-07 18:28:12 +11:00
|
|
|
attribute = R.attr.account_toolbar_icon_tint_collapsed;
|
|
|
|
} else {
|
2017-05-26 03:10:49 +10:00
|
|
|
toolbar.setTitleTextColor(Color.TRANSPARENT);
|
|
|
|
toolbar.setSubtitleTextColor(Color.TRANSPARENT);
|
|
|
|
|
2017-03-07 18:28:12 +11:00
|
|
|
attribute = R.attr.account_toolbar_icon_tint_uncollapsed;
|
|
|
|
}
|
|
|
|
if (attribute != priorAttribute) {
|
|
|
|
priorAttribute = attribute;
|
|
|
|
Context context = toolbar.getContext();
|
|
|
|
ThemeUtils.setDrawableTint(context, toolbar.getNavigationIcon(), attribute);
|
|
|
|
ThemeUtils.setDrawableTint(context, toolbar.getOverflowIcon(), attribute);
|
|
|
|
}
|
2017-08-04 19:44:10 +10:00
|
|
|
|
2017-08-05 19:34:50 +10:00
|
|
|
if(floatingBtn != null && hideFab && !isSelf && !blocking) {
|
2017-08-04 19:44:10 +10:00
|
|
|
if (verticalOffset > oldOffset) {
|
|
|
|
floatingBtn.show();
|
|
|
|
}
|
|
|
|
if (verticalOffset < oldOffset) {
|
|
|
|
floatingBtn.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oldOffset = verticalOffset;
|
2017-03-07 18:28:12 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
// Initialise the default UI states.
|
2017-03-07 06:48:31 +11:00
|
|
|
floatingBtn.hide();
|
2017-08-04 18:53:38 +10:00
|
|
|
followBtn.setVisibility(View.GONE);
|
2017-08-05 18:09:17 +10:00
|
|
|
followsYouView.setVisibility(View.GONE);
|
2017-03-07 06:48:31 +11:00
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
// Obtain information to fill out the profile.
|
2017-01-31 15:51:02 +11:00
|
|
|
obtainAccount();
|
|
|
|
if (!accountId.equals(loggedInAccountId)) {
|
2017-02-20 11:27:15 +11:00
|
|
|
isSelf = false;
|
2017-01-31 15:51:02 +11:00
|
|
|
obtainRelationships();
|
|
|
|
} else {
|
|
|
|
/* Cause the options menu to update and instead show an options menu for when the
|
|
|
|
* account being shown is their own account. */
|
|
|
|
isSelf = true;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
|
|
|
// Setup the tabs and timeline pager.
|
2017-02-23 06:13:51 +11:00
|
|
|
AccountPagerAdapter adapter = new AccountPagerAdapter(getSupportFragmentManager(), this,
|
|
|
|
accountId);
|
2017-01-28 14:33:43 +11:00
|
|
|
String[] pageTitles = {
|
2017-01-31 15:51:02 +11:00
|
|
|
getString(R.string.title_statuses),
|
|
|
|
getString(R.string.title_follows),
|
|
|
|
getString(R.string.title_followers)
|
2017-01-28 14:33:43 +11:00
|
|
|
};
|
|
|
|
adapter.setPageTitles(pageTitles);
|
2017-08-09 15:16:00 +10:00
|
|
|
ViewPager viewPager = findViewById(R.id.pager);
|
2017-03-03 11:25:35 +11:00
|
|
|
int pageMargin = getResources().getDimensionPixelSize(R.dimen.tab_page_margin);
|
2017-02-03 11:18:41 +11:00
|
|
|
viewPager.setPageMargin(pageMargin);
|
2017-03-03 11:25:35 +11:00
|
|
|
Drawable pageMarginDrawable = ThemeUtils.getDrawable(this, R.attr.tab_page_margin_drawable,
|
|
|
|
R.drawable.tab_page_margin_dark);
|
|
|
|
viewPager.setPageMarginDrawable(pageMarginDrawable);
|
2017-01-28 14:33:43 +11:00
|
|
|
viewPager.setAdapter(adapter);
|
|
|
|
tabLayout.setupWithViewPager(viewPager);
|
2017-01-31 15:51:02 +11:00
|
|
|
for (int i = 0; i < tabLayout.getTabCount(); i++) {
|
|
|
|
TabLayout.Tab tab = tabLayout.getTabAt(i);
|
2017-02-02 07:20:39 +11:00
|
|
|
if (tab != null) {
|
2017-02-23 06:13:51 +11:00
|
|
|
tab.setCustomView(adapter.getTabView(i, tabLayout));
|
2017-02-02 07:20:39 +11:00
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-04-22 13:16:05 +10:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
outState.putString("accountId", accountId);
|
2017-05-01 11:55:33 +10:00
|
|
|
outState.putSerializable("followState", followState);
|
|
|
|
outState.putBoolean("blocking", blocking);
|
|
|
|
outState.putBoolean("muting", muting);
|
2017-04-22 13:16:05 +10:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void obtainAccount() {
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.account(accountId).enqueue(new Callback<Account>() {
|
2017-01-28 14:33:43 +11:00
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onResponse(@NonNull Call<Account> call,
|
|
|
|
@NonNull Response<Account> response) {
|
2017-03-14 11:49:12 +11:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
onObtainAccountSuccess(response.body());
|
|
|
|
} else {
|
|
|
|
onObtainAccountFailure();
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
2017-03-09 09:19:03 +11:00
|
|
|
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onFailure(@NonNull Call<Account> call, @NonNull Throwable t) {
|
2017-03-09 09:19:03 +11:00
|
|
|
onObtainAccountFailure();
|
|
|
|
}
|
|
|
|
});
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void onObtainAccountSuccess(Account account) {
|
2017-03-11 09:37:02 +11:00
|
|
|
loadedAccount = account;
|
|
|
|
|
2017-08-09 15:16:00 +10:00
|
|
|
TextView username = findViewById(R.id.account_username);
|
|
|
|
TextView displayName = findViewById(R.id.account_display_name);
|
|
|
|
TextView note = findViewById(R.id.account_note);
|
2017-01-28 14:33:43 +11:00
|
|
|
|
|
|
|
String usernameFormatted = String.format(
|
2017-01-31 15:51:02 +11:00
|
|
|
getString(R.string.status_username_format), account.username);
|
2017-01-28 14:33:43 +11:00
|
|
|
username.setText(usernameFormatted);
|
|
|
|
|
2017-03-11 09:31:08 +11:00
|
|
|
displayName.setText(account.getDisplayName());
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-05-26 03:10:49 +10:00
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setTitle(account.getDisplayName());
|
|
|
|
|
|
|
|
String subtitle = String.format(getString(R.string.status_username_format),
|
|
|
|
account.username);
|
|
|
|
getSupportActionBar().setSubtitle(subtitle);
|
|
|
|
}
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
boolean useCustomTabs = PreferenceManager.getDefaultSharedPreferences(this)
|
2017-10-30 20:48:27 +11:00
|
|
|
.getBoolean("customTabs", false);
|
2017-05-05 08:55:34 +10:00
|
|
|
LinkHelper.setClickableText(note, account.note, null, useCustomTabs, new LinkListener() {
|
2017-04-25 21:30:57 +10:00
|
|
|
@Override
|
|
|
|
public void onViewTag(String tag) {
|
|
|
|
Intent intent = new Intent(AccountActivity.this, ViewTagActivity.class);
|
|
|
|
intent.putExtra("hashtag", tag);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewAccount(String id) {
|
|
|
|
Intent intent = new Intent(AccountActivity.this, AccountActivity.class);
|
|
|
|
intent.putExtra("id", id);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-03-15 01:27:22 +11:00
|
|
|
if (account.locked) {
|
|
|
|
accountLockedView.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
accountLockedView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:38:20 +11:00
|
|
|
Picasso.with(this)
|
|
|
|
.load(account.avatar)
|
|
|
|
.placeholder(R.drawable.avatar_default)
|
|
|
|
.into(avatar);
|
|
|
|
Picasso.with(this)
|
|
|
|
.load(account.header)
|
2017-05-05 02:07:59 +10:00
|
|
|
.placeholder(R.drawable.account_header_default)
|
2017-03-08 13:38:20 +11:00
|
|
|
.into(header);
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-04-05 06:36:42 +10:00
|
|
|
NumberFormat nf = NumberFormat.getInstance();
|
2017-01-31 15:51:02 +11:00
|
|
|
|
|
|
|
// Add counts to the tabs in the TabLayout.
|
|
|
|
String[] counts = {
|
2017-03-07 06:48:31 +11:00
|
|
|
nf.format(Integer.parseInt(account.statusesCount)),
|
|
|
|
nf.format(Integer.parseInt(account.followingCount)),
|
|
|
|
nf.format(Integer.parseInt(account.followersCount)),
|
2017-01-31 15:51:02 +11:00
|
|
|
};
|
2017-03-07 06:48:31 +11:00
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
for (int i = 0; i < tabLayout.getTabCount(); i++) {
|
|
|
|
TabLayout.Tab tab = tabLayout.getTabAt(i);
|
|
|
|
if (tab != null) {
|
|
|
|
View view = tab.getCustomView();
|
2017-02-02 07:20:39 +11:00
|
|
|
if (view != null) {
|
2017-08-09 15:16:00 +10:00
|
|
|
TextView total = view.findViewById(R.id.total);
|
2017-02-02 07:20:39 +11:00
|
|
|
total.setText(counts[i]);
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onObtainAccountFailure() {
|
2017-04-06 17:09:49 +10:00
|
|
|
Snackbar.make(tabLayout, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-02-27 11:14:50 +11:00
|
|
|
.setAction(R.string.action_retry, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
obtainAccount();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void obtainRelationships() {
|
2017-03-09 11:01:45 +11:00
|
|
|
List<String> ids = new ArrayList<>(1);
|
|
|
|
ids.add(accountId);
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.relationships(ids).enqueue(new Callback<List<Relationship>>() {
|
2017-01-28 14:33:43 +11:00
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onResponse(@NonNull Call<List<Relationship>> call,
|
|
|
|
@NonNull Response<List<Relationship>> response) {
|
|
|
|
List<Relationship> relationships = response.body();
|
|
|
|
if (response.isSuccessful() && relationships != null) {
|
|
|
|
Relationship relationship = relationships.get(0);
|
2017-08-05 18:09:17 +10:00
|
|
|
onObtainRelationshipsSuccess(relationship);
|
2017-03-14 11:49:12 +11:00
|
|
|
} else {
|
|
|
|
onObtainRelationshipsFailure(new Exception(response.message()));
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
2017-03-09 11:01:45 +11:00
|
|
|
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onFailure(@NonNull Call<List<Relationship>> call, @NonNull Throwable t) {
|
2017-03-09 11:01:45 +11:00
|
|
|
onObtainRelationshipsFailure((Exception) t);
|
|
|
|
}
|
|
|
|
});
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-08-05 18:09:17 +10:00
|
|
|
private void onObtainRelationshipsSuccess(Relationship relation) {
|
|
|
|
if (relation.following) {
|
2017-05-01 11:55:33 +10:00
|
|
|
followState = FollowState.FOLLOWING;
|
2017-08-05 18:09:17 +10:00
|
|
|
} else if (relation.requested) {
|
2017-05-01 11:55:33 +10:00
|
|
|
followState = FollowState.REQUESTED;
|
|
|
|
} else {
|
|
|
|
followState = FollowState.NOT_FOLLOWING;
|
|
|
|
}
|
2017-08-05 18:09:17 +10:00
|
|
|
this.blocking = relation.blocking;
|
|
|
|
this.muting = relation.muting;
|
2017-03-07 06:48:31 +11:00
|
|
|
|
2017-08-09 15:16:00 +10:00
|
|
|
if (relation.followedBy) {
|
2017-08-05 18:09:17 +10:00
|
|
|
followsYouView.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
followsYouView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2017-03-07 06:48:31 +11:00
|
|
|
updateButtons();
|
|
|
|
}
|
|
|
|
|
2017-08-04 18:53:38 +10:00
|
|
|
private void updateFollowButton(Button button) {
|
2017-05-01 11:55:33 +10:00
|
|
|
switch (followState) {
|
|
|
|
case NOT_FOLLOWING: {
|
2017-08-04 18:53:38 +10:00
|
|
|
button.setText(getString(R.string.action_follow));
|
2017-05-01 11:55:33 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REQUESTED: {
|
2017-08-04 18:53:38 +10:00
|
|
|
button.setText(getString(R.string.state_follow_requested));
|
2017-05-01 11:55:33 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FOLLOWING: {
|
2017-08-04 18:53:38 +10:00
|
|
|
button.setText(getString(R.string.action_unfollow));
|
2017-05-01 11:55:33 +10:00
|
|
|
break;
|
|
|
|
}
|
2017-04-12 14:21:52 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 06:48:31 +11:00
|
|
|
private void updateButtons() {
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
|
2017-04-07 01:07:40 +10:00
|
|
|
if(!isSelf && !blocking) {
|
2017-03-07 06:48:31 +11:00
|
|
|
floatingBtn.show();
|
2017-08-04 18:53:38 +10:00
|
|
|
followBtn.setVisibility(View.VISIBLE);
|
2017-04-07 01:07:40 +10:00
|
|
|
|
2017-08-04 18:53:38 +10:00
|
|
|
updateFollowButton(followBtn);
|
2017-04-07 01:07:40 +10:00
|
|
|
|
2017-03-07 06:48:31 +11:00
|
|
|
floatingBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-08-04 18:53:38 +10:00
|
|
|
mention();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
followBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-08-09 15:16:00 +10:00
|
|
|
switch (followState) {
|
|
|
|
case NOT_FOLLOWING: {
|
|
|
|
follow(accountId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REQUESTED: {
|
|
|
|
showFollowRequestPendingDialog();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FOLLOWING: {
|
|
|
|
showUnfollowWarningDialog();
|
|
|
|
break;
|
|
|
|
}
|
2017-05-01 11:55:33 +10:00
|
|
|
}
|
2017-08-04 18:53:38 +10:00
|
|
|
updateFollowButton(followBtn);
|
2017-03-07 06:48:31 +11:00
|
|
|
}
|
|
|
|
});
|
2017-08-05 19:34:50 +10:00
|
|
|
} else {
|
|
|
|
floatingBtn.hide();
|
|
|
|
followBtn.setVisibility(View.GONE);
|
2017-03-07 06:48:31 +11:00
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-02-27 11:14:50 +11:00
|
|
|
private void onObtainRelationshipsFailure(Exception exception) {
|
|
|
|
Log.e(TAG, "Could not obtain relationships. " + exception.getMessage());
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.account_toolbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
private String getFollowAction() {
|
|
|
|
switch (followState) {
|
|
|
|
default:
|
|
|
|
case NOT_FOLLOWING: return getString(R.string.action_follow);
|
|
|
|
case REQUESTED:
|
|
|
|
case FOLLOWING: return getString(R.string.action_unfollow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
2017-01-31 15:51:02 +11:00
|
|
|
if (!isSelf) {
|
|
|
|
MenuItem follow = menu.findItem(R.id.action_follow);
|
2017-05-01 11:55:33 +10:00
|
|
|
follow.setTitle(getFollowAction());
|
|
|
|
follow.setVisible(followState != FollowState.REQUESTED);
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
MenuItem block = menu.findItem(R.id.action_block);
|
2017-05-01 11:55:33 +10:00
|
|
|
String title;
|
2017-01-31 15:51:02 +11:00
|
|
|
if (blocking) {
|
|
|
|
title = getString(R.string.action_unblock);
|
|
|
|
} else {
|
|
|
|
title = getString(R.string.action_block);
|
|
|
|
}
|
|
|
|
block.setTitle(title);
|
2017-03-10 03:51:44 +11:00
|
|
|
MenuItem mute = menu.findItem(R.id.action_mute);
|
|
|
|
if (muting) {
|
|
|
|
title = getString(R.string.action_unmute);
|
|
|
|
} else {
|
|
|
|
title = getString(R.string.action_mute);
|
|
|
|
}
|
|
|
|
mute.setTitle(title);
|
2017-01-28 14:33:43 +11:00
|
|
|
} else {
|
2017-01-31 15:51:02 +11:00
|
|
|
// It shouldn't be possible to block or follow yourself.
|
|
|
|
menu.removeItem(R.id.action_follow);
|
|
|
|
menu.removeItem(R.id.action_block);
|
2017-03-10 03:51:44 +11:00
|
|
|
menu.removeItem(R.id.action_mute);
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
return super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
2017-03-09 11:01:45 +11:00
|
|
|
private void follow(final String id) {
|
|
|
|
Callback<Relationship> cb = new Callback<Relationship>() {
|
2017-01-31 15:51:02 +11:00
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onResponse(@NonNull Call<Relationship> call,
|
|
|
|
@NonNull Response<Relationship> response) {
|
|
|
|
Relationship relationship = response.body();
|
|
|
|
if (response.isSuccessful() && relationship != null) {
|
2017-05-01 11:55:33 +10:00
|
|
|
if (relationship.following) {
|
|
|
|
followState = FollowState.FOLLOWING;
|
|
|
|
} else if (relationship.requested) {
|
|
|
|
followState = FollowState.REQUESTED;
|
|
|
|
Snackbar.make(container, R.string.state_follow_requested,
|
|
|
|
Snackbar.LENGTH_LONG).show();
|
|
|
|
} else {
|
|
|
|
followState = FollowState.NOT_FOLLOWING;
|
2017-05-11 19:11:15 +10:00
|
|
|
broadcast(TimelineReceiver.Types.UNFOLLOW_ACCOUNT, id);
|
2017-05-01 11:55:33 +10:00
|
|
|
}
|
2017-03-14 11:49:12 +11:00
|
|
|
updateButtons();
|
|
|
|
} else {
|
|
|
|
onFollowFailure(id);
|
|
|
|
}
|
2017-03-09 11:01:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onFailure(@NonNull Call<Relationship> call, @NonNull Throwable t) {
|
2017-03-09 11:01:45 +11:00
|
|
|
onFollowFailure(id);
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-01 11:55:33 +10:00
|
|
|
Assert.expect(followState != FollowState.REQUESTED);
|
|
|
|
switch (followState) {
|
2017-06-23 04:01:25 +10:00
|
|
|
case NOT_FOLLOWING: { mastodonApi.followAccount(id).enqueue(cb); break; }
|
|
|
|
case FOLLOWING: { mastodonApi.unfollowAccount(id).enqueue(cb); break; }
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void onFollowFailure(final String id) {
|
|
|
|
View.OnClickListener listener = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
follow(id);
|
|
|
|
}
|
|
|
|
};
|
2017-05-01 11:55:33 +10:00
|
|
|
Snackbar.make(container, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-01-31 15:51:02 +11:00
|
|
|
.setAction(R.string.action_retry, listener)
|
|
|
|
.show();
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-08-09 15:16:00 +10:00
|
|
|
private void showFollowRequestPendingDialog() {
|
2017-05-01 11:55:33 +10:00
|
|
|
DialogInterface.OnClickListener waitListener = new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
};
|
2017-08-09 15:16:00 +10:00
|
|
|
new AlertDialog.Builder(this)
|
2017-05-01 11:55:33 +10:00
|
|
|
.setMessage(R.string.dialog_message_follow_request)
|
2017-08-09 15:16:00 +10:00
|
|
|
.setPositiveButton(android.R.string.ok, waitListener)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showUnfollowWarningDialog() {
|
|
|
|
DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
dialogInterface.dismiss();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DialogInterface.OnClickListener unfollowListener = new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
follow(accountId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setMessage(R.string.dialog_unfollow_warning)
|
|
|
|
.setPositiveButton(android.R.string.ok, unfollowListener)
|
|
|
|
.setNegativeButton(android.R.string.cancel, cancelListener)
|
2017-05-01 11:55:33 +10:00
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void block(final String id) {
|
2017-03-09 11:01:45 +11:00
|
|
|
Callback<Relationship> cb = new Callback<Relationship>() {
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onResponse(@NonNull Call<Relationship> call,
|
|
|
|
@NonNull Response<Relationship> response) {
|
|
|
|
Relationship relationship = response.body();
|
|
|
|
if (response.isSuccessful() && relationship != null) {
|
2017-05-11 19:11:15 +10:00
|
|
|
broadcast(TimelineReceiver.Types.BLOCK_ACCOUNT, id);
|
2017-08-09 15:16:00 +10:00
|
|
|
blocking = relationship.blocking;
|
2017-03-14 11:49:12 +11:00
|
|
|
updateButtons();
|
|
|
|
} else {
|
|
|
|
onBlockFailure(id);
|
|
|
|
}
|
2017-03-09 11:01:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onFailure(@NonNull Call<Relationship> call, @NonNull Throwable t) {
|
2017-03-09 11:01:45 +11:00
|
|
|
onBlockFailure(id);
|
|
|
|
}
|
|
|
|
};
|
2017-01-31 15:51:02 +11:00
|
|
|
if (blocking) {
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.unblockAccount(id).enqueue(cb);
|
2017-01-31 15:51:02 +11:00
|
|
|
} else {
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.blockAccount(id).enqueue(cb);
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void onBlockFailure(final String id) {
|
|
|
|
View.OnClickListener listener = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
block(id);
|
|
|
|
}
|
|
|
|
};
|
2017-05-01 11:55:33 +10:00
|
|
|
Snackbar.make(container, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-01-31 15:51:02 +11:00
|
|
|
.setAction(R.string.action_retry, listener)
|
|
|
|
.show();
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-03-10 03:51:44 +11:00
|
|
|
private void mute(final String id) {
|
|
|
|
Callback<Relationship> cb = new Callback<Relationship>() {
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onResponse(@NonNull Call<Relationship> call,
|
|
|
|
@NonNull Response<Relationship> response) {
|
|
|
|
Relationship relationship = response.body();
|
|
|
|
if (response.isSuccessful() && relationship != null) {
|
2017-05-11 19:11:15 +10:00
|
|
|
broadcast(TimelineReceiver.Types.MUTE_ACCOUNT, id);
|
2017-08-09 15:16:00 +10:00
|
|
|
muting = relationship.muting;
|
2017-03-14 11:49:12 +11:00
|
|
|
updateButtons();
|
|
|
|
} else {
|
|
|
|
onMuteFailure(id);
|
|
|
|
}
|
2017-03-10 03:51:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-09 15:16:00 +10:00
|
|
|
public void onFailure(@NonNull Call<Relationship> call, @NonNull Throwable t) {
|
2017-03-10 03:51:44 +11:00
|
|
|
onMuteFailure(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (muting) {
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.unmuteAccount(id).enqueue(cb);
|
2017-03-10 03:51:44 +11:00
|
|
|
} else {
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi.muteAccount(id).enqueue(cb);
|
2017-03-10 03:51:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMuteFailure(final String id) {
|
|
|
|
View.OnClickListener listener = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mute(id);
|
|
|
|
}
|
|
|
|
};
|
2017-05-01 11:55:33 +10:00
|
|
|
Snackbar.make(container, R.string.error_generic, Snackbar.LENGTH_LONG)
|
2017-03-10 03:51:44 +11:00
|
|
|
.setAction(R.string.action_retry, listener)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
2017-08-04 18:53:38 +10:00
|
|
|
private boolean mention() {
|
|
|
|
if (loadedAccount == null) {
|
|
|
|
// If the account isn't loaded yet, eat the input.
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-02 06:59:29 +11:00
|
|
|
Intent intent = new ComposeActivity.IntentBuilder()
|
|
|
|
.mentionedUsernames(Collections.singleton(loadedAccount.username))
|
|
|
|
.build(this);
|
|
|
|
startActivity(intent);
|
2017-08-04 18:53:38 +10:00
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-11 19:11:15 +10:00
|
|
|
private void broadcast(String action, String id) {
|
|
|
|
Intent intent = new Intent(action);
|
|
|
|
intent.putExtra("id", id);
|
|
|
|
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
|
|
|
|
}
|
2017-03-10 03:51:44 +11:00
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2017-03-07 06:48:31 +11:00
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
2017-01-28 14:33:43 +11:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-11 09:37:02 +11:00
|
|
|
case R.id.action_mention: {
|
2017-08-04 18:53:38 +10:00
|
|
|
return mention();
|
2017-03-11 09:37:02 +11:00
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
case R.id.action_open_in_web: {
|
2017-03-20 13:38:39 +11:00
|
|
|
if (loadedAccount == null) {
|
2017-03-28 13:13:55 +11:00
|
|
|
// If the account isn't loaded yet, eat the input.
|
2017-03-20 13:38:39 +11:00
|
|
|
return false;
|
|
|
|
}
|
2017-10-29 06:59:33 +11:00
|
|
|
LinkHelper.openLink(loadedAccount.url, this);
|
2017-01-31 15:51:02 +11:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
case R.id.action_follow: {
|
2017-01-31 15:51:02 +11:00
|
|
|
follow(accountId);
|
2017-01-28 14:33:43 +11:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_block: {
|
2017-01-31 15:51:02 +11:00
|
|
|
block(accountId);
|
2017-01-28 14:33:43 +11:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-10 03:51:44 +11:00
|
|
|
case R.id.action_mute: {
|
|
|
|
mute(accountId);
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2017-08-04 19:44:10 +10:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public FloatingActionButton getActionButton() {
|
2017-08-09 15:16:00 +10:00
|
|
|
if (!isSelf && !blocking) {
|
2017-08-05 19:34:50 +10:00
|
|
|
return floatingBtn;
|
|
|
|
}
|
|
|
|
return null;
|
2017-08-04 19:44:10 +10:00
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|