138 lines
5.1 KiB
Java
138 lines
5.1 KiB
Java
package com.codigoparallevar.minicards;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.DialogInterface;
|
|
import android.os.Bundle;
|
|
import android.support.design.widget.FloatingActionButton;
|
|
import android.support.v7.app.AlertDialog;
|
|
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 java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.Vector;
|
|
|
|
public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|
|
|
public static final String INTENT = "com.codigoparallevar.minicards.DECK";
|
|
private ListView listView;
|
|
private CardPreviewArrayAdapter cardArrayAdapter;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_deck_preview);
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
setSupportActionBar(toolbar);
|
|
|
|
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.create_new_card_fab);
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
createOpenCardDialog();
|
|
}
|
|
});
|
|
|
|
listView = (ListView) findViewById(R.id.card_deck_list);
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
|
|
reload();
|
|
}
|
|
|
|
public void reload() {
|
|
cardArrayAdapter = new CardPreviewArrayAdapter(this, listAvailableCards());
|
|
|
|
listView.setAdapter(cardArrayAdapter);
|
|
}
|
|
|
|
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);
|
|
|
|
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(CardFile.DEFAULT_BACKGROUND_COLOR);
|
|
|
|
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,
|
|
CardActivity.DEVELOPER_VISUALIZATION_MODE);
|
|
}
|
|
});
|
|
|
|
Dialog dialog = builder.create();
|
|
dialog.show();
|
|
}
|
|
|
|
private PreviewCard[] listAvailableCards() {
|
|
String cardsPath = CardFile.getDefaultCardStorage(this);
|
|
File cardsDir = new File(cardsPath);
|
|
if (!cardsDir.exists()) {
|
|
cardsDir.mkdir();
|
|
}
|
|
|
|
File[] cardFiles = cardsDir.listFiles();
|
|
if (cardFiles.length == 0){
|
|
try {
|
|
CardFile.createDefaultCards(cardsDir);
|
|
} catch (IOException e) {
|
|
Log.e("Minicards Deck preview",
|
|
"IOException when creating default cards", e);
|
|
}
|
|
cardFiles = cardsDir.listFiles();
|
|
}
|
|
|
|
List<PreviewCard> cards = new Vector<>(cardFiles.length);
|
|
for (File cardFile : cardFiles) {
|
|
try {
|
|
CardFile card = CardFile.load(cardFile.getAbsolutePath(), new StubPartGrid());
|
|
cards.add(new PreviewCard(card.getName(), card.getBackgroundColor(), card.getPath()));
|
|
} catch (ErrorLoadingCardException e) {
|
|
Log.e("Minicards Deck preview",
|
|
"Error loading card, [reason=" + e.getReason()
|
|
+ "; path=" + cardFile.getAbsolutePath(),
|
|
e);
|
|
} catch (IOException e) {
|
|
Log.e("Minicards Deck preview",
|
|
"IOException loading card, [msg=" + e.getMessage()
|
|
+ "; path=" + cardFile.getAbsolutePath(),
|
|
e);
|
|
}
|
|
}
|
|
|
|
return cards.toArray(new PreviewCard[cards.size()]);
|
|
}
|
|
}
|