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

198 lines
8.0 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.DialogInterface;
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;
2019-06-11 19:38:26 +02:00
import androidx.appcompat.app.AlertDialog;
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;
import java.util.List;
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
*/
public class DraftsListAdapter extends BaseAdapter {
private List<StoredStatus> storedStatuses;
private LayoutInflater layoutInflater;
private Context context;
private DraftsListAdapter draftsListAdapter;
private boolean clickable;
2017-11-24 15:34:55 +01:00
private RelativeLayout textviewNoAction;
2017-07-15 14:59:09 +02:00
public DraftsListAdapter(Context context, List<StoredStatus> storedStatuses){
this.storedStatuses = storedStatuses;
this.context = context;
layoutInflater = LayoutInflater.from(context);
draftsListAdapter = this;
this.clickable = false;
}
2017-11-24 15:34:55 +01:00
public DraftsListAdapter(Context context, List<StoredStatus> storedStatuses, boolean clickable, RelativeLayout textviewNoAction){
this.storedStatuses = storedStatuses;
this.context = context;
layoutInflater = LayoutInflater.from(context);
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) {
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-12-15 18:03:06 +01:00
if( this.clickable ){
if (draft.getStatus().getContent().length() > 300)
2017-10-30 07:54:02 +01: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());
}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);
2018-11-28 17:50:14 +01: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);
}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()));
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;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
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();
2017-11-24 15:34:55 +01:00
if( storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
textviewNoAction.setVisibility(View.VISIBLE);
2017-07-15 14:59:09 +02:00
dialog.dismiss();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
});
if( clickable){
holder.drafts_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentToot = new Intent(context, TootActivity.class);
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 {
LinearLayout drafts_container;
2017-07-15 14:59:09 +02:00
TextView draft_title;
TextView draft_date;
ImageView draft_delete;
}
}