Add support for renaming and deleting cards.
This commit is contained in:
parent
eafa61c34b
commit
42e67dc6a6
12 changed files with 197 additions and 8 deletions
|
@ -5,6 +5,7 @@ import android.content.Context;
|
|||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
||||
import com.codigoparallevar.minicards.parts.samples.ColorBox;
|
||||
|
@ -265,4 +266,20 @@ public class CardFile {
|
|||
public static String getDefaultCardStorage(Context context) {
|
||||
return context.getFilesDir().getAbsolutePath() + CardFile.PATH_SEPARATOR + "cards";
|
||||
}
|
||||
|
||||
public void rename(Context context, String newName) {
|
||||
String oldPath = getPath();
|
||||
setName(newName);
|
||||
try {
|
||||
save(context);
|
||||
|
||||
new File(oldPath).delete();
|
||||
} catch (IOException e) {
|
||||
Log.e("Minicards - cardfile rename", "Cannot create new file for rename", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
new File(getPath()).delete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,32 @@
|
|||
package com.codigoparallevar.minicards;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.CardView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
|
||||
private final PreviewCard[] cards;
|
||||
private final ReloadableAppCompatActivity activity;
|
||||
|
||||
public CardPreviewArrayAdapter(@NonNull Context context, PreviewCard[] cards) {
|
||||
super(context, R.layout.card_preview);
|
||||
public CardPreviewArrayAdapter(@NonNull ReloadableAppCompatActivity activity, PreviewCard[] cards) {
|
||||
super(activity, R.layout.card_preview);
|
||||
|
||||
this.activity = activity;
|
||||
this.cards = cards;
|
||||
}
|
||||
|
||||
|
@ -27,7 +38,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
|
|||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
View row = inflater.inflate(R.layout.card_preview, parent, false);
|
||||
final View row = inflater.inflate(R.layout.card_preview, parent, false);
|
||||
final PreviewCard card = this.cards[position];
|
||||
|
||||
row.setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -38,6 +49,15 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
|
|||
CardPreviewArrayAdapter.this.getContext().startActivity(i);
|
||||
}
|
||||
});
|
||||
|
||||
final ImageView settingsButton = (ImageView) row.findViewById(R.id.card_preview_settings_button);
|
||||
settingsButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
openSettingsMenu(card);
|
||||
}
|
||||
});
|
||||
|
||||
CardView cardView = (CardView) row.findViewById(R.id.card_preview_card);
|
||||
TextView nameView = (TextView) row.findViewById(R.id.card_preview_name);
|
||||
|
||||
|
@ -47,4 +67,91 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
|
|||
|
||||
return row;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
final EditText cardNameEditText = (EditText) openCardOptions.findViewById(R.id.card_setting_name_edit_text);
|
||||
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();
|
||||
|
||||
final TextView deleteCardLink = (TextView) openCardOptions.findViewById(R.id.card_setting_delete_card);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import android.content.DialogInterface;
|
|||
import android.os.Bundle;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -19,7 +18,7 @@ import java.io.IOException;
|
|||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
public class DeckPreviewActivity extends AppCompatActivity {
|
||||
public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
||||
|
||||
public static final String INTENT = "com.codigoparallevar.minicards.DECK";
|
||||
private ListView listView;
|
||||
|
@ -47,7 +46,11 @@ public class DeckPreviewActivity extends AppCompatActivity {
|
|||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
cardArrayAdapter = new CardPreviewArrayAdapter(getApplicationContext(), listAvailableCards());
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
cardArrayAdapter = new CardPreviewArrayAdapter(this, listAvailableCards());
|
||||
|
||||
listView.setAdapter(cardArrayAdapter);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package com.codigoparallevar.minicards;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
abstract class ReloadableAppCompatActivity extends AppCompatActivity {
|
||||
public abstract void reload();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue