2018-01-21 11:05:54 +00:00
|
|
|
package com.codigoparallevar.minicards;
|
|
|
|
|
2018-01-24 20:53:28 +00:00
|
|
|
import android.app.Dialog;
|
2018-01-21 11:05:54 +00:00
|
|
|
import android.content.Context;
|
2018-01-24 20:53:28 +00:00
|
|
|
import android.content.DialogInterface;
|
2018-01-21 11:05:54 +00:00
|
|
|
import android.content.Intent;
|
2020-03-02 21:10:22 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.cardview.widget.CardView;
|
2018-01-24 20:53:28 +00:00
|
|
|
import android.util.Log;
|
2018-01-21 11:05:54 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
2018-01-24 20:53:28 +00:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
2018-01-21 11:05:54 +00:00
|
|
|
import android.widget.TextView;
|
2018-01-24 20:53:28 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-05-20 08:42:38 +00:00
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
|
|
|
2018-01-24 20:53:28 +00:00
|
|
|
import java.io.IOException;
|
2018-01-21 11:05:54 +00:00
|
|
|
|
|
|
|
class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
|
|
|
|
private final PreviewCard[] cards;
|
2018-01-24 20:53:28 +00:00
|
|
|
private final ReloadableAppCompatActivity activity;
|
2018-01-21 11:05:54 +00:00
|
|
|
|
2018-01-24 20:53:28 +00:00
|
|
|
public CardPreviewArrayAdapter(@NonNull ReloadableAppCompatActivity activity, PreviewCard[] cards) {
|
|
|
|
super(activity, R.layout.card_preview);
|
2018-01-21 11:05:54 +00:00
|
|
|
|
2018-01-24 20:53:28 +00:00
|
|
|
this.activity = activity;
|
2018-01-21 11:05:54 +00:00
|
|
|
this.cards = cards;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return this.cards.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2018-01-24 20:53:28 +00:00
|
|
|
final View row = inflater.inflate(R.layout.card_preview, parent, false);
|
2018-01-22 00:03:17 +00:00
|
|
|
final PreviewCard card = this.cards[position];
|
2018-01-21 11:05:54 +00:00
|
|
|
|
2020-05-20 08:42:38 +00:00
|
|
|
TextView nameView = row.findViewById(R.id.card_preview_name);
|
|
|
|
final CardView cardView = row.findViewById(R.id.card_preview_card);
|
|
|
|
final FloatingActionButton settingsButton = row.findViewById(R.id.card_preview_settings_button);
|
|
|
|
|
|
|
|
cardView.setOnClickListener(new View.OnClickListener() {
|
2018-01-21 11:05:54 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2020-05-20 14:53:18 +00:00
|
|
|
CardActivity.openCard(getContext(), card, CardActivity.DEVELOPER_VISUALIZATION_MODE);
|
2018-01-21 11:05:54 +00:00
|
|
|
}
|
|
|
|
});
|
2018-01-24 20:53:28 +00:00
|
|
|
|
2020-05-20 08:42:38 +00:00
|
|
|
|
2018-01-24 20:53:28 +00:00
|
|
|
settingsButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
openSettingsMenu(card);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-21 11:05:54 +00:00
|
|
|
|
|
|
|
nameView.setText(card.getName());
|
|
|
|
return row;
|
|
|
|
}
|
2018-01-24 20:53:28 +00:00
|
|
|
|
|
|
|
private void openSettingsMenu(final PreviewCard card) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
|
|
|
|
|
|
|
final View openCardOptions = (LayoutInflater.from(getContext())
|
|
|
|
.inflate(R.layout.card_settings_dialog, null));
|
|
|
|
|
2020-05-19 17:19:26 +00:00
|
|
|
final EditText cardNameEditText = openCardOptions.findViewById(R.id.card_setting_name_edit_text);
|
2018-01-24 20:53:28 +00:00
|
|
|
cardNameEditText.setText(card.getName());
|
|
|
|
|
|
|
|
builder.setTitle("Card settings")
|
|
|
|
.setView(openCardOptions)
|
|
|
|
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
|
|
|
CardFile cardFile = CardFile.load(card.getPath(), new StubPartGrid());
|
|
|
|
cardFile.rename(getContext(), cardNameEditText.getText().toString());
|
|
|
|
CardPreviewArrayAdapter.this.activity.reload();
|
|
|
|
} catch (ErrorLoadingCardException e) {
|
|
|
|
Log.e("Minicards CardSettings", "Error loading card "+ e, e);
|
|
|
|
Toast.makeText(getContext(),
|
|
|
|
"Error loading card " + e,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e("Minicards CardSettings", "Error creating card "+ e, e);
|
|
|
|
Toast.makeText(getContext(),
|
|
|
|
"Error creating card " + e,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final Dialog dialog = builder.create();
|
|
|
|
|
2020-05-19 17:19:26 +00:00
|
|
|
final TextView deleteCardLink = openCardOptions.findViewById(R.id.card_setting_delete_card);
|
2018-01-24 20:53:28 +00:00
|
|
|
deleteCardLink.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
dialog.dismiss();
|
|
|
|
checkDeleteCard(card);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDeleteCard(final PreviewCard card) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
|
|
|
|
.setTitle("Delete card " + card.getName())
|
|
|
|
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
|
|
|
CardFile cardFile = CardFile.load(card.getPath(), new StubPartGrid());
|
|
|
|
cardFile.delete();
|
|
|
|
CardPreviewArrayAdapter.this.activity.reload();
|
|
|
|
} catch (ErrorLoadingCardException e) {
|
|
|
|
Log.e("Minicards CardSettings", "Error loading card "+ e, e);
|
|
|
|
Toast.makeText(getContext(),
|
|
|
|
"Error loading card " + e,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e("Minicards CardSettings", "Error loading card "+ e, e);
|
|
|
|
Toast.makeText(getContext(),
|
|
|
|
"Error loading card " + e,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
final Dialog dialog = builder.create();
|
|
|
|
dialog.show();
|
|
|
|
}
|
2018-01-21 11:05:54 +00:00
|
|
|
}
|