2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.drawers;
|
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>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-09-26 19:18:54 +02:00
|
|
|
import android.content.SharedPreferences;
|
2018-09-26 19:07:22 +02:00
|
|
|
import android.os.AsyncTask;
|
2018-09-26 18:30:08 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
2018-09-26 19:18:54 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2018-09-26 18:30:08 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.asynctasks.DeleteDomainsAsyncTask;
|
2019-11-15 16:32:25 +01:00
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.interfaces.OnRetrieveDomainsInterface;
|
2019-11-15 16:32:25 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2018-09-26 19:18:54 +02:00
|
|
|
|
2018-09-26 18:30:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 26/09/2018.
|
|
|
|
* Adapter for domains
|
|
|
|
*/
|
2020-06-16 18:23:13 +02:00
|
|
|
public class DomainsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnRetrieveDomainsInterface {
|
2018-09-26 18:30:08 +02:00
|
|
|
|
|
|
|
private List<String> domains;
|
|
|
|
private Context context;
|
|
|
|
private DomainsListAdapter domainsListAdapter;
|
|
|
|
private RelativeLayout textviewNoAction;
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public DomainsListAdapter(List<String> domains, RelativeLayout textviewNoAction) {
|
2018-09-26 18:30:08 +02:00
|
|
|
this.domains = domains;
|
|
|
|
this.domainsListAdapter = this;
|
|
|
|
this.textviewNoAction = textviewNoAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2019-08-19 09:44:15 +02:00
|
|
|
context = parent.getContext();
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(context);
|
2018-09-26 18:30:08 +02:00
|
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_domain, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
final DomainsListAdapter.ViewHolder holder = (DomainsListAdapter.ViewHolder) viewHolder;
|
|
|
|
final String domain = domains.get(position);
|
2018-09-26 19:18:54 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
|
|
|
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (theme == Helper.THEME_DARK) {
|
2019-05-18 11:10:30 +02:00
|
|
|
Helper.changeDrawableColor(context, holder.domain_delete, R.color.dark_text);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2019-05-18 11:10:30 +02:00
|
|
|
Helper.changeDrawableColor(context, holder.domain_delete, R.color.black);
|
2018-09-26 19:18:54 +02:00
|
|
|
}
|
2018-09-26 18:30:08 +02:00
|
|
|
holder.domain_name.setText(domain);
|
2020-03-07 14:16:28 +01:00
|
|
|
holder.domain_delete.setOnClickListener(v -> {
|
|
|
|
SharedPreferences sharedpreferences1 = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
int theme1 = sharedpreferences1.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
|
|
|
int style;
|
|
|
|
if (theme1 == Helper.THEME_DARK) {
|
|
|
|
style = R.style.DialogDark;
|
|
|
|
} else if (theme1 == Helper.THEME_BLACK) {
|
|
|
|
style = R.style.DialogBlack;
|
|
|
|
} else {
|
|
|
|
style = R.style.Dialog;
|
2018-09-26 18:30:08 +02:00
|
|
|
}
|
2020-03-07 14:16:28 +01:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
|
|
|
|
builder.setMessage(context.getString(R.string.unblock_domain_confirm_message, domain));
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_alert)
|
|
|
|
.setTitle(R.string.unblock_domain_confirm)
|
|
|
|
.setPositiveButton(R.string.yes, (dialog, which) -> {
|
|
|
|
domains.remove(domain);
|
|
|
|
domainsListAdapter.notifyItemRemoved(holder.getAdapterPosition());
|
|
|
|
new DeleteDomainsAsyncTask(context, domain, DomainsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
if (domains.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
|
|
|
dialog.dismiss();
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
|
|
|
|
.show();
|
2018-09-26 18:30:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return domains.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRetrieveDomains(APIResponse apiResponse) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-26 19:07:22 +02:00
|
|
|
@Override
|
|
|
|
public void onRetrieveDomainsDeleted(int response) {
|
2018-09-26 19:18:54 +02:00
|
|
|
String message;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (response == 200) {
|
2018-09-26 19:18:54 +02:00
|
|
|
message = context.getString(R.string.toast_unblock_domain);
|
2018-11-25 10:45:16 +01:00
|
|
|
Toasty.success(context, message, Toast.LENGTH_LONG).show();
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-09-26 19:18:54 +02:00
|
|
|
message = context.getString(R.string.toast_error);
|
2018-11-25 10:45:16 +01:00
|
|
|
Toasty.error(context, message, Toast.LENGTH_LONG).show();
|
2018-09-26 19:18:54 +02:00
|
|
|
}
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2018-09-26 19:07:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:16:28 +01:00
|
|
|
private static class ViewHolder extends RecyclerView.ViewHolder {
|
2018-09-26 18:30:08 +02:00
|
|
|
TextView domain_name;
|
|
|
|
ImageView domain_delete;
|
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
domain_name = itemView.findViewById(R.id.domain_name);
|
|
|
|
domain_delete = itemView.findViewById(R.id.domain_delete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|