Add support for renaming and deleting cards.

This commit is contained in:
kenkeiras 2018-01-24 21:53:28 +01:00
parent eafa61c34b
commit 42e67dc6a6
12 changed files with 197 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log;
import com.codigoparallevar.minicards.parts.buttons.RoundButton; import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.ColorBox;
@ -265,4 +266,20 @@ public class CardFile {
public static String getDefaultCardStorage(Context context) { public static String getDefaultCardStorage(Context context) {
return context.getFilesDir().getAbsolutePath() + CardFile.PATH_SEPARATOR + "cards"; 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();
}
} }

View File

@ -1,21 +1,32 @@
package com.codigoparallevar.minicards; package com.codigoparallevar.minicards;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> { class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
private final PreviewCard[] cards; private final PreviewCard[] cards;
private final ReloadableAppCompatActivity activity;
public CardPreviewArrayAdapter(@NonNull Context context, PreviewCard[] cards) { public CardPreviewArrayAdapter(@NonNull ReloadableAppCompatActivity activity, PreviewCard[] cards) {
super(context, R.layout.card_preview); super(activity, R.layout.card_preview);
this.activity = activity;
this.cards = cards; this.cards = cards;
} }
@ -27,7 +38,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 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]; final PreviewCard card = this.cards[position];
row.setOnClickListener(new View.OnClickListener() { row.setOnClickListener(new View.OnClickListener() {
@ -38,6 +49,15 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
CardPreviewArrayAdapter.this.getContext().startActivity(i); 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); CardView cardView = (CardView) row.findViewById(R.id.card_preview_card);
TextView nameView = (TextView) row.findViewById(R.id.card_preview_name); TextView nameView = (TextView) row.findViewById(R.id.card_preview_name);
@ -47,4 +67,91 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
return row; 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();
}
} }

View File

@ -5,7 +5,6 @@ import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -19,7 +18,7 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
public class DeckPreviewActivity extends AppCompatActivity { public class DeckPreviewActivity extends ReloadableAppCompatActivity {
public static final String INTENT = "com.codigoparallevar.minicards.DECK"; public static final String INTENT = "com.codigoparallevar.minicards.DECK";
private ListView listView; private ListView listView;
@ -47,7 +46,11 @@ public class DeckPreviewActivity extends AppCompatActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
cardArrayAdapter = new CardPreviewArrayAdapter(getApplicationContext(), listAvailableCards()); reload();
}
public void reload() {
cardArrayAdapter = new CardPreviewArrayAdapter(this, listAvailableCards());
listView.setAdapter(cardArrayAdapter); listView.setAdapter(cardArrayAdapter);
} }

View File

@ -0,0 +1,7 @@
package com.codigoparallevar.minicards;
import android.support.v7.app.AppCompatActivity;
abstract class ReloadableAppCompatActivity extends AppCompatActivity {
public abstract void reload();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 827 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -14,7 +14,6 @@
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:minHeight="100dp"
android:paddingBottom="5dp" android:paddingBottom="5dp"
android:paddingLeft="15dp" android:paddingLeft="15dp"
android:paddingRight="15dp" android:paddingRight="15dp"
@ -26,6 +25,7 @@
tools:layout_editor_absoluteY="3dp"> tools:layout_editor_absoluteY="3dp">
<RelativeLayout <RelativeLayout
android:minHeight="100dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
@ -38,7 +38,19 @@
android:text="TextView" android:text="TextView"
tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="3dp" /> tools:layout_editor_absoluteY="3dp" />
<ImageView
android:id="@+id/card_preview_settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:src="@drawable/ic_settings_black" />
</RelativeLayout> </RelativeLayout>
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
</android.support.v7.widget.LinearLayoutCompat> </android.support.v7.widget.LinearLayoutCompat>

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/card_setting_name_text_view"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:text="Card name"
android:layout_alignBaseline="@id/card_setting_name_edit_text"
/>
<EditText
android:id="@+id/card_setting_name_edit_text"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:ems="10"
android:layout_toRightOf="@id/card_setting_name_text_view"
android:text="My new card" />
<TextView
android:id="@+id/card_setting_delete_card"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/card_setting_name_edit_text"
android:layout_marginTop="10dp"
android:clickable="true"
android:text="Delete card"
android:textColor="@android:color/holo_red_dark"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>

View File

@ -2,7 +2,6 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:orientation="vertical"
android:layout_centerHorizontal="true"> android:layout_centerHorizontal="true">