diff --git a/app/build.gradle b/app/build.gradle index 60f8f7a..5f303e0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,5 +29,6 @@ dependencies { implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:25.4.0' compile 'com.getbase:floatingactionbutton:1.10.1' + compile 'com.larswerkman:HoloColorPicker:1.5' implementation 'com.android.support:cardview-v7:25.4.0' } diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 5dc4c9f..9ff3ec7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -13,9 +13,6 @@ import android.view.View; import com.codigoparallevar.minicards.motion.MotionMode; 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.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; @@ -62,6 +59,7 @@ class CanvasView extends View implements PartGrid { @Nullable private Tuple2 _mouseDownPoint = null; + private int cardBackgroundColor; public CanvasView(Context context) { super(context); @@ -75,7 +73,7 @@ class CanvasView extends View implements PartGrid { CardFile card = CardFile.load(path, this); name = card.getName(); - setBackgroundColor(card.getBackgroundColor()); + setCardBackgroundColor(card.getBackgroundColor()); parts = card.getParts(); List connections = card.getConnections(); @@ -273,6 +271,7 @@ class CanvasView extends View implements PartGrid { CardFile cardFile = new CardFile(CardFile.getDefaultCardStorage(getContext())); cardFile.setName(name); cardFile.addParts(parts); + cardFile.setBackgroundColor(getCardBackgroundColor()); cardFile.save(getContext()); } @@ -376,4 +375,13 @@ class CanvasView extends View implements PartGrid { int yOffset = _viewOrigin.item2 + getHeight() / 2; return new Tuple2<>(xOffset, yOffset); } + + public int getCardBackgroundColor() { + return cardBackgroundColor; + } + + public void setCardBackgroundColor(int backgroundColor) { + this.cardBackgroundColor = backgroundColor; + setBackgroundColor(backgroundColor); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java b/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java index 256bf76..0257d2d 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java @@ -1,5 +1,6 @@ package com.codigoparallevar.minicards; +import android.content.Context; import android.content.Intent; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.ActionBar; @@ -183,4 +184,10 @@ public class CardActivity extends AppCompatActivity { 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); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java index 1ca279b..feff0c2 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java @@ -1,9 +1,10 @@ package com.codigoparallevar.minicards; +import android.annotation.TargetApi; 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; @@ -24,7 +25,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -32,6 +32,8 @@ public class CardFile { static final int DEFAULT_BACKGROUND_COLOR = Color.parseColor("#044563"); static final String PATH_SEPARATOR = "/"; + @NonNull + private final String cardsDirectory; @NonNull private String name; @@ -44,13 +46,18 @@ public class CardFile { private String path; public CardFile(String cardsDirectory) { + this.cardsDirectory = cardsDirectory; this.parts = new ArrayList<>(); this.connections = new ArrayList<>(); this.name = generateAnonymousName(); - this.path = cardsDirectory + PATH_SEPARATOR + name + ".json"; + regeneratePath(); this.backgroundColor = DEFAULT_BACKGROUND_COLOR; } + private void regeneratePath() { + this.path = cardsDirectory + PATH_SEPARATOR + name + ".json"; + } + @NonNull private String generateAnonymousName() { return "unnamed-" + UUID.randomUUID().toString().substring(0, 6); @@ -59,6 +66,7 @@ public class CardFile { @NonNull public CardFile setName(String name) { this.name = name; + regeneratePath(); return this; } @@ -191,16 +199,14 @@ public class CardFile { (color & 0x0000FF)); } + @TargetApi(Build.VERSION_CODES.KITKAT) @NonNull public static CardFile load(String path, PartGrid grid) throws ErrorLoadingCardException, IOException { CardFile card = new CardFile(new File(path).getParent()); File file = new File(path); - FileReader fileIn = null; - List connections = new LinkedList<>(); - try { - fileIn = new FileReader(file); + try (FileReader fileIn = new FileReader(file)) { char[] data = new char[(int) file.length()]; fileIn.read(data); JSONObject state = new JSONObject(new String(data)); @@ -226,8 +232,6 @@ public class CardFile { } } catch (JSONException e) { throw new ErrorLoadingCardException(ErrorLoadingCardException.Reason.UNKNOWN_FORMAT); - } finally { - fileIn.close(); } return card; diff --git a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java index e3d918e..265f0b4 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java @@ -1,13 +1,23 @@ 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.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; +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; import android.view.View; +import android.widget.EditText; import android.widget.ListView; +import android.widget.Toast; + +import com.larswerkman.holocolorpicker.ColorPicker; import java.io.File; import java.io.IOException; @@ -31,8 +41,7 @@ public class DeckPreviewActivity extends AppCompatActivity { fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); + createOpenCardDialog(); } }); @@ -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() { String cardsPath = CardFile.getDefaultCardStorage(this); File cardsDir = new File(cardsPath); diff --git a/app/src/main/res/layout/activity_deck_preview.xml b/app/src/main/res/layout/activity_deck_preview.xml index 8b41796..454b5e7 100644 --- a/app/src/main/res/layout/activity_deck_preview.xml +++ b/app/src/main/res/layout/activity_deck_preview.xml @@ -28,6 +28,7 @@ android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" + android:theme="@style/AppTheme" app:srcCompat="@drawable/ic_add_black" /> diff --git a/app/src/main/res/layout/create_new_card_view.xml b/app/src/main/res/layout/create_new_card_view.xml new file mode 100644 index 0000000..a876fba --- /dev/null +++ b/app/src/main/res/layout/create_new_card_view.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 707f98f..0908017 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -2,7 +2,7 @@ #066996 #044563 - #b2ff59 + #ffb300 #B2000000 #e5e5e5