fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayMutedInstanceFragmen...

289 lines
12 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2018-09-26 18:30:08 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-26 18:30:08 +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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-09-26 18:30:08 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-09-26 18:30:08 +02:00
* see <http://www.gnu.org/licenses>. */
2019-06-23 14:43:35 +02:00
import android.annotation.SuppressLint;
2018-09-26 18:30:08 +02:00
import android.content.Context;
2019-06-23 14:43:35 +02:00
import android.content.DialogInterface;
2018-09-26 18:30:08 +02:00
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2019-06-23 14:43:35 +02:00
import androidx.appcompat.app.AlertDialog;
2019-06-11 19:38:26 +02:00
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-06-23 14:43:35 +02:00
2018-09-26 18:30:08 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-06-23 14:43:35 +02:00
import android.view.WindowManager;
import android.widget.EditText;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2018-09-26 18:30:08 +02:00
import android.widget.RelativeLayout;
import android.widget.Toast;
2019-09-06 17:55:14 +02:00
2019-06-23 14:43:35 +02:00
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2019-09-06 17:55:14 +02:00
2018-09-26 18:30:08 +02:00
import java.util.ArrayList;
import java.util.List;
2019-09-06 17:55:14 +02:00
2019-06-23 14:43:35 +02:00
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.client.API;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
2019-06-23 14:43:35 +02:00
import app.fedilab.android.client.Entities.Error;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.drawers.DomainsListAdapter;
import app.fedilab.android.helper.Helper;
2019-06-23 14:43:35 +02:00
import app.fedilab.android.interfaces.OnPostActionInterface;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveDomainsAsyncTask;
import app.fedilab.android.interfaces.OnRetrieveDomainsInterface;
2018-09-26 18:30:08 +02:00
/**
* Created by Thomas on 26/09/2018.
* Fragment to display muted instances
*/
2019-06-23 14:43:35 +02:00
public class DisplayMutedInstanceFragment extends Fragment implements OnRetrieveDomainsInterface, OnPostActionInterface {
2018-09-26 18:30:08 +02:00
private boolean flag_loading;
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
private DomainsListAdapter domainsListAdapter;
private String max_id;
private List<String> domains;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private RecyclerView lv_domains;
2019-06-23 14:43:35 +02:00
private FloatingActionButton add_new;
2018-09-26 18:30:08 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_muted_instances, container, false);
context = getContext();
Bundle bundle = this.getArguments();
domains = new ArrayList<>();
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
lv_domains = rootView.findViewById(R.id.lv_domains);
lv_domains.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
mainLoader = rootView.findViewById(R.id.loader);
nextElementLoader = rootView.findViewById(R.id.loading_next_domains);
textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2019-08-19 09:44:15 +02:00
domainsListAdapter = new DomainsListAdapter(this.domains, textviewNoAction);
2018-09-26 18:30:08 +02:00
lv_domains.setAdapter(domainsListAdapter);
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(context);
lv_domains.setLayoutManager(mLayoutManager);
lv_domains.addOnScrollListener(new RecyclerView.OnScrollListener() {
2019-09-06 17:55:14 +02:00
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
2018-09-26 18:30:08 +02:00
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
flag_loading = true;
2019-09-06 17:55:14 +02:00
asyncTask = new RetrieveDomainsAsyncTask(context, max_id, DisplayMutedInstanceFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
2018-09-26 18:30:08 +02:00
}
} else {
nextElementLoader.setVisibility(View.GONE);
}
}
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
max_id = null;
domains = new ArrayList<>();
firstLoad = true;
flag_loading = true;
swiped = true;
2019-09-06 17:55:14 +02:00
asyncTask = new RetrieveDomainsAsyncTask(context, max_id, DisplayMutedInstanceFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2018-09-26 18:30:08 +02:00
});
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-09-26 18:30:08 +02:00
case Helper.THEME_LIGHT:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.white));
break;
case Helper.THEME_DARK:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4__,
R.color.mastodonC4,
R.color.mastodonC4);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.mastodonC1_));
break;
case Helper.THEME_BLACK:
swipeRefreshLayout.setColorSchemeResources(R.color.dark_icon,
R.color.mastodonC2,
R.color.mastodonC3);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.black_3));
break;
}
asyncTask = new RetrieveDomainsAsyncTask(context, max_id, DisplayMutedInstanceFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-06-23 14:43:35 +02:00
try {
add_new = ((MainActivity) context).findViewById(R.id.add_new);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
if (add_new != null)
2019-06-23 14:43:35 +02:00
add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
2019-09-06 17:55:14 +02:00
} else if (theme == Helper.THEME_BLACK) {
2019-06-23 14:43:35 +02:00
style = R.style.DialogBlack;
2019-09-06 17:55:14 +02:00
} else {
2019-06-23 14:43:35 +02:00
style = R.style.Dialog;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
LayoutInflater inflater = getLayoutInflater();
2019-09-06 17:55:14 +02:00
View dialogView = inflater.inflate(R.layout.add_blocked_instance, new LinearLayout(context), false);
2019-06-23 14:43:35 +02:00
dialogBuilder.setView(dialogView);
EditText add_domain = dialogView.findViewById(R.id.add_domain);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
2019-09-06 17:55:14 +02:00
if (add_domain.getText() != null && add_domain.getText().toString().trim().matches("^[\\da-zA-Z.-]+\\.[a-zA-Z.]{2,10}$")) {
2019-06-23 14:43:35 +02:00
new PostActionAsyncTask(context, API.StatusAction.BLOCK_DOMAIN, add_domain.getText().toString().trim(), DisplayMutedInstanceFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.dismiss();
2019-09-06 17:55:14 +02:00
} else {
2019-06-23 14:43:35 +02:00
Toasty.error(context, context.getString(R.string.toast_empty_content)).show();
}
}
});
2019-11-11 09:34:13 +01:00
dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
2019-06-23 14:43:35 +02:00
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.block_domain));
2019-09-06 17:55:14 +02:00
if (alertDialog.getWindow() != null)
2019-06-23 14:43:35 +02:00
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
}
});
2018-09-26 18:30:08 +02:00
return rootView;
}
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2018-09-26 18:30:08 +02:00
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
2018-09-26 18:30:08 +02:00
asyncTask.cancel(true);
}
2019-09-06 17:55:14 +02:00
public void scrollToTop() {
if (lv_domains != null)
2018-09-26 18:30:08 +02:00
lv_domains.setAdapter(domainsListAdapter);
}
@Override
public void onRetrieveDomains(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null) {
2019-09-30 18:14:46 +02:00
if(apiResponse.getError().getError().length() < 100) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
}else{
Toasty.error(context, getString(R.string.long_api_error,"\ud83d\ude05"), Toast.LENGTH_LONG).show();
}
2018-09-26 18:30:08 +02:00
swipeRefreshLayout.setRefreshing(false);
swiped = false;
flag_loading = false;
return;
}
2019-09-06 17:55:14 +02:00
flag_loading = (apiResponse.getMax_id() == null);
2018-09-26 18:30:08 +02:00
List<String> domains = apiResponse.getDomains();
2019-09-06 17:55:14 +02:00
if (!swiped && firstLoad && (domains == null || domains.size() == 0))
2018-09-26 18:30:08 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
max_id = apiResponse.getMax_id();
2019-09-06 17:55:14 +02:00
if (swiped) {
2019-08-19 09:44:15 +02:00
domainsListAdapter = new DomainsListAdapter(this.domains, textviewNoAction);
2018-09-26 18:30:08 +02:00
lv_domains.setAdapter(domainsListAdapter);
swiped = false;
}
2019-09-06 17:55:14 +02:00
if (domains != null && domains.size() > 0) {
2018-09-26 18:30:08 +02:00
this.domains.addAll(domains);
domainsListAdapter.notifyDataSetChanged();
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
}
2018-09-26 19:07:22 +02:00
@Override
public void onRetrieveDomainsDeleted(int response) {
}
2018-09-26 18:30:08 +02:00
2019-06-23 14:43:35 +02:00
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
2019-09-06 17:55:14 +02:00
if (error != null) {
Toasty.error(context, error.getError(), Toast.LENGTH_LONG).show();
2019-06-23 14:43:35 +02:00
return;
}
Helper.manageMessageStatusCode(context, statusCode, statusAction);
2019-09-06 17:55:14 +02:00
this.domains.add(0, userId);
2019-06-23 14:43:35 +02:00
domainsListAdapter.notifyItemInserted(0);
}
2018-09-26 18:30:08 +02:00
}