Implement base connection to PrograMaker API.

- Login and check of the login token.
This commit is contained in:
Sergio Martínez Portela 2020-05-19 19:19:26 +02:00
parent 1cdc679c70
commit 033b79cba1
27 changed files with 214 additions and 61 deletions

View File

@ -19,6 +19,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
} }
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
} }
dependencies { dependencies {

View File

@ -17,8 +17,8 @@ import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Position;
import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.functional.Tuple2;
import com.codigoparallevar.minicards.types.Tuple4; import com.codigoparallevar.minicards.types.functional.Tuple4;
import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;

View File

@ -16,7 +16,7 @@ import com.codigoparallevar.minicards.parts.strings.ConvertToString;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;

View File

@ -50,7 +50,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
} }
}); });
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() { settingsButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -58,19 +58,10 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
} }
}); });
CardView cardView = (CardView) row.findViewById(R.id.card_preview_card); CardView cardView = row.findViewById(R.id.card_preview_card);
TextView nameView = (TextView) row.findViewById(R.id.card_preview_name); TextView nameView = row.findViewById(R.id.card_preview_name);
int backgroundColor = card.getColor();
cardView.setBackgroundColor(backgroundColor);
nameView.setText(card.getName()); nameView.setText(card.getName());
if (backgroundColor == CardFile.DEFAULT_BACKGROUND_COLOR){
nameView.setTextColor(0xFFF0F0F0);
}
else {
nameView.setTextColor(0xFFFFFF ^ backgroundColor);
}
return row; return row;
} }
@ -80,7 +71,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
final View openCardOptions = (LayoutInflater.from(getContext()) final View openCardOptions = (LayoutInflater.from(getContext())
.inflate(R.layout.card_settings_dialog, null)); .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()); cardNameEditText.setText(card.getName());
builder.setTitle("Card settings") builder.setTitle("Card settings")
@ -115,7 +106,7 @@ class CardPreviewArrayAdapter extends ArrayAdapter<PreviewCard> {
final Dialog dialog = builder.create(); 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() { deleteCardLink.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {

View File

@ -2,8 +2,13 @@ package com.codigoparallevar.minicards;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; 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 com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
@ -27,6 +32,8 @@ import java.util.Vector;
import com.programaker.api.ProgramakerApi; import com.programaker.api.ProgramakerApi;
public class DeckPreviewActivity extends ReloadableAppCompatActivity { 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"; public static final String INTENT = "com.codigoparallevar.minicards.DECK";
private ListView listView; private ListView listView;
@ -39,11 +46,11 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
final View loginDialog = (LayoutInflater.from(this) final View loginDialog = (LayoutInflater.from(this)
.inflate(R.layout.login_dialog_view, null)); .inflate(R.layout.login_dialog_view, null));
final EditText loginUsernameText = (EditText) loginDialog.findViewById(R.id.login_username_text); final EditText loginUsernameText = loginDialog.findViewById(R.id.login_username_text);
final EditText loginPasswordText = (EditText) loginDialog.findViewById(R.id.login_password_text); final EditText loginPasswordText = loginDialog.findViewById(R.id.login_password_text);
final Button loginButton = (Button) loginDialog.findViewById(R.id.login_dialog_login_button); final Button loginButton = loginDialog.findViewById(R.id.login_dialog_login_button);
final Button cancelButton = (Button) loginDialog.findViewById(R.id.login_dialog_cancel_button); final Button cancelButton = loginDialog.findViewById(R.id.login_dialog_cancel_button);
final TextView messageLabel = (TextView) loginDialog.findViewById(R.id.login_message_label); final TextView messageLabel = loginDialog.findViewById(R.id.login_message_label);
builder.setTitle("Login").setView(loginDialog); builder.setTitle("Login").setView(loginDialog);
final Dialog dialog = builder.create(); final Dialog dialog = builder.create();
@ -84,19 +91,95 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
public void onClick(View v) { public void onClick(View v) {
messageLabel.setVisibility(View.VISIBLE); messageLabel.setVisibility(View.VISIBLE);
messageLabel.setText(R.string.loading); 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<String>() {
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<Tuple3<String, String, Function<String>>,
Void, Tuple2<String, Function<String>>>{
@Override
protected Tuple2<String, Function<String>> doInBackground(Tuple3<String, String, Function<String>>... 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<String, Function<String>>(token, tuples[0]._z);
}
@Override
protected void onPostExecute(Tuple2<String, Function<String>> result) {
result.item2.apply(result.item1);
}
}
static class CheckNeededLoginButton extends AsyncTask<Tuple2<ProgramakerApi, Button>, Void, Tuple2<Boolean, Button>>{
@Override
protected Tuple2<Boolean, Button> doInBackground(Tuple2<ProgramakerApi, Button>... tuples) {
return new Tuple2<>(tuples[0].item1.check(), tuples[0].item2);
}
@Override
protected void onPostExecute(Tuple2<Boolean, Button> result) {
if (!result.item1) {
result.item2.setVisibility(View.VISIBLE);
}
}
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_preview); setContentView(R.layout.activity_deck_preview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(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() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { 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() { loginButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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);
String token = getToken();
new AsyncTask<ProgramakerApi, Void, Boolean>() { if (token == null) {
protected Boolean doInBackground(ProgramakerApi... api) { loginButton.setVisibility(View.VISIBLE);
return api[0].Check(); }
} else {
loginButton.setVisibility(View.GONE);
protected void onPostExecute(Boolean result) { this.ProgramakerApi.setToken(token);
Log.d("MiniCards", "check result: " + result); // Double check that is not needed
} new CheckNeededLoginButton().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this.ProgramakerApi); new Tuple2<>(this.ProgramakerApi, loginButton));
}
} }
@Override @Override
@ -145,7 +229,7 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
final View openCardOptions = (LayoutInflater.from(this) final View openCardOptions = (LayoutInflater.from(this)
.inflate(R.layout.create_new_card_view, null)); .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") builder.setTitle("Create a new card")
.setView(openCardOptions) .setView(openCardOptions)

View File

@ -2,7 +2,7 @@ package com.codigoparallevar.minicards;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.functional.Tuple2;
public abstract class PartInstantiator { public abstract class PartInstantiator {
protected abstract Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center); protected abstract Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center);

View File

@ -12,7 +12,7 @@ import com.codigoparallevar.minicards.parts.logic.Toggle;
import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.strings.ConvertToString; import com.codigoparallevar.minicards.parts.strings.ConvertToString;
import com.codigoparallevar.minicards.types.Part; 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.List;
import java.util.Vector; import java.util.Vector;

View File

@ -6,7 +6,7 @@ import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.functional.Tuple2;
public class ScrolledCanvas { public class ScrolledCanvas {
private final Canvas canvas; private final Canvas canvas;

View File

@ -2,7 +2,7 @@ package com.codigoparallevar.minicards;
import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Selectable; 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.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;

View File

@ -11,7 +11,7 @@ import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; 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.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal; import com.codigoparallevar.minicards.types.wireData.Signal;

View File

@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid; 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.Wiring.BooleanWire;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;

View File

@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid; 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.Wiring.SignalWire;
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;

View File

@ -7,7 +7,7 @@ import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid; 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.Wiring.StringWire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;

View File

@ -12,7 +12,7 @@ import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; 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.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal; import com.codigoparallevar.minicards.types.wireData.Signal;

View File

@ -13,7 +13,7 @@ import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; 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.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;

View File

@ -11,7 +11,7 @@ import com.codigoparallevar.minicards.parts.connectors.BooleanRoundInputConnecto
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid; 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.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;

View File

@ -14,7 +14,7 @@ import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; 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.AnyInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;

View File

@ -3,6 +3,7 @@ package com.codigoparallevar.minicards.types;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.functional.Tuple2;
public interface PartGrid { public interface PartGrid {
Selectable getPartOn(int x, int y); Selectable getPartOn(int x, int y);

View File

@ -3,7 +3,7 @@ package com.codigoparallevar.minicards.types.connectors.output;
import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Selectable; 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.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;

View File

@ -0,0 +1,5 @@
package com.codigoparallevar.minicards.types.functional;
public abstract class Function<T> {
public abstract void apply(T param);
}

View File

@ -1,4 +1,4 @@
package com.codigoparallevar.minicards.types; package com.codigoparallevar.minicards.types.functional;
public class Tuple2<T1, T2> { public class Tuple2<T1, T2> {
public final T1 item1; public final T1 item1;

View File

@ -0,0 +1,19 @@
package com.codigoparallevar.minicards.types.functional;
public class Tuple3<T, T1, T2> {
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 + ")";
}
}

View File

@ -1,4 +1,4 @@
package com.codigoparallevar.minicards.types; package com.codigoparallevar.minicards.types.functional;
public class Tuple4<T, T1, T2, T3> { public class Tuple4<T, T1, T2, T3> {

View File

@ -1,24 +1,25 @@
package com.programaker.api package com.programaker.api
import android.os.Build import android.os.Build
import android.util.JsonReader
import android.util.Log import android.util.Log
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import org.json.JSONObject
import java.io.* import java.io.DataOutputStream
import java.io.FileNotFoundException
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.net.URLConnection import java.net.URLConnection
import javax.net.ssl.HttpsURLConnection
class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") { class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") {
private val LogTag: String = "ProgramakerApi" private val LogTag: String = "ProgramakerApi"
private val TokenEntry: String = "ProgramakerAuthToken" var token: String? = null
// API // API
fun Check(): Boolean { fun check(): Boolean {
val conn = URL(getCheckUrl()).openConnection() as HttpURLConnection val conn = URL(getCheckUrl()).openConnection() as HttpURLConnection
if (conn == null){ if (conn == null){
Log.e(LogTag, "URL Connection not established, set to 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) { } catch(ex: JsonParseException) {
ex.logError(LogTag) ex.logError(LogTag)
return false return false
} catch (ex: FileNotFoundException) {
ex.logError(LogTag)
return false
} }
return result.success 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 // Initialization
init { init {
// Disable connection reuse if necessary // Disable connection reuse if necessary
@ -51,11 +76,20 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api")
// Private functions // Private functions
private fun getCheckUrl(): String { 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) { 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 <T>parseJson(content: InputStream, resultClass: Type): T { private fun <T>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() { class JsonParseException(private val content: String, private val cls: Type) : Exception() {
fun logError(tag: String) { fun logError(tag: String) {
Log.e(tag, "Cannot JSON parse: ${this.content} as ${this.cls}") Log.e(tag, "Cannot JSON parse: ${this.content} as ${this.cls}")

View File

@ -0,0 +1,5 @@
package com.programaker.api
class ProgramakerLoginResult(val user_id: String, val token: String, val success: Boolean) {
}

View File

@ -0,0 +1,5 @@
package com.programaker.api
class ProgramakerNetworkException : Throwable() {
}

View File

@ -14,4 +14,5 @@
<string name="login_username_label">Username</string> <string name="login_username_label">Username</string>
<string name="action_login_short">Login</string> <string name="action_login_short">Login</string>
<string name="loading">Loading...</string> <string name="loading">Loading...</string>
<string name="invalid_user_pass">Invalid username/password</string>
</resources> </resources>