diff --git a/app/build.gradle b/app/build.gradle index 5dd9eec..bef0b57 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,6 +19,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility = 1.8 + targetCompatibility = 1.8 + } } dependencies { diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 657fe65..23d9f73 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -17,8 +17,8 @@ import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Selectable; -import com.codigoparallevar.minicards.types.Tuple2; -import com.codigoparallevar.minicards.types.Tuple4; +import com.codigoparallevar.minicards.types.functional.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple4; import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java index 6a8309e..d463ca2 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java @@ -16,7 +16,7 @@ import com.codigoparallevar.minicards.parts.strings.ConvertToString; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import org.json.JSONArray; import org.json.JSONException; diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardPreviewArrayAdapter.java b/app/src/main/java/com/codigoparallevar/minicards/CardPreviewArrayAdapter.java index 7cefcbe..e0b8f69 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardPreviewArrayAdapter.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardPreviewArrayAdapter.java @@ -50,7 +50,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter { } }); - final ImageView settingsButton = (ImageView) row.findViewById(R.id.card_preview_settings_button); + final ImageView settingsButton = row.findViewById(R.id.card_preview_settings_button); settingsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -58,19 +58,10 @@ class CardPreviewArrayAdapter extends ArrayAdapter { } }); - CardView cardView = (CardView) row.findViewById(R.id.card_preview_card); - TextView nameView = (TextView) row.findViewById(R.id.card_preview_name); + CardView cardView = row.findViewById(R.id.card_preview_card); + TextView nameView = row.findViewById(R.id.card_preview_name); - int backgroundColor = card.getColor(); - cardView.setBackgroundColor(backgroundColor); nameView.setText(card.getName()); - if (backgroundColor == CardFile.DEFAULT_BACKGROUND_COLOR){ - nameView.setTextColor(0xFFF0F0F0); - } - else { - nameView.setTextColor(0xFFFFFF ^ backgroundColor); - } - return row; } @@ -80,7 +71,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter { 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); + final EditText cardNameEditText = openCardOptions.findViewById(R.id.card_setting_name_edit_text); cardNameEditText.setText(card.getName()); builder.setTitle("Card settings") @@ -115,7 +106,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter { final Dialog dialog = builder.create(); - final TextView deleteCardLink = (TextView) openCardOptions.findViewById(R.id.card_setting_delete_card); + final TextView deleteCardLink = openCardOptions.findViewById(R.id.card_setting_delete_card); deleteCardLink.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java index c6f8386..722ae9f 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/DeckPreviewActivity.java @@ -2,8 +2,13 @@ package com.codigoparallevar.minicards; import android.app.Dialog; import android.content.DialogInterface; +import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; + +import com.codigoparallevar.minicards.types.functional.Function; +import com.codigoparallevar.minicards.types.functional.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple3; import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; @@ -27,6 +32,8 @@ import java.util.Vector; import com.programaker.api.ProgramakerApi; public class DeckPreviewActivity extends ReloadableAppCompatActivity { + private static final String TOKEN_KEY = "PROGRAMAKER_API_TOKEN"; + private static final String PREFERENCES_NAME = "MINICARDS_PREFERENCES"; public static final String INTENT = "com.codigoparallevar.minicards.DECK"; private ListView listView; @@ -39,11 +46,11 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity { 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); + final EditText loginUsernameText = loginDialog.findViewById(R.id.login_username_text); + final EditText loginPasswordText = loginDialog.findViewById(R.id.login_password_text); + final Button loginButton = loginDialog.findViewById(R.id.login_dialog_login_button); + final Button cancelButton = loginDialog.findViewById(R.id.login_dialog_cancel_button); + final TextView messageLabel = loginDialog.findViewById(R.id.login_message_label); builder.setTitle("Login").setView(loginDialog); final Dialog dialog = builder.create(); @@ -84,19 +91,95 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity { public void onClick(View v) { messageLabel.setVisibility(View.VISIBLE); messageLabel.setText(R.string.loading); - watcher.afterTextChanged(null); + new CheckLogin().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + new Tuple3<>( + loginUsernameText.getText().toString(), + loginPasswordText.getText().toString(), + new Function() { + public void apply(String token) { + if (token == null) { + messageLabel.setText(R.string.invalid_user_pass); + } else { + DeckPreviewActivity.this.setToken(token); + final Button loginToProgramakerButton = findViewById(R.id.login_in_programaker_button); + + loginToProgramakerButton.setVisibility(View.GONE); + // Re-check... just in case + new CheckNeededLoginButton().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + new Tuple2<>(DeckPreviewActivity.this.ProgramakerApi, + loginToProgramakerButton)); + dialog.cancel(); + } + } + })); } }); } + private void setToken(String token) { + SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE); + SharedPreferences.Editor edit = preferences.edit(); + + edit.putString(TOKEN_KEY, token); + edit.commit(); + + this.ProgramakerApi.setToken(token); + } + + private String getToken() { + SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE); + if (!preferences.contains(TOKEN_KEY)) { + return null; + } + else { + return preferences.getString(TOKEN_KEY, null); + } + } + + static class CheckLogin extends AsyncTask>, + Void, Tuple2>>{ + @Override + protected Tuple2> doInBackground(Tuple3>... tuples) { + ProgramakerApi api = new ProgramakerApi(); + boolean logged = false; + String token = null; + try { + token = api.login(tuples[0]._x, tuples[0]._y); + } + catch (Exception e) { + Log.e("Login to PrograMaker", e.toString()); + } + return new Tuple2>(token, tuples[0]._z); + } + + @Override + protected void onPostExecute(Tuple2> result) { + result.item2.apply(result.item1); + } + } + + static class CheckNeededLoginButton extends AsyncTask, Void, Tuple2>{ + @Override + protected Tuple2 doInBackground(Tuple2... tuples) { + return new Tuple2<>(tuples[0].item1.check(), tuples[0].item2); + } + + @Override + protected void onPostExecute(Tuple2 result) { + if (!result.item1) { + result.item2.setVisibility(View.VISIBLE); + } + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_deck_preview); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.create_new_card_fab); + FloatingActionButton fab = findViewById(R.id.create_new_card_fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -104,7 +187,7 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity { } }); - Button loginButton = (Button) findViewById(R.id.login_in_programaker_button); + final Button loginButton = findViewById(R.id.login_in_programaker_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -112,18 +195,19 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity { } }); - listView = (ListView) findViewById(R.id.card_deck_list); + 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); + String token = getToken(); + if (token == null) { + loginButton.setVisibility(View.VISIBLE); + } + else { + loginButton.setVisibility(View.GONE); + this.ProgramakerApi.setToken(token); + // Double check that is not needed + new CheckNeededLoginButton().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + new Tuple2<>(this.ProgramakerApi, loginButton)); + } } @Override @@ -145,7 +229,7 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity { 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 EditText cardNameEditText = openCardOptions.findViewById(R.id.card_name_edit_text); builder.setTitle("Create a new card") .setView(openCardOptions) diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java b/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java index 9328e4d..830195e 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java @@ -2,7 +2,7 @@ package com.codigoparallevar.minicards; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; public abstract class PartInstantiator { protected abstract Part instantiate(PartGrid grid, Tuple2 center); diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java index 375e96a..ca142e6 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java @@ -12,7 +12,7 @@ import com.codigoparallevar.minicards.parts.logic.Toggle; import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.strings.ConvertToString; import com.codigoparallevar.minicards.types.Part; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import java.util.List; import java.util.Vector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java b/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java index 28c8ed0..90a3858 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java +++ b/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java @@ -6,7 +6,7 @@ import android.graphics.Path; import android.graphics.Rect; import androidx.annotation.NonNull; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; public class ScrolledCanvas { private final Canvas canvas; diff --git a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java index fab7739..6649ed3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java @@ -2,7 +2,7 @@ package com.codigoparallevar.minicards; import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Selectable; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java index f545071..d6f9b38 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java @@ -11,7 +11,7 @@ import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java index b1b3969..79febf7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java @@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.Wiring.BooleanWire; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java index d6c19a1..94956bb 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java @@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.Wiring.SignalWire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java index 8dfb040..323db01 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java @@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.Wiring.StringWire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java index 8b34e01..4629bc3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java @@ -12,7 +12,7 @@ import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java index a8e9a57..4d7132f 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java @@ -13,7 +13,7 @@ import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java index 6c52a5c..57613f6 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java @@ -11,7 +11,7 @@ import com.codigoparallevar.minicards.parts.connectors.BooleanRoundInputConnecto import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java index f7a1311..19535b4 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java @@ -14,7 +14,7 @@ import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java index b0a0161..60061ca 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -3,6 +3,7 @@ package com.codigoparallevar.minicards.types; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.functional.Tuple2; public interface PartGrid { Selectable getPartOn(int x, int y); diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java index 4970793..ea6773c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java @@ -3,7 +3,7 @@ package com.codigoparallevar.minicards.types.connectors.output; import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Selectable; -import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.wireData.WireDataType; diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/functional/Function.java b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Function.java new file mode 100644 index 0000000..d9ab706 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Function.java @@ -0,0 +1,5 @@ +package com.codigoparallevar.minicards.types.functional; + +public abstract class Function { + public abstract void apply(T param); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple2.java similarity index 76% rename from app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java rename to app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple2.java index 081350a..9835e58 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple2.java @@ -1,4 +1,4 @@ -package com.codigoparallevar.minicards.types; +package com.codigoparallevar.minicards.types.functional; public class Tuple2 { public final T1 item1; diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple3.java b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple3.java new file mode 100644 index 0000000..6a5c416 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple3.java @@ -0,0 +1,19 @@ +package com.codigoparallevar.minicards.types.functional; + +public class Tuple3 { + + public final T _x; + public final T1 _y; + public final T2 _z; + + public Tuple3(T x, T1 y, T2 z) { + _x = x; + _y = y; + _z = z; + } + + @Override + public String toString(){ + return "(" + _x + " - " + _y + ", " + _z + ")"; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple4.java similarity index 87% rename from app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java rename to app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple4.java index 3f10b97..43eb6d1 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Tuple4.java @@ -1,4 +1,4 @@ -package com.codigoparallevar.minicards.types; +package com.codigoparallevar.minicards.types.functional; public class Tuple4 { diff --git a/app/src/main/java/com/programaker/api/ProgramakerApi.kt b/app/src/main/java/com/programaker/api/ProgramakerApi.kt index e664944..f1b3f88 100644 --- a/app/src/main/java/com/programaker/api/ProgramakerApi.kt +++ b/app/src/main/java/com/programaker/api/ProgramakerApi.kt @@ -1,24 +1,25 @@ package com.programaker.api import android.os.Build -import android.util.JsonReader import android.util.Log import com.google.gson.Gson -import com.google.gson.GsonBuilder -import java.io.* +import org.json.JSONObject +import java.io.DataOutputStream +import java.io.FileNotFoundException +import java.io.InputStream +import java.io.InputStreamReader import java.lang.reflect.Type import java.net.HttpURLConnection import java.net.URL import java.net.URLConnection -import javax.net.ssl.HttpsURLConnection class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") { private val LogTag: String = "ProgramakerApi" - private val TokenEntry: String = "ProgramakerAuthToken" + var token: String? = null // API - fun Check(): Boolean { + fun check(): Boolean { val conn = URL(getCheckUrl()).openConnection() as HttpURLConnection if (conn == null){ Log.e(LogTag, "URL Connection not established, set to NULL") @@ -36,10 +37,34 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") } catch(ex: JsonParseException) { ex.logError(LogTag) return false + } catch (ex: FileNotFoundException) { + ex.logError(LogTag) + return false } return result.success } + fun login(user: String, password: String): String { + val conn = URL(getLoginUrl()).openConnection() as HttpURLConnection + conn.setRequestProperty("Content-Type", "application/json") + + conn.requestMethod = "POST"; + conn.doOutput = true; + + val postData = JSONObject() + postData.put("username", user) + postData.put("password", password) + + val wr = DataOutputStream(conn.outputStream) + wr.writeBytes(postData.toString()); + wr.flush(); + wr.close(); + + val result: ProgramakerLoginResult + result = parseJson(conn.inputStream, ProgramakerLoginResult::class.java) + return result.token + } + // Initialization init { // Disable connection reuse if necessary @@ -51,11 +76,20 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") // Private functions private fun getCheckUrl(): String { - return "$ApiRoot/v0/ping" + return "$ApiRoot/v0/sessions/check" + } + + private fun getLoginUrl(): String { + return "$ApiRoot/v0/sessions/login" } private fun addAuthHeader(conn: URLConnection) { - // TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + if (token != null) { + conn.setRequestProperty("Authorization", token) + } + else { + throw TokenNotFoundException() + } } private fun parseJson(content: InputStream, resultClass: Type): T { @@ -65,6 +99,10 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") } } +private fun FileNotFoundException.logError(tag: String) { + Log.e(tag, "Cannot open: ${this.message}") +} + class JsonParseException(private val content: String, private val cls: Type) : Exception() { fun logError(tag: String) { Log.e(tag, "Cannot JSON parse: ${this.content} as ${this.cls}") diff --git a/app/src/main/java/com/programaker/api/ProgramakerLoginResult.kt b/app/src/main/java/com/programaker/api/ProgramakerLoginResult.kt new file mode 100644 index 0000000..e60b218 --- /dev/null +++ b/app/src/main/java/com/programaker/api/ProgramakerLoginResult.kt @@ -0,0 +1,5 @@ +package com.programaker.api + +class ProgramakerLoginResult(val user_id: String, val token: String, val success: Boolean) { + +} diff --git a/app/src/main/java/com/programaker/api/ProgramakerNetworkException.kt b/app/src/main/java/com/programaker/api/ProgramakerNetworkException.kt new file mode 100644 index 0000000..a2c3ee1 --- /dev/null +++ b/app/src/main/java/com/programaker/api/ProgramakerNetworkException.kt @@ -0,0 +1,5 @@ +package com.programaker.api + +class ProgramakerNetworkException : Throwable() { + +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 301e576..07d416e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,4 +14,5 @@ Username Login Loading... + Invalid username/password