fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayDraftsFragment.java

150 lines
6.3 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* 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
* 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,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
2017-10-30 07:54:02 +01:00
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
2017-10-30 07:54:02 +01:00
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2017-10-30 07:54:02 +01:00
import java.util.ArrayList;
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.StoredStatus;
import app.fedilab.android.drawers.DraftsListAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
2017-10-30 07:54:02 +01:00
/**
* Created by Thomas on 19/09/2017.
* Fragment to display drafts toots
*/
public class DisplayDraftsFragment extends Fragment {
private Context context;
2017-10-30 07:54:02 +01:00
private List<StoredStatus> drafts;
private DraftsListAdapter draftsListAdapter;
private RelativeLayout textviewNoAction;
2019-09-06 17:55:14 +02:00
@Override
2017-12-15 18:03:06 +01:00
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_drafts, container, false);
context = getContext();
2017-10-30 07:54:02 +01:00
final ListView lv_draft_toots = rootView.findViewById(R.id.lv_draft_toots);
2017-10-28 11:02:37 +02:00
RelativeLayout mainLoader = rootView.findViewById(R.id.loader);
2017-10-30 07:54:02 +01:00
textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
2017-10-30 07:54:02 +01:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_DARK) {
Helper.changeDrawableColor(context, R.drawable.ic_cancel, R.color.dark_text);
} else {
Helper.changeDrawableColor(context, R.drawable.ic_cancel, R.color.black);
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();
//Removes all scheduled toots that have sent
new StatusStoredDAO(context, db).removeAllSent();
2017-10-30 07:54:02 +01:00
drafts = new StatusStoredDAO(context, db).getAllDrafts();
2019-09-06 17:55:14 +02:00
if (drafts != null && drafts.size() > 0) {
2019-08-19 09:44:15 +02:00
draftsListAdapter = new DraftsListAdapter(drafts, true, textviewNoAction);
lv_draft_toots.setAdapter(draftsListAdapter);
draftsListAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
} else {
textviewNoAction.setVisibility(View.VISIBLE);
}
mainLoader.setVisibility(View.GONE);
2017-10-30 07:54:02 +01:00
FloatingActionButton delete_all = null;
try {
delete_all = ((MainActivity) context).findViewById(R.id.delete_all);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
if (delete_all != null)
2017-10-30 07:54:02 +01:00
delete_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-11-03 14:45:55 +01:00
final 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);
2017-10-30 07:54:02 +01:00
builder.setTitle(R.string.delete_all);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
new StatusStoredDAO(context, db).removeAllDrafts();
drafts = new ArrayList<>();
drafts.clear();
2019-08-19 09:44:15 +02:00
draftsListAdapter = new DraftsListAdapter(drafts, true, textviewNoAction);
2017-10-30 07:54:02 +01:00
lv_draft_toots.setAdapter(draftsListAdapter);
draftsListAdapter.notifyDataSetChanged();
textviewNoAction.setVisibility(View.VISIBLE);
dialogConfirm.dismiss();
}
})
2019-11-11 09:34:13 +01:00
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
2017-10-30 07:54:02 +01:00
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
dialogConfirm.dismiss();
}
})
.show();
}
});
return rootView;
}
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
}