Add dialog for card creation.

This commit is contained in:
kenkeiras 2018-01-23 00:59:28 +01:00
parent 1508b143c5
commit aad92328cd
8 changed files with 128 additions and 16 deletions

View File

@ -29,5 +29,6 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:25.4.0' implementation 'com.android.support:design:25.4.0'
compile 'com.getbase:floatingactionbutton:1.10.1' compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.larswerkman:HoloColorPicker:1.5'
implementation 'com.android.support:cardview-v7:25.4.0' implementation 'com.android.support:cardview-v7:25.4.0'
} }

View File

@ -13,9 +13,6 @@ import android.view.View;
import com.codigoparallevar.minicards.motion.MotionMode; import com.codigoparallevar.minicards.motion.MotionMode;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.samples.Placeholder;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
@ -62,6 +59,7 @@ class CanvasView extends View implements PartGrid {
@Nullable @Nullable
private Tuple2<Integer, Integer> _mouseDownPoint = null; private Tuple2<Integer, Integer> _mouseDownPoint = null;
private int cardBackgroundColor;
public CanvasView(Context context) { public CanvasView(Context context) {
super(context); super(context);
@ -75,7 +73,7 @@ class CanvasView extends View implements PartGrid {
CardFile card = CardFile.load(path, this); CardFile card = CardFile.load(path, this);
name = card.getName(); name = card.getName();
setBackgroundColor(card.getBackgroundColor()); setCardBackgroundColor(card.getBackgroundColor());
parts = card.getParts(); parts = card.getParts();
List<PartConnection> connections = card.getConnections(); List<PartConnection> connections = card.getConnections();
@ -273,6 +271,7 @@ class CanvasView extends View implements PartGrid {
CardFile cardFile = new CardFile(CardFile.getDefaultCardStorage(getContext())); CardFile cardFile = new CardFile(CardFile.getDefaultCardStorage(getContext()));
cardFile.setName(name); cardFile.setName(name);
cardFile.addParts(parts); cardFile.addParts(parts);
cardFile.setBackgroundColor(getCardBackgroundColor());
cardFile.save(getContext()); cardFile.save(getContext());
} }
@ -376,4 +375,13 @@ class CanvasView extends View implements PartGrid {
int yOffset = _viewOrigin.item2 + getHeight() / 2; int yOffset = _viewOrigin.item2 + getHeight() / 2;
return new Tuple2<>(xOffset, yOffset); return new Tuple2<>(xOffset, yOffset);
} }
public int getCardBackgroundColor() {
return cardBackgroundColor;
}
public void setCardBackgroundColor(int backgroundColor) {
this.cardBackgroundColor = backgroundColor;
setBackgroundColor(backgroundColor);
}
} }

View File

@ -1,5 +1,6 @@
package com.codigoparallevar.minicards; package com.codigoparallevar.minicards;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@ -183,4 +184,10 @@ public class CardActivity extends AppCompatActivity {
removePartFab.getY(), removePartFab.getY() + removePartFab.getHeight()); removePartFab.getY(), removePartFab.getY() + removePartFab.getHeight());
} }
} }
public static void openCard(Context context, CardFile cardfile) {
Intent i = new Intent(INTENT);
i.putExtra(CARD_PATH_KEY, cardfile.getPath());
context.startActivity(i);
}
} }

View File

@ -1,9 +1,10 @@
package com.codigoparallevar.minicards; package com.codigoparallevar.minicards;
import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
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;
@ -24,7 +25,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -32,6 +32,8 @@ public class CardFile {
static final int DEFAULT_BACKGROUND_COLOR = Color.parseColor("#044563"); static final int DEFAULT_BACKGROUND_COLOR = Color.parseColor("#044563");
static final String PATH_SEPARATOR = "/"; static final String PATH_SEPARATOR = "/";
@NonNull
private final String cardsDirectory;
@NonNull @NonNull
private String name; private String name;
@ -44,13 +46,18 @@ public class CardFile {
private String path; private String path;
public CardFile(String cardsDirectory) { public CardFile(String cardsDirectory) {
this.cardsDirectory = cardsDirectory;
this.parts = new ArrayList<>(); this.parts = new ArrayList<>();
this.connections = new ArrayList<>(); this.connections = new ArrayList<>();
this.name = generateAnonymousName(); this.name = generateAnonymousName();
this.path = cardsDirectory + PATH_SEPARATOR + name + ".json"; regeneratePath();
this.backgroundColor = DEFAULT_BACKGROUND_COLOR; this.backgroundColor = DEFAULT_BACKGROUND_COLOR;
} }
private void regeneratePath() {
this.path = cardsDirectory + PATH_SEPARATOR + name + ".json";
}
@NonNull @NonNull
private String generateAnonymousName() { private String generateAnonymousName() {
return "unnamed-" + UUID.randomUUID().toString().substring(0, 6); return "unnamed-" + UUID.randomUUID().toString().substring(0, 6);
@ -59,6 +66,7 @@ public class CardFile {
@NonNull @NonNull
public CardFile setName(String name) { public CardFile setName(String name) {
this.name = name; this.name = name;
regeneratePath();
return this; return this;
} }
@ -191,16 +199,14 @@ public class CardFile {
(color & 0x0000FF)); (color & 0x0000FF));
} }
@TargetApi(Build.VERSION_CODES.KITKAT)
@NonNull @NonNull
public static CardFile load(String path, PartGrid grid) throws ErrorLoadingCardException, IOException { public static CardFile load(String path, PartGrid grid) throws ErrorLoadingCardException, IOException {
CardFile card = new CardFile(new File(path).getParent()); CardFile card = new CardFile(new File(path).getParent());
File file = new File(path); File file = new File(path);
FileReader fileIn = null;
List<PartConnection> connections = new LinkedList<>();
try { try (FileReader fileIn = new FileReader(file)) {
fileIn = new FileReader(file);
char[] data = new char[(int) file.length()]; char[] data = new char[(int) file.length()];
fileIn.read(data); fileIn.read(data);
JSONObject state = new JSONObject(new String(data)); JSONObject state = new JSONObject(new String(data));
@ -226,8 +232,6 @@ public class CardFile {
} }
} catch (JSONException e) { } catch (JSONException e) {
throw new ErrorLoadingCardException(ErrorLoadingCardException.Reason.UNKNOWN_FORMAT); throw new ErrorLoadingCardException(ErrorLoadingCardException.Reason.UNKNOWN_FORMAT);
} finally {
fileIn.close();
} }
return card; return card;

View File

@ -1,13 +1,23 @@
package com.codigoparallevar.minicards; package com.codigoparallevar.minicards;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; 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.View; import android.view.View;
import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import com.larswerkman.holocolorpicker.ColorPicker;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -31,8 +41,7 @@ public class DeckPreviewActivity extends AppCompatActivity {
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) createOpenCardDialog();
.setAction("Action", null).show();
} }
}); });
@ -44,6 +53,47 @@ public class DeckPreviewActivity extends AppCompatActivity {
} }
private void createOpenCardDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View openCardOptions = (LayoutInflater.from(this)
.inflate(R.layout.create_new_card_view, null));
final EditText cardNameEditText = (EditText) openCardOptions.findViewById(R.id.card_name_edit_text);
final ColorPicker backgroundColorPicker = (ColorPicker) openCardOptions.findViewById(R.id.card_background_color_picker);
builder.setTitle("Create a new card")
.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) {
CardFile cardfile = new CardFile(CardFile.getDefaultCardStorage(DeckPreviewActivity.this));
cardfile.setName(cardNameEditText.getText().toString());
cardfile.setBackgroundColor(backgroundColorPicker.getColor());
try {
cardfile.save(DeckPreviewActivity.this);
} catch (IOException e) {
Log.e("Minicards DeckPreview", "Error creating card "+ e, e);
Toast
.makeText(DeckPreviewActivity.this, "Error creating card " + e, Toast.LENGTH_LONG)
.show();
return;
}
CardActivity.openCard(DeckPreviewActivity.this, cardfile);
}
});
Dialog dialog = builder.create();
dialog.show();
}
private PreviewCard[] listAvailableCards() { private PreviewCard[] listAvailableCards() {
String cardsPath = CardFile.getDefaultCardStorage(this); String cardsPath = CardFile.getDefaultCardStorage(this);
File cardsDir = new File(cardsPath); File cardsDir = new File(cardsPath);

View File

@ -28,6 +28,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin" android:layout_margin="@dimen/fab_margin"
android:theme="@style/AppTheme"
app:srcCompat="@drawable/ic_add_black" /> app:srcCompat="@drawable/ic_add_black" />
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,41 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/card_name_text_view"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:text="Card name"
android:layout_alignBaseline="@id/card_name_edit_text"
/>
<EditText
android:id="@+id/card_name_edit_text"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:ems="10"
android:layout_toRightOf="@id/card_name_text_view"
android:text="My new card" />
</RelativeLayout>
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/card_background_color_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>

View File

@ -2,7 +2,7 @@
<resources> <resources>
<color name="colorPrimary">#066996</color> <color name="colorPrimary">#066996</color>
<color name="colorPrimaryDark">#044563</color> <color name="colorPrimaryDark">#044563</color>
<color name="colorAccent">#b2ff59</color> <color name="colorAccent">#ffb300</color>
<color name="black_semi_transparent">#B2000000</color> <color name="black_semi_transparent">#B2000000</color>
<color name="background">#e5e5e5</color> <color name="background">#e5e5e5</color>