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

450 lines
24 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-07-16 17:09:35 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-07-16 17:09:35 +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-16 17:09:35 +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-16 17:09:35 +02:00
* see <http://www.gnu.org/licenses>. */
2017-10-29 09:39:29 +01:00
import android.annotation.SuppressLint;
2017-07-16 17:09:35 +02:00
import android.content.Context;
import android.content.DialogInterface;
2017-07-16 19:04:53 +02:00
import android.content.Intent;
2017-07-16 17:09:35 +02:00
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
2019-01-20 15:20:07 +01:00
import android.os.AsyncTask;
2017-07-17 18:53:12 +02:00
import android.os.Build;
2017-07-16 19:04:53 +02:00
import android.os.Bundle;
2019-06-11 19:38:26 +02:00
import androidx.appcompat.app.AlertDialog;
import android.text.Html;
import android.text.Spanned;
2017-07-16 17:09:35 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
2017-07-17 18:53:12 +02:00
import android.widget.DatePicker;
import android.widget.ImageButton;
2017-07-16 17:09:35 +02:00
import android.widget.ImageView;
import android.widget.LinearLayout;
2017-07-17 18:53:12 +02:00
import android.widget.RelativeLayout;
2017-07-16 17:09:35 +02:00
import android.widget.TextView;
2017-07-17 18:53:12 +02:00
import android.widget.TimePicker;
import android.widget.Toast;
2017-07-16 19:04:53 +02:00
2017-07-17 18:53:12 +02:00
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
2017-07-16 17:09:35 +02:00
import java.util.List;
2019-01-20 19:07:24 +01:00
import java.util.TimeZone;
2017-07-16 19:04:53 +02:00
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.StoredStatus;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.jobs.ApplicationJob;
import app.fedilab.android.jobs.ScheduledBoostsSyncJob;
import app.fedilab.android.jobs.ScheduledTootsSyncJob;
import app.fedilab.android.sqlite.BoostScheduleDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
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.activities.MainActivity;
import app.fedilab.android.activities.ShowConversationActivity;
import app.fedilab.android.activities.TootActivity;
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.fragments.DisplayScheduledTootsFragment;
import app.fedilab.android.interfaces.OnPostActionInterface;
2017-07-16 17:09:35 +02:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.helper.Helper.changeDrawableColor;
2017-07-16 17:09:35 +02:00
/**
* Created by Thomas on 16/07/2017.
* Adapter for scheduled toots
*/
2019-01-20 15:20:07 +01:00
public class ScheduledTootsListAdapter extends BaseAdapter implements OnPostActionInterface {
2017-07-16 17:09:35 +02:00
private Context context;
private List<StoredStatus> storedStatuses;
private LayoutInflater layoutInflater;
private ScheduledTootsListAdapter scheduledTootsListAdapter;
2017-07-17 18:53:12 +02:00
private RelativeLayout textviewNoAction;
private DisplayScheduledTootsFragment.typeOfSchedule type;
2017-07-16 17:09:35 +02:00
public ScheduledTootsListAdapter(Context context, DisplayScheduledTootsFragment.typeOfSchedule type, List<StoredStatus> storedStatuses, RelativeLayout textviewNoAction){
2017-07-16 17:09:35 +02:00
this.context = context;
this.storedStatuses = storedStatuses;
layoutInflater = LayoutInflater.from(this.context);
scheduledTootsListAdapter = this;
2017-07-17 18:53:12 +02:00
this.textviewNoAction = textviewNoAction;
this.type = type;
2017-07-16 17:09:35 +02:00
}
@Override
public int getCount() {
return storedStatuses.size();
}
@Override
2019-01-20 09:45:48 +01:00
public StoredStatus getItem(int position) {
2017-07-16 17:09:35 +02:00
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 storedStatus = storedStatuses.get(position);
2019-01-20 09:45:48 +01:00
final Status status = storedStatuses.get(position).getStatus();
2017-07-16 17:09:35 +02:00
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_scheduled_toot, parent, false);
holder = new ViewHolder();
holder.scheduled_toot_pp= convertView.findViewById(R.id.scheduled_toot_pp);
2017-10-29 09:39:29 +01:00
holder.scheduled_toot_title = convertView.findViewById(R.id.scheduled_toot_title);
holder.scheduled_toot_date_creation = convertView.findViewById(R.id.scheduled_toot_date_creation);
holder.scheduled_toot_media_count = convertView.findViewById(R.id.scheduled_toot_media_count);
holder.scheduled_toot_failed = convertView.findViewById(R.id.scheduled_toot_failed);
holder.scheduled_toot_delete = convertView.findViewById(R.id.scheduled_toot_delete);
holder.scheduled_toot_privacy = convertView.findViewById(R.id.scheduled_toot_privacy);
holder.scheduled_toot_date = convertView.findViewById(R.id.scheduled_toot_date);
holder.scheduled_toot_container = convertView.findViewById(R.id.scheduled_toot_container);
2017-07-16 17:09:35 +02:00
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2017-08-13 11:30:28 +02:00
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == Helper.THEME_BLACK) {
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_cancel,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_public,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_lock_open,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_lock_outline,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_mail_outline,R.color.action_black);
}else if( theme == Helper.THEME_DARK){
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_cancel,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_public,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_lock_open,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_lock_outline,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_mail_outline,R.color.action_dark);
2017-07-16 17:09:35 +02:00
}else {
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_cancel,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_public,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_lock_open,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_lock_outline,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_mail_outline,R.color.action_light);
2017-07-16 17:09:35 +02:00
}
switch (status.getVisibility()) {
case "public":
2017-10-28 14:32:18 +02:00
holder.scheduled_toot_privacy.setImageResource(R.drawable.ic_public);
2017-07-16 17:09:35 +02:00
break;
case "unlisted":
2017-10-28 14:54:28 +02:00
holder.scheduled_toot_privacy.setImageResource(R.drawable.ic_lock_open);
2017-07-16 17:09:35 +02:00
break;
case "private":
2017-10-28 14:32:18 +02:00
holder.scheduled_toot_privacy.setImageResource(R.drawable.ic_lock_outline);
2017-07-16 17:09:35 +02:00
break;
case "direct":
2017-10-11 14:16:56 +02:00
holder.scheduled_toot_privacy.setImageResource(R.drawable.ic_mail_outline);
2017-07-16 17:09:35 +02:00
break;
}
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2018-11-03 14:45:55 +01:00
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;
}
2017-07-16 17:09:35 +02:00
//Delete scheduled toot
holder.scheduled_toot_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2018-11-03 14:45:55 +01:00
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
String message;
if( type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
message = status.getContent() + '\n' + Helper.dateToString(storedStatus.getCreation_date());
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString();
else
//noinspection deprecation
message = Html.fromHtml(status.getContent()).toString();
message += '\n' + Helper.dateToString(storedStatus.getScheduled_date());
}
builder.setMessage(message);
2017-07-16 17:09:35 +02:00
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.remove_scheduled)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2019-01-20 15:20:07 +01:00
if( type != DisplayScheduledTootsFragment.typeOfSchedule.SERVER) {
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
new StatusStoredDAO(context, db).remove(storedStatus.getId());
else if (type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST)
new BoostScheduleDAO(context, db).remove(storedStatus.getId());
storedStatuses.remove(storedStatus);
scheduledTootsListAdapter.notifyDataSetChanged();
if (storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
2017-07-17 18:53:12 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
2019-01-20 15:20:07 +01:00
try {
//Cancel the job
ApplicationJob.cancelJob(storedStatus.getJobId());
} catch (Exception ignored) {
}
}else{
new PostActionAsyncTask(context, API.StatusAction.DELETESCHEDULED, storedStatus, ScheduledTootsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
storedStatuses.remove(storedStatus);
scheduledTootsListAdapter.notifyDataSetChanged();
if (storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
textviewNoAction.setVisibility(View.VISIBLE);
}
2017-07-16 17:09:35 +02:00
dialog.dismiss();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
});
2017-07-17 18:53:12 +02:00
2019-01-20 15:20:07 +01:00
if( type != DisplayScheduledTootsFragment.typeOfSchedule.SERVER) {
if (storedStatus.getJobId() > 0) {
holder.scheduled_toot_failed.setVisibility(View.GONE);
} else {
holder.scheduled_toot_failed.setVisibility(View.VISIBLE);
}
2017-07-16 19:04:53 +02:00
}
2017-07-16 17:09:35 +02:00
holder.scheduled_toot_media_count.setText(context.getString(R.string.media_count, status.getMedia_attachments().size()));
2018-04-28 16:54:06 +02:00
holder.scheduled_toot_date_creation.setText(Helper.dateToString(storedStatus.getCreation_date()));
holder.scheduled_toot_date.setText(Helper.dateToString(storedStatus.getScheduled_date()));
if( type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST){
holder.scheduled_toot_media_count.setVisibility(View.GONE);
holder.scheduled_toot_date_creation.setVisibility(View.GONE);
holder.scheduled_toot_privacy.setVisibility(View.GONE);
Helper.loadGiF(context, storedStatus.getStatus().getAccount().getAvatar(), holder.scheduled_toot_pp);
}else {
holder.scheduled_toot_pp.setVisibility(View.GONE);
}
2017-07-17 18:53:12 +02:00
holder.scheduled_toot_date.setOnClickListener(new View.OnClickListener() {
2017-07-16 19:04:53 +02:00
@Override
public void onClick(View v) {
2018-11-03 14:45:55 +01:00
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
2017-07-17 18:53:12 +02:00
LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
2017-10-29 09:39:29 +01:00
@SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.datetime_picker, null);
2017-07-17 18:53:12 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == Helper.THEME_BLACK){
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_skip_previous,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_skip_next,R.color.action_black);
Helper.changeDrawableColor(context, R.drawable.ic_check,R.color.action_black);
}else if( theme == Helper.THEME_DARK){
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_skip_previous,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_skip_next,R.color.action_dark);
Helper.changeDrawableColor(context, R.drawable.ic_check,R.color.action_dark);
2017-07-17 18:53:12 +02:00
}else {
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_skip_previous,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_skip_next,R.color.action_light);
Helper.changeDrawableColor(context, R.drawable.ic_check,R.color.action_light);
2017-07-17 18:53:12 +02:00
}
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
2017-10-29 09:39:29 +01:00
final DatePicker datePicker = dialogView.findViewById(R.id.date_picker);
final TimePicker timePicker = dialogView.findViewById(R.id.time_picker);
2019-05-24 15:10:43 +02:00
if (android.text.format.DateFormat.is24HourFormat(context))
timePicker.setIs24HourView(true);
2017-10-29 09:39:29 +01:00
Button date_time_cancel = dialogView.findViewById(R.id.date_time_cancel);
final ImageButton date_time_previous = dialogView.findViewById(R.id.date_time_previous);
final ImageButton date_time_next = dialogView.findViewById(R.id.date_time_next);
final ImageButton date_time_set = dialogView.findViewById(R.id.date_time_set);
2017-07-17 18:53:12 +02:00
//Buttons management
date_time_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
date_time_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePicker.setVisibility(View.GONE);
timePicker.setVisibility(View.VISIBLE);
date_time_previous.setVisibility(View.VISIBLE);
date_time_next.setVisibility(View.GONE);
date_time_set.setVisibility(View.VISIBLE);
}
});
date_time_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePicker.setVisibility(View.VISIBLE);
timePicker.setVisibility(View.GONE);
date_time_previous.setVisibility(View.GONE);
date_time_next.setVisibility(View.VISIBLE);
date_time_set.setVisibility(View.GONE);
}
});
date_time_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour, minute;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
}else {
//noinspection deprecation
hour = timePicker.getCurrentHour();
//noinspection deprecation
minute = timePicker.getCurrentMinute();
}
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
hour,
minute);
long time = calendar.getTimeInMillis();
if( (time - new Date().getTime()) < 60000 ){
2018-11-25 10:45:16 +01:00
Toasty.error(context, context.getString(R.string.toot_scheduled_date), Toast.LENGTH_LONG).show();
2017-07-17 18:53:12 +02:00
}else {
//Schedules the toot to the new date
2019-01-20 15:20:07 +01:00
if( type != DisplayScheduledTootsFragment.typeOfSchedule.SERVER) {
try {
//Removes the job
ApplicationJob.cancelJob(storedStatus.getJobId());
//Replace it by the new one
StoredStatus storedStatusnew = null;
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT) {
ScheduledTootsSyncJob.schedule(context, storedStatus.getId(), time);
storedStatusnew = new StatusStoredDAO(context, db).getStatus(storedStatus.getId());
} else if (type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST) {
ScheduledBoostsSyncJob.scheduleUpdate(context, storedStatus.getId(), time);
storedStatusnew = new BoostScheduleDAO(context, db).getStatus(storedStatus.getId());
}
//Date displayed is changed
assert storedStatusnew != null;
storedStatus.setScheduled_date(storedStatusnew.getScheduled_date());
scheduledTootsListAdapter.notifyDataSetChanged();
//Notifiy all is ok
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
Toasty.success(context, context.getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
else
Toasty.success(context, context.getString(R.string.boost_scheduled), Toast.LENGTH_LONG).show();
} catch (Exception ignored) {
}
2019-01-20 15:20:07 +01:00
}else{
2019-01-20 19:07:24 +01:00
int offset = TimeZone.getDefault().getRawOffset();
calendar.add(Calendar.MILLISECOND, -offset);
final String date = Helper.dateToString(new Date(calendar.getTimeInMillis()));
storedStatus.getStatus().setScheduled_at(date);
2019-01-20 15:20:07 +01:00
new PostActionAsyncTask(context, API.StatusAction.UPDATESERVERSCHEDULE, storedStatus, ScheduledTootsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Toasty.success(context, context.getString(R.string.boost_scheduled), Toast.LENGTH_LONG).show();
}
2017-07-17 18:53:12 +02:00
alertDialog.dismiss();
}
}
});
alertDialog.show();
2017-07-16 19:04:53 +02:00
}
});
if( type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
holder.scheduled_toot_title.setText(status.getContent());
else if( type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST){
Spanned message;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY);
else
//noinspection deprecation
message = Html.fromHtml(status.getContent());
holder.scheduled_toot_title.setText(message, TextView.BufferType.SPANNABLE);
}
2017-07-16 17:09:35 +02:00
2017-07-16 19:04:53 +02:00
if( type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
holder.scheduled_toot_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", storedStatus.getId());
b.putBoolean("restoredScheduled", true);
intentToot.putExtras(b);
context.startActivity(intentToot);
}
});
else if( type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST)
holder.scheduled_toot_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentToot = new Intent(context, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putParcelable("status", storedStatus.getStatus());
intentToot.putExtras(b);
context.startActivity(intentToot);
}
});
2019-01-20 15:20:07 +01:00
else if( type == DisplayScheduledTootsFragment.typeOfSchedule.SERVER)
holder.scheduled_toot_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentToot = new Intent(context, TootActivity.class);
Bundle b = new Bundle();
if( storedStatus.getStatus().getSpoiler_text().equals("null"))
storedStatus.getStatus().setSpoiler_text("");
b.putParcelable("storedStatus", storedStatus);
intentToot.putExtras(b);
context.startActivity(intentToot);
}
});
2017-07-16 17:09:35 +02:00
return convertView;
}
2019-01-20 15:20:07 +01:00
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
}
2017-07-16 17:09:35 +02:00
private class ViewHolder {
LinearLayout scheduled_toot_container;
2017-07-16 17:09:35 +02:00
TextView scheduled_toot_title;
TextView scheduled_toot_date_creation;
TextView scheduled_toot_media_count;
2017-07-16 19:04:53 +02:00
TextView scheduled_toot_failed;
2017-07-16 17:09:35 +02:00
ImageView scheduled_toot_delete;
ImageView scheduled_toot_privacy;
ImageView scheduled_toot_pp;
2017-07-16 17:09:35 +02:00
Button scheduled_toot_date;
}
}