Commit efcf3bb3 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Option preparation

Refract many files
Add Options view
parent d359d8ce
......@@ -27,7 +27,7 @@
<activity android:name=".BaseActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".FoundationListActivity"
<activity android:name=".FoundationsOptionsActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ArticleKeyboardActivity"
......
......@@ -23,8 +23,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import java.util.List;
import fr.utc.simde.payutc.articles.GroupAdapter;
import fr.utc.simde.payutc.articles.GroupFragment;
import fr.utc.simde.payutc.adapters.GroupAdapter;
import fr.utc.simde.payutc.fragments.GroupFragment;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.Panier;
......
......@@ -3,16 +3,10 @@ package fr.utc.simde.payutc;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -22,7 +16,6 @@ import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Config;
import fr.utc.simde.payutc.tools.Dialog;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.InternetBroadcast;
import fr.utc.simde.payutc.tools.NemopaySession;
/**
......@@ -144,7 +137,7 @@ public abstract class BaseActivity extends NFCActivity {
}
dialog.startLoading(activity, getString(R.string.information_collection), getString(R.string.foundation_list_collecting));
final Intent intent = new Intent(activity, FoundationListActivity.class);
final Intent intent = new Intent(activity, FoundationsOptionsActivity.class);
new Thread() {
@Override
......@@ -195,7 +188,7 @@ public abstract class BaseActivity extends NFCActivity {
public void run() {
dialog.stopLoading();
if (activity.getClass().getSimpleName().equals("FoundationListActivity"))
if (activity.getClass().getSimpleName().equals("FoundationsOptionsActivity"))
finish();
activity.startActivity(intent);
......
package fr.utc.simde.payutc;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
......@@ -18,12 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.w3c.dom.Text;
import java.io.IOException;
import fr.utc.simde.payutc.articles.GridAdapter;
import fr.utc.simde.payutc.articles.ListAdapater;
import fr.utc.simde.payutc.adapters.ListAdapater;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
......
package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import fr.utc.simde.payutc.adapters.FoundationsAdapter;
import fr.utc.simde.payutc.adapters.ListAdapater;
/**
* Created by Samy on 26/10/2017.
*/
public class FoundationsOptionsActivity extends BaseActivity {
private static final String LOG_TAG = "_FoundationsOptionsActivity";
TabHost tabHost;
ListView foundationList;
ListView optionList;
FoundationsAdapter foundationsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foundations_options);
this.tabHost = findViewById(R.id.tabs_foundations_options);
this.foundationList = findViewById(R.id.list_foundations);
this.optionList = findViewById(R.id.list_options);
this.tabHost.setup();
this.tabHost.addTab(this.tabHost.newTabSpec(getString(R.string.foundations)).setIndicator(getString(R.string.foundations)).setContent(R.id.list_foundations));
this.tabHost.addTab(this.tabHost.newTabSpec(getString(R.string.options)).setIndicator(getString(R.string.options)).setContent(R.id.list_options));
try {
setFoundationList((ArrayNode) new ObjectMapper().readTree(getIntent().getExtras().getString("foundationList")));
} catch (Exception e) {
Log.wtf(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.error_unexpected), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
finish();
}
});
}
}
@Override
protected void onIdentification(final String badgeId) {}
protected void setFoundationList(final ArrayNode foundationList) throws Exception {
/*ArrayList<String> arrlist = new ArrayList<String>();
for (final JsonNode foundation : foundationList) {
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
arrlist.add(foundation.get("name").textValue());
}
this.foundationList.setAdapter(new ArrayAdapter<String>(FoundationsOptionsActivity.this, R.layout.fragment_list, arrlist));
*/
this.foundationsAdapter = new FoundationsAdapter(FoundationsOptionsActivity.this, foundationList);
this.foundationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
startArticlesActivity(FoundationsOptionsActivity.this, foundationsAdapter.getFoundationId(position), foundationsAdapter.getFoundationName(position));
}
});
this.foundationList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
foundationsAdapter.toast(position);
return true;
}
});
this.foundationList.setAdapter(this.foundationsAdapter);
}
}
package fr.utc.simde.payutc.adapters;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import fr.utc.simde.payutc.ArticleCategoryActivity;
import fr.utc.simde.payutc.R;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
* Created by Samy on 28/10/2017.
*/
public abstract class ArticlesAdapter extends BaseAdapter {
private static final String LOG_TAG = "_ArticlesAdapter";
protected Activity activity;
protected Boolean printCotisant;
protected Boolean print18;
protected Bitmap[] imageList;
protected Integer[] nbrClicksList;
protected View[] viewList;
protected TextView[] clickViewList;
protected ArrayNode articleList;
public ArticlesAdapter(final Activity activity, final ArrayNode articleList, final Boolean printCotisant, final Boolean print18) throws Exception {
this.activity = activity;
this.printCotisant = printCotisant;
this.print18 = print18;
this.articleList = articleList;
this.imageList = new Bitmap[articleList.size()];
this.nbrClicksList = new Integer[articleList.size()];
this.viewList = new View[articleList.size()];
this.clickViewList = new TextView[articleList.size()];
for (int i = 0; i < this.nbrClicksList.length; i++)
this.nbrClicksList[i] = 0;
}
@Override
public int getCount() {
return this.articleList.size();
}
@Override
public Object getItem(int position) {
return getArticle(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) { return view; }
public void setClickView(final int position) {
if (this.clickViewList[position] != null) {
if (this.nbrClicksList[position] == 0) {
this.clickViewList[position].setText("");
this.clickViewList[position].setAlpha(0.0f);
}
else {
this.clickViewList[position].setText(Integer.toString(this.nbrClicksList[position]));
this.clickViewList[position].setAlpha(1.0f);
}
}
}
public void toast(final int position, int lengthLong) {
Toast.makeText(this.activity, articleList.get(position).get("name").textValue() + ": " + String.format("%.2f", new Float(articleList.get(position).get("price").intValue()) / 100.00f) + "€", lengthLong).show();
}
public void setInfos(JsonNode article, ImageView imageCotisant, ImageView image18) {
if (this.printCotisant) {
if (article.get("cotisant").booleanValue())
imageCotisant.setAlpha(1.0f);
else
imageCotisant.setAlpha(0.3f);
}
else
imageCotisant.setVisibility(View.GONE);
if (this.print18) {
if (article.get("alcool").booleanValue())
image18.setAlpha(1.0f);
else
image18.setAlpha(0.3f);
}
else
image18.setVisibility(View.GONE);
}
public void onClick(final int position) {
this.nbrClicksList[position]++;
setClickView(position);
}
public void clear() {
for (int i = 0; i < getCount(); i++) {
this.nbrClicksList[i] = 0;
setClickView(i);
}
}
public JsonNode getArticle(final int position) {
return this.articleList.get(position);
}
public void setImage(final ImageView imageView, final String url, final int position) {
final HTTPRequest[] request = new HTTPRequest[1];
if (imageList[position] != null)
imageView.setImageBitmap(imageList[position]);
else if (url != null && !url.equals("")) {
new Thread(){
@Override
public void run() {
request[0] = new HTTPRequest(url);
if (request[0].get() == 200) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
imageList[position] = request[0].getImageResponse();
imageView.setImageBitmap(imageList[position]);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}.start();
//new DownloadImageTask(imageView, imageList[position]).execute(url);
}
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Bitmap image;
public DownloadImageTask(ImageView imageView, Bitmap image) {
this.imageView = imageView;
this.image = image;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
try {
URL url = new URL(urldisplay);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
image = BitmapFactory.decodeStream(input);
} catch (Exception e) {
image = null;
}
return image;
}
@SuppressLint("NewApi")
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
}
}
}
}
package fr.utc.simde.payutc.adapters;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.node.ArrayNode;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 28/10/2017.
*/
public class FoundationsAdapter extends BaseAdapter {
private static final String LOG_TAG = "_FoundationsAdapter";
protected Activity activity;
protected ArrayNode foundationList;
public FoundationsAdapter(final Activity activity, final ArrayNode foundationList) throws Exception {
this.activity = activity;
this.foundationList = foundationList;
}
@Override
public int getCount() {
return this.foundationList.size();
}
@Override
public Object getItem(int position) {
return this.foundationList.get(position);
}
@Override
public long getItemId(int position) {
return getFoundationId(position);
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {/*
LinearLayout linearLayout = new LinearLayout(this.activity);
TextView textView = new TextView(this.activity);
textView.setText(getFoundationName(position));
textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
linearLayout.addView(textView);
return linearLayout;*/
LayoutInflater layoutInflater = LayoutInflater.from(this.activity);
view = layoutInflater.inflate(R.layout.fragment_list, null);
TextView textView = view.findViewById(R.id.text_element);
textView.setText(this.foundationList.get(position).get("name").textValue());
return view;
}
public int getFoundationId(int position) {
return this.foundationList.get(position).get("fun_id").intValue();
}
public String getFoundationName(int position) {
return this.foundationList.get(position).get("name").textValue();
}
public void toast(final int position) {
Toast.makeText(this.activity, String.valueOf(this.foundationList.get(position).get("fun_id").intValue()) + ": " + this.foundationList.get(position).get("name").textValue(), Toast.LENGTH_SHORT).show();
}
}
\ No newline at end of file
package fr.utc.simde.payutc.adapters;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import fr.utc.simde.payutc.R;
import fr.utc.simde.payutc.adapters.ArticlesAdapter;
/**
* Created by Samy on 29/10/2017.
*/
public class GridAdapter extends ArticlesAdapter {
private static final String LOG_TAG = "_GridAdapter";
private int size;
public GridAdapter(final Activity activity, final ArrayNode articleList, final int nbrColumns, final Boolean printCotisant, final Boolean print18) throws Exception {
super(activity, articleList, printCotisant, print18);
switch (nbrColumns) {
case 1:
this.size = 250;
break;
case 2:
this.size = 200;
break;
case 3:
this.size = 150;
break;
case 4:
this.size = 125;
break;
case 5:
default:
this.size = 100;
break;
}
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
JsonNode article = this.articleList.get(position);
if (this.viewList[position] == null) {
LayoutInflater layoutInflater = LayoutInflater.from(this.activity);
this.viewList[position] = layoutInflater.inflate(R.layout.fragment_article_grid, null);
ImageView imageView = this.viewList[position].findViewById(R.id.image_article);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(this.size, this.size);
imageView.setLayoutParams(parms);
if (article == null || article.size() == 0)
this.viewList[position].setVisibility(View.INVISIBLE);
else {
this.clickViewList[position] = this.viewList[position].findViewById(R.id.text_nbr_clicks);
TextView textView = this.viewList[position].findViewById(R.id.text_article);
textView.setText(article.get("name").textValue());
ImageView imageCotisant = this.viewList[position].findViewById(R.id.image_cotisant);
ImageView image18 = this.viewList[position].findViewById(R.id.image_18);
LinearLayout.LayoutParams imageParms = new LinearLayout.LayoutParams(45, 45);
imageParms.setMargins(0, this.size - 45, 0, 0);
imageCotisant.setLayoutParams(imageParms);
image18.setLayoutParams(imageParms);
setInfos(article, imageCotisant, image18);
setImage(imageView, article.get("image_url").textValue(), position);
setClickView(position);
}
}
return this.viewList[position];
}
}
package fr.utc.simde.payutc.adapters;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import fr.utc.simde.payutc.ArticleCategoryActivity;