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.DialogInterface;
|
2017-09-20 10:04:40 +02:00
|
|
|
import android.content.Intent;
|
2017-11-03 16:24:27 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-11-28 17:50:14 +01:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2017-09-20 10:04:40 +02:00
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.os.Bundle;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
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;
|
2017-09-20 10:04:40 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-09-28 18:08:07 +02:00
|
|
|
import app.fedilab.android.activities.MainActivity;
|
|
|
|
import app.fedilab.android.activities.PixelfedComposeActivity;
|
|
|
|
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;
|
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.TootActivity;
|
2017-07-15 14:59:09 +02:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.changeDrawableColor;
|
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
|
|
|
|
|
|
|
private List<StoredStatus> storedStatuses;
|
|
|
|
private Context context;
|
|
|
|
private DraftsListAdapter draftsListAdapter;
|
2017-09-20 10:04:40 +02:00
|
|
|
private 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;
|
2017-09-20 10:04:40 +02:00
|
|
|
this.clickable = false;
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public DraftsListAdapter(List<StoredStatus> storedStatuses, boolean clickable, RelativeLayout textviewNoAction) {
|
2017-09-20 10:04:40 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2017-07-15 14:59:09 +02:00
|
|
|
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2017-09-20 10:04:40 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (this.clickable) {
|
2017-09-20 10:04:40 +02:00
|
|
|
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) {
|
2017-09-20 10:04:40 +02:00
|
|
|
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
|
|
|
}
|
2017-09-20 14:51:15 +02:00
|
|
|
|
2017-11-03 16:24:27 +01: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);
|
2017-11-03 16:24:27 +01:00
|
|
|
}
|
2018-04-28 16:54:06 +02:00
|
|
|
holder.draft_date.setText(Helper.dateToString(draft.getCreation_date()));
|
2017-07-15 14:59:09 +02:00
|
|
|
holder.draft_delete.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2018-11-03 14:45:55 +01:00
|
|
|
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) {
|
2018-11-03 14:45:55 +01:00
|
|
|
style = R.style.DialogBlack;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-11-03 14:45:55 +01:00
|
|
|
style = R.style.Dialog;
|
|
|
|
}
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
|
2018-04-28 16:54:06 +02:00
|
|
|
builder.setMessage(draft.getStatus().getContent() + '\n' + Helper.dateToString(draft.getCreation_date()));
|
2017-07-15 14:59:09 +02:00
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_alert)
|
|
|
|
.setTitle(R.string.remove_draft)
|
|
|
|
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
new StatusStoredDAO(context, db).remove(draft.getId());
|
|
|
|
storedStatuses.remove(draft);
|
|
|
|
draftsListAdapter.notifyDataSetChanged();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
|
2017-11-24 15:34:55 +01:00
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
2017-07-15 14:59:09 +02:00
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
})
|
2019-11-11 09:34:13 +01:00
|
|
|
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
|
2017-07-15 14:59:09 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
});
|
2019-09-06 17:55:14 +02:00
|
|
|
if (clickable) {
|
2017-09-20 10:04:40 +02:00
|
|
|
holder.drafts_container.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-09-28 18:08:07 +02:00
|
|
|
Intent intentToot;
|
|
|
|
if(MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
|
|
|
|
intentToot = new Intent(context, TootActivity.class);
|
|
|
|
}else {
|
|
|
|
intentToot = new Intent(context, PixelfedComposeActivity.class);
|
|
|
|
}
|
2017-09-20 10:04:40 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class ViewHolder {
|
2017-09-20 10:04:40 +02:00
|
|
|
LinearLayout drafts_container;
|
2017-07-15 14:59:09 +02:00
|
|
|
TextView draft_title;
|
|
|
|
TextView draft_date;
|
|
|
|
ImageView draft_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|