fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/DraftsListAdapter.java

186 lines
7.5 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-07-15 14:59:09 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-07-15 14:59:09 +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
2017-07-15 14:59:09 +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,
2017-07-15 14:59:09 +02:00
* see <http://www.gnu.org/licenses>. */
2018-11-28 17:50:14 +01:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2018-11-28 17:50:14 +01:00
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
2017-07-15 14:59:09 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
2017-11-24 15:34:55 +01:00
import android.widget.RelativeLayout;
2017-07-15 14:59:09 +02:00
import android.widget.TextView;
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.AlertDialog;
2017-07-15 14:59:09 +02:00
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
2019-09-28 18:08:07 +02:00
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.PixelfedComposeActivity;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.activities.TootActivity;
2019-09-28 18:08:07 +02:00
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.StoredStatus;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
2017-07-15 14:59:09 +02:00
/**
* Created by Thomas on 15/07/2017.
* Adapter for toot drafts
*/
2019-09-06 17:55:14 +02:00
public class DraftsListAdapter extends BaseAdapter {
2017-07-15 14:59:09 +02:00
2021-01-19 17:43:51 +01:00
private final List<StoredStatus> storedStatuses;
2017-07-15 14:59:09 +02:00
private Context context;
2021-01-19 17:43:51 +01:00
private final DraftsListAdapter draftsListAdapter;
private final boolean clickable;
2017-11-24 15:34:55 +01:00
private RelativeLayout textviewNoAction;
2017-07-15 14:59:09 +02:00
2019-09-06 17:55:14 +02:00
public DraftsListAdapter(List<StoredStatus> storedStatuses) {
2017-07-15 14:59:09 +02:00
this.storedStatuses = storedStatuses;
draftsListAdapter = this;
this.clickable = false;
}
2019-09-06 17:55:14 +02:00
public DraftsListAdapter(List<StoredStatus> storedStatuses, boolean clickable, RelativeLayout textviewNoAction) {
this.storedStatuses = storedStatuses;
draftsListAdapter = this;
this.clickable = clickable;
2017-11-24 15:34:55 +01:00
this.textviewNoAction = textviewNoAction;
2017-07-15 14:59:09 +02:00
}
@Override
public int getCount() {
return storedStatuses.size();
}
@Override
public Object getItem(int position) {
return storedStatuses.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
2019-08-19 09:44:15 +02:00
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
2017-07-15 14:59:09 +02:00
final StoredStatus draft = storedStatuses.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_draft, parent, false);
holder = new ViewHolder();
2017-10-30 07:54:02 +01:00
holder.draft_title = convertView.findViewById(R.id.draft_title);
holder.draft_date = convertView.findViewById(R.id.draft_date);
holder.draft_delete = convertView.findViewById(R.id.draft_delete);
holder.drafts_container = convertView.findViewById(R.id.drafts_container);
2017-07-15 14:59:09 +02:00
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
2017-10-30 07:54:02 +01:00
2020-04-09 18:57:12 +02:00
final SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-09-06 17:55:14 +02:00
if (this.clickable) {
if (draft.getStatus().getContent().length() > 300)
2019-09-06 17:55:14 +02:00
holder.draft_title.setText(String.format("%s…", draft.getStatus().getContent().substring(0, 299)));
2017-07-15 14:59:09 +02:00
else
holder.draft_title.setText(draft.getStatus().getContent());
2019-09-06 17:55:14 +02:00
} else {
if (draft.getStatus() != null && draft.getStatus().getContent() != null) {
if (draft.getStatus().getContent().length() > 20)
holder.draft_title.setText(draft.getStatus().getContent().substring(0, 20));
else
holder.draft_title.setText(draft.getStatus().getContent());
}
holder.draft_title.setTypeface(Typeface.DEFAULT_BOLD);
2017-07-15 14:59:09 +02:00
}
//Manages theme for icon colors
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 || theme == Helper.THEME_BLACK) {
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, holder.draft_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.draft_delete, R.color.black);
}
2018-04-28 16:54:06 +02:00
holder.draft_date.setText(Helper.dateToString(draft.getCreation_date()));
2020-03-07 14:16:28 +01:00
holder.draft_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;
2017-07-15 14:59:09 +02:00
}
2020-03-07 14:16:28 +01:00
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
builder.setMessage(draft.getStatus().getContent() + '\n' + Helper.dateToString(draft.getCreation_date()));
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.remove_draft)
.setPositiveButton(R.string.yes, (dialog, which) -> {
new StatusStoredDAO(context, db).remove(draft.getId());
storedStatuses.remove(draft);
draftsListAdapter.notifyDataSetChanged();
if (storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
textviewNoAction.setVisibility(View.VISIBLE);
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
2017-07-15 14:59:09 +02:00
});
2019-09-06 17:55:14 +02:00
if (clickable) {
2020-03-07 14:16:28 +01:00
holder.drafts_container.setOnClickListener(v -> {
Intent intentToot;
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
intentToot = new Intent(context, TootActivity.class);
} else {
intentToot = new Intent(context, PixelfedComposeActivity.class);
}
2020-03-07 14:16:28 +01:00
Bundle b = new Bundle();
b.putLong("restored", draft.getId());
intentToot.putExtras(b);
context.startActivity(intentToot);
});
}
2017-07-15 14:59:09 +02:00
return convertView;
}
2020-03-07 14:16:28 +01:00
private static class ViewHolder {
LinearLayout drafts_container;
2017-07-15 14:59:09 +02:00
TextView draft_title;
TextView draft_date;
ImageView draft_delete;
}
}