2017-01-28 14:33:43 +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;
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-01-31 15:51:02 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.support.v4.app.Fragment;
|
2017-01-31 15:51:02 +11:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-01-28 14:33:43 +11:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-03-09 09:19:03 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Account;
|
2017-03-10 02:59:18 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Relationship;
|
2017-01-31 15:51:02 +11:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-09 09:19:03 +11:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
2017-03-11 07:12:40 +11:00
|
|
|
public class AccountFragment extends Fragment implements AccountActionListener {
|
2017-03-10 10:20:08 +11:00
|
|
|
private static final String TAG = "Account"; // logging tag
|
2017-02-07 18:05:50 +11:00
|
|
|
|
2017-03-14 22:45:19 +11:00
|
|
|
private Call<List<Account>> listCall;
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
public enum Type {
|
|
|
|
FOLLOWS,
|
|
|
|
FOLLOWERS,
|
2017-02-22 09:55:37 +11:00
|
|
|
BLOCKS,
|
2017-03-14 22:45:19 +11:00
|
|
|
MUTES,
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private Type type;
|
|
|
|
private String accountId;
|
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private EndlessOnScrollListener scrollListener;
|
|
|
|
private AccountAdapter adapter;
|
|
|
|
private TabLayout.OnTabSelectedListener onTabSelectedListener;
|
2017-03-10 02:59:18 +11:00
|
|
|
private MastodonAPI api;
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
public static AccountFragment newInstance(Type type) {
|
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
AccountFragment fragment = new AccountFragment();
|
|
|
|
arguments.putString("type", type.name());
|
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
public static AccountFragment newInstance(Type type, String accountId) {
|
2017-01-28 14:33:43 +11:00
|
|
|
Bundle arguments = new Bundle();
|
|
|
|
AccountFragment fragment = new AccountFragment();
|
|
|
|
arguments.putString("type", type.name());
|
2017-01-31 15:51:02 +11:00
|
|
|
arguments.putString("accountId", accountId);
|
2017-01-28 14:33:43 +11:00
|
|
|
fragment.setArguments(arguments);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Bundle arguments = getArguments();
|
|
|
|
type = Type.valueOf(arguments.getString("type"));
|
|
|
|
accountId = arguments.getString("accountId");
|
2017-03-10 02:59:18 +11:00
|
|
|
api = ((BaseActivity) getActivity()).mastodonAPI;
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
|
|
|
@Nullable Bundle savedInstanceState) {
|
2017-01-31 15:51:02 +11:00
|
|
|
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_account, container, false);
|
|
|
|
|
|
|
|
Context context = getContext();
|
|
|
|
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
|
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
layoutManager = new LinearLayoutManager(context);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
context, layoutManager.getOrientation());
|
2017-02-22 09:55:37 +11:00
|
|
|
Drawable drawable = ThemeUtils.getDrawable(context, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
2017-01-31 15:51:02 +11:00
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
|
|
|
scrollListener = new EndlessOnScrollListener(layoutManager) {
|
|
|
|
@Override
|
|
|
|
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
|
|
|
|
AccountAdapter adapter = (AccountAdapter) view.getAdapter();
|
|
|
|
Account account = adapter.getItem(adapter.getItemCount() - 2);
|
|
|
|
if (account != null) {
|
2017-03-11 07:12:40 +11:00
|
|
|
fetchAccounts(account.id, null);
|
2017-01-31 15:51:02 +11:00
|
|
|
} else {
|
|
|
|
fetchAccounts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
recyclerView.addOnScrollListener(scrollListener);
|
2017-02-22 13:12:49 +11:00
|
|
|
if (type == Type.BLOCKS) {
|
2017-03-11 07:12:40 +11:00
|
|
|
adapter = new BlocksAdapter(this);
|
2017-02-22 13:12:49 +11:00
|
|
|
} else {
|
2017-03-11 07:12:40 +11:00
|
|
|
adapter = new FollowAdapter(this);
|
2017-02-22 13:12:49 +11:00
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
if (jumpToTopAllowed()) {
|
|
|
|
TabLayout layout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
@Override
|
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
jumpToTop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
layout.addOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
|
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:45:19 +11:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
if (listCall != null) listCall.cancel();
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
2017-02-22 09:55:37 +11:00
|
|
|
if (jumpToTopAllowed()) {
|
|
|
|
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2017-03-11 07:12:40 +11:00
|
|
|
private void fetchAccounts(final String fromId, String uptoId) {
|
2017-03-09 09:19:03 +11:00
|
|
|
Callback<List<Account>> cb = new Callback<List<Account>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<List<Account>> call, retrofit2.Response<List<Account>> response) {
|
|
|
|
onFetchAccountsSuccess(response.body(), fromId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<List<Account>> call, Throwable t) {
|
|
|
|
onFetchAccountsFailure((Exception) t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
case FOLLOWS: {
|
2017-03-14 22:45:19 +11:00
|
|
|
listCall = api.accountFollowing(accountId, fromId, uptoId, null);
|
|
|
|
listCall.enqueue(cb);
|
2017-01-31 15:51:02 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FOLLOWERS: {
|
2017-03-14 22:45:19 +11:00
|
|
|
listCall = api.accountFollowers(accountId, fromId, uptoId, null);
|
|
|
|
listCall.enqueue(cb);
|
2017-02-22 09:55:37 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLOCKS: {
|
2017-03-14 22:45:19 +11:00
|
|
|
listCall = api.blocks(fromId, uptoId, null);
|
|
|
|
listCall.enqueue(cb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MUTES: {
|
|
|
|
listCall = api.mutes(fromId, uptoId, null);
|
|
|
|
listCall.enqueue(cb);
|
2017-01-31 15:51:02 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchAccounts() {
|
2017-03-11 07:12:40 +11:00
|
|
|
fetchAccounts(null, null);
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
private static boolean findAccount(List<Account> accounts, String id) {
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.id.equals(id)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
2017-02-22 09:55:37 +11:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchAccountsSuccess(List<Account> accounts, String fromId) {
|
|
|
|
if (fromId != null) {
|
|
|
|
if (accounts.size() > 0 && !findAccount(accounts, fromId)) {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.LOADING);
|
|
|
|
adapter.addItems(accounts);
|
|
|
|
} else {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.END_OF_TIMELINE);
|
|
|
|
}
|
2017-02-08 08:47:05 +11:00
|
|
|
} else {
|
2017-03-02 08:01:49 +11:00
|
|
|
if (accounts.size() > 0) {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.LOADING);
|
|
|
|
adapter.update(accounts);
|
|
|
|
} else {
|
|
|
|
setFetchTimelineState(FooterViewHolder.State.END_OF_TIMELINE);
|
|
|
|
}
|
2017-02-08 08:47:05 +11:00
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:05:50 +11:00
|
|
|
private void onFetchAccountsFailure(Exception exception) {
|
2017-02-08 08:47:05 +11:00
|
|
|
setFetchTimelineState(FooterViewHolder.State.RETRY);
|
2017-02-07 18:05:50 +11:00
|
|
|
Log.e(TAG, "Fetch failure: " + exception.getMessage());
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
|
2017-02-08 08:47:05 +11:00
|
|
|
private void setFetchTimelineState(FooterViewHolder.State state) {
|
2017-02-22 09:55:37 +11:00
|
|
|
// Set the adapter to set its state when it's bound, if the current Footer is offscreen.
|
|
|
|
adapter.setFooterState(state);
|
|
|
|
// Check if it's onscreen, and update it directly if it is.
|
2017-01-31 15:51:02 +11:00
|
|
|
RecyclerView.ViewHolder viewHolder =
|
|
|
|
recyclerView.findViewHolderForAdapterPosition(adapter.getItemCount() - 1);
|
|
|
|
if (viewHolder != null) {
|
|
|
|
FooterViewHolder holder = (FooterViewHolder) viewHolder;
|
2017-02-08 08:47:05 +11:00
|
|
|
holder.setState(state);
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewAccount(String id) {
|
|
|
|
Intent intent = new Intent(getContext(), AccountActivity.class);
|
|
|
|
intent.putExtra("id", id);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
2017-02-22 13:12:49 +11:00
|
|
|
public void onBlock(final boolean block, final String id, final int position) {
|
2017-03-10 02:59:18 +11:00
|
|
|
Callback<Relationship> cb = new Callback<Relationship>() {
|
2017-02-22 13:12:49 +11:00
|
|
|
@Override
|
2017-03-10 02:59:18 +11:00
|
|
|
public void onResponse(Call<Relationship> call, retrofit2.Response<Relationship> response) {
|
|
|
|
onBlockSuccess(block, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Relationship> call, Throwable t) {
|
|
|
|
onBlockFailure(block, id);
|
2017-02-22 13:12:49 +11:00
|
|
|
}
|
|
|
|
};
|
2017-03-10 02:59:18 +11:00
|
|
|
|
|
|
|
if (!block) {
|
|
|
|
api.unblockAccount(id).enqueue(cb);
|
|
|
|
} else {
|
|
|
|
api.blockAccount(id).enqueue(cb);
|
|
|
|
}
|
2017-02-22 13:12:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onBlockSuccess(boolean blocked, int position) {
|
|
|
|
BlocksAdapter blocksAdapter = (BlocksAdapter) adapter;
|
|
|
|
blocksAdapter.setBlocked(blocked, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onBlockFailure(boolean block, String id) {
|
|
|
|
String verb;
|
|
|
|
if (block) {
|
|
|
|
verb = "block";
|
|
|
|
} else {
|
|
|
|
verb = "unblock";
|
|
|
|
}
|
|
|
|
Log.e(TAG, String.format("Failed to %s account id %s", verb, id));
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:55:37 +11:00
|
|
|
private boolean jumpToTopAllowed() {
|
|
|
|
return type != Type.BLOCKS;
|
|
|
|
}
|
|
|
|
|
2017-01-31 15:51:02 +11:00
|
|
|
private void jumpToTop() {
|
|
|
|
layoutManager.scrollToPositionWithOffset(0, 0);
|
|
|
|
scrollListener.reset();
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
}
|