package com.codigoparallevar.minicards; import android.app.Dialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Vector; import com.programaker.api.ProgramakerApi; public class DeckPreviewActivity extends ReloadableAppCompatActivity { public static final String INTENT = "com.codigoparallevar.minicards.DECK"; private ListView listView; private CardPreviewArrayAdapter cardArrayAdapter; private ProgramakerApi ProgramakerApi = new ProgramakerApi(); protected void openLoginDialog(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); final View loginDialog = (LayoutInflater.from(this) .inflate(R.layout.login_dialog_view, null)); final EditText loginUsernameText = (EditText) loginDialog.findViewById(R.id.login_username_text); final EditText loginPasswordText = (EditText) loginDialog.findViewById(R.id.login_password_text); final Button loginButton = (Button) loginDialog.findViewById(R.id.login_dialog_login_button); final Button cancelButton = (Button) loginDialog.findViewById(R.id.login_dialog_cancel_button); final TextView messageLabel = (TextView) loginDialog.findViewById(R.id.login_message_label); builder.setTitle("Login").setView(loginDialog); final Dialog dialog = builder.create(); dialog.show(); cancelButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { dialog.cancel(); } }); final TextWatcher watcher = (new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { if ((messageLabel.getVisibility() != View.VISIBLE) && (loginUsernameText.getText().length() > 0) && (loginPasswordText.getText().length() > 0)) { loginButton.setEnabled(true); } else { loginButton.setEnabled(false); } } }); loginButton.setEnabled(false); loginUsernameText.addTextChangedListener(watcher); loginPasswordText.addTextChangedListener(watcher); loginButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { messageLabel.setVisibility(View.VISIBLE); messageLabel.setText(R.string.loading); watcher.afterTextChanged(null); } }); } @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(); } }); Button loginButton = (Button) findViewById(R.id.login_in_programaker_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DeckPreviewActivity.this.openLoginDialog(v); } }); listView = (ListView) findViewById(R.id.card_deck_list); new AsyncTask() { protected Boolean doInBackground(ProgramakerApi... api) { return api[0].Check(); } protected void onPostExecute(Boolean result) { Log.d("MiniCards", "check result: " + result); } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this.ProgramakerApi); } @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 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()]); } }