Add support for fetching custom blocks.
This commit is contained in:
parent
cca4b70b90
commit
8507e868d8
20 changed files with 269 additions and 52 deletions
|
@ -2,15 +2,27 @@ package com.codigoparallevar.minicards;
|
|||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.codigoparallevar.minicards.types.functional.Consumer;
|
||||
import com.codigoparallevar.minicards.types.functional.Producer;
|
||||
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
||||
import com.codigoparallevar.minicards.ui_helpers.GetAsync;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.programaker.api.ProgramakerApi;
|
||||
import com.programaker.api.data.ProgramakerCustomBlock;
|
||||
import com.programaker.api.data.api_results.ProgramakerGetCustomBlocksResult;
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class CardActivity extends AppCompatActivity {
|
||||
|
||||
|
@ -19,7 +31,6 @@ public class CardActivity extends AppCompatActivity {
|
|||
public static final String VISUALIZATION_MODE_KEY = "VISUALIZATION_MODE";
|
||||
public static final String DEVELOPER_VISUALIZATION_MODE = "DEVELOPER_VISUALIZATION_MODE";
|
||||
|
||||
|
||||
CanvasView canvasView;
|
||||
com.getbase.floatingactionbutton.AddFloatingActionButton AddPartButton;
|
||||
com.getbase.floatingactionbutton.FloatingActionButton SetDevModeButton;
|
||||
|
@ -34,11 +45,39 @@ public class CardActivity extends AppCompatActivity {
|
|||
boolean devMode = false;
|
||||
FloatingActionButton removePartFab;
|
||||
private PartsHolder partsHolder;
|
||||
private ProgramakerApi ProgramakerApi;
|
||||
private ConfigManager Config;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
this.Config = new ConfigManager(this);
|
||||
this.ProgramakerApi = new ProgramakerApi();
|
||||
String token = this.Config.getToken();
|
||||
if (token != null) {
|
||||
this.ProgramakerApi.setToken(token);
|
||||
}
|
||||
|
||||
new GetAsync<ProgramakerGetCustomBlocksResult>().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
||||
new Tuple2<>(new Producer<ProgramakerGetCustomBlocksResult>() {
|
||||
@Override
|
||||
public ProgramakerGetCustomBlocksResult get() {
|
||||
return CardActivity.this.ProgramakerApi.fetchCustomBlocks();
|
||||
}
|
||||
}, new Consumer<ProgramakerGetCustomBlocksResult>() {
|
||||
@Override
|
||||
public void apply(ProgramakerGetCustomBlocksResult result) {
|
||||
if (result == null) {
|
||||
Log.e("CARDActivity", "error retrieving custom blocks");
|
||||
}
|
||||
else {
|
||||
Log.d("CARDActivity", "custom blocks: " + result.toString());
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
// Hide action bar
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.codigoparallevar.minicards;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.view.View;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
public class ConfigManager {
|
||||
private final Context context;
|
||||
private static final String TOKEN_KEY = "PROGRAMAKER_API_TOKEN";
|
||||
private static final String PREFERENCES_NAME = "MINICARDS_PREFERENCES";
|
||||
|
||||
public ConfigManager(Context ctx) {
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
SharedPreferences preferences = this.context.getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
|
||||
SharedPreferences.Editor edit = preferences.edit();
|
||||
|
||||
edit.putString(TOKEN_KEY, token);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
SharedPreferences preferences = this.context.getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
|
||||
if (!preferences.contains(TOKEN_KEY)) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return preferences.getString(TOKEN_KEY, null);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,11 +2,10 @@ 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.Consumer;
|
||||
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
||||
import com.codigoparallevar.minicards.types.functional.Tuple3;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
|
@ -32,13 +31,11 @@ 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;
|
||||
private CardPreviewArrayAdapter cardArrayAdapter;
|
||||
private ProgramakerApi ProgramakerApi = new ProgramakerApi();
|
||||
private ProgramakerApi ProgramakerApi;
|
||||
private ConfigManager Config;
|
||||
|
||||
protected void openLoginDialog(View view) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
|
@ -95,12 +92,13 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
new Tuple3<>(
|
||||
loginUsernameText.getText().toString(),
|
||||
loginPasswordText.getText().toString(),
|
||||
new Function<String>() {
|
||||
new Consumer<String>() {
|
||||
public void apply(String token) {
|
||||
if (token == null) {
|
||||
messageLabel.setText(R.string.invalid_user_pass);
|
||||
} else {
|
||||
DeckPreviewActivity.this.setToken(token);
|
||||
DeckPreviewActivity.this.Config.setToken(token);
|
||||
DeckPreviewActivity.this.ProgramakerApi.setToken(token);
|
||||
final Button loginToProgramakerButton = findViewById(R.id.login_in_programaker_button);
|
||||
|
||||
loginToProgramakerButton.setVisibility(View.GONE);
|
||||
|
@ -116,30 +114,10 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
});
|
||||
}
|
||||
|
||||
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>>>{
|
||||
static class CheckLogin extends AsyncTask<Tuple3<String, String, Consumer<String>>,
|
||||
Void, Tuple2<String, Consumer<String>>>{
|
||||
@Override
|
||||
protected Tuple2<String, Function<String>> doInBackground(Tuple3<String, String, Function<String>>... tuples) {
|
||||
protected Tuple2<String, Consumer<String>> doInBackground(Tuple3<String, String, Consumer<String>>... tuples) {
|
||||
ProgramakerApi api = new ProgramakerApi();
|
||||
boolean logged = false;
|
||||
String token = null;
|
||||
|
@ -149,11 +127,11 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
catch (Exception e) {
|
||||
Log.e("Login to PrograMaker", e.toString());
|
||||
}
|
||||
return new Tuple2<String, Function<String>>(token, tuples[0]._z);
|
||||
return new Tuple2<String, Consumer<String>>(token, tuples[0]._z);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Tuple2<String, Function<String>> result) {
|
||||
protected void onPostExecute(Tuple2<String, Consumer<String>> result) {
|
||||
result.item2.apply(result.item1);
|
||||
}
|
||||
}
|
||||
|
@ -175,6 +153,10 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
this.Config = new ConfigManager(this);
|
||||
this.ProgramakerApi = new ProgramakerApi();
|
||||
|
||||
setContentView(R.layout.activity_deck_preview);
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
@ -196,17 +178,16 @@ public class DeckPreviewActivity extends ReloadableAppCompatActivity {
|
|||
});
|
||||
|
||||
listView = findViewById(R.id.card_deck_list);
|
||||
|
||||
String token = getToken();
|
||||
String token = this.Config.getToken();
|
||||
if (token == null) {
|
||||
loginButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
else {
|
||||
loginButton.setVisibility(View.GONE);
|
||||
this.ProgramakerApi.setToken(token);
|
||||
// Double check that is not needed
|
||||
loginButton.setVisibility(View.GONE);
|
||||
// Double check that is not needed, token might have been deleted
|
||||
new CheckNeededLoginButton().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
||||
new Tuple2<>(this.ProgramakerApi, loginButton));
|
||||
new Tuple2<>(this.ProgramakerApi, loginButton));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package com.codigoparallevar.minicards.types.functional;
|
||||
|
||||
public abstract class Function<T> {
|
||||
public abstract class Consumer<T> {
|
||||
public abstract void apply(T param);
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.codigoparallevar.minicards.types.functional;
|
||||
|
||||
public abstract class Producer<T> {
|
||||
public abstract T get();
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.codigoparallevar.minicards.ui_helpers;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import com.codigoparallevar.minicards.types.functional.Consumer;
|
||||
import com.codigoparallevar.minicards.types.functional.Producer;
|
||||
import com.codigoparallevar.minicards.types.functional.Tuple2;
|
||||
|
||||
public class GetAsync<T> extends AsyncTask<Tuple2<Producer<T>, Consumer<T>>,
|
||||
Void,
|
||||
Tuple2<T, Consumer<T>>> {
|
||||
|
||||
@Override
|
||||
protected Tuple2<T, Consumer<T>> doInBackground(Tuple2<Producer<T>, Consumer<T>>... data) {
|
||||
try {
|
||||
return new Tuple2<>(data[0].item1.get(), data[0].item2);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Log.e("GetAsync", ex.toString());
|
||||
return new Tuple2<>(null, data[0].item2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Tuple2<T, Consumer<T>> result) {
|
||||
result.item2.apply(result.item1);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue