Commit 7db0cc34 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'feature/ArticleActivities' into develop

parents 6e29c9f4 ff68cbc7
...@@ -3,7 +3,10 @@ package fr.utc.simde.payutc; ...@@ -3,7 +3,10 @@ package fr.utc.simde.payutc;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost; import android.widget.TabHost;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -12,7 +15,7 @@ import java.util.ArrayList; ...@@ -12,7 +15,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import fr.utc.simde.payutc.fragments.ArticleGroupFragment; import fr.utc.simde.payutc.articles.ArticleGroupFragment;
/** /**
* Created by Samy on 27/10/2017. * Created by Samy on 27/10/2017.
...@@ -21,18 +24,64 @@ import fr.utc.simde.payutc.fragments.ArticleGroupFragment; ...@@ -21,18 +24,64 @@ import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
public class ArticleCategoryActivity extends BaseActivity { public class ArticleCategoryActivity extends BaseActivity {
private static final String LOG_TAG = "_ArticleCategoryActivit"; private static final String LOG_TAG = "_ArticleCategoryActivit";
private TabHost categoryTabList; private ImageButton paramButton;
private ImageButton deleteButton;
private TabHost tabHost;
private Panier panier;
private List<ArticleGroupFragment> articleGroupFragmentList;
private int nbrCategories; private int nbrCategories;
public class Panier {
private int totalPrice;
private List<Integer> articleList = new ArrayList<Integer>();
private TextView textView;
public Panier(TextView textView) {
this.totalPrice = 0;
this.textView = textView;
setText();
}
public void setText() {
if (this.totalPrice == 0)
this.textView.setText("Panier vide");
else
this.textView.setText("Total: " + String.format("%.2f", new Float(totalPrice) / 100.00f) + "€");
}
public void addArticle(final int id, final int price) {
this.articleList.add(id);
this.totalPrice += price;
setText();
}
public void clear() {
this.articleList.clear();
this.totalPrice = 0;
setText();
}
}
@Override @Override
public void onCreate (Bundle savedInstanceState) { public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView (R.layout.activity_articles_category); setContentView(R.layout.activity_article_category);
this.nbrCategories = 0; TextView textView = findViewById(R.id.text_price);
this.panier = new Panier(textView);
this.paramButton = findViewById(R.id.image_param);
this.deleteButton = findViewById(R.id.image_delete);
this.tabHost = findViewById(R.id.tab_categories);
this.tabHost.setup();
this.categoryTabList = findViewById(R.id.tab_categories); this.articleGroupFragmentList = new ArrayList<ArticleGroupFragment>();
this.categoryTabList.setup(); this.nbrCategories = 0;
try { try {
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList"))); createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList")));
...@@ -54,6 +103,16 @@ public class ArticleCategoryActivity extends BaseActivity { ...@@ -54,6 +103,16 @@ public class ArticleCategoryActivity extends BaseActivity {
} }
}); });
} }
this.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (ArticleGroupFragment articleGroupFragment : articleGroupFragmentList)
articleGroupFragment.clear();
panier.clear();
}
});
} }
@Override @Override
...@@ -89,11 +148,15 @@ public class ArticleCategoryActivity extends BaseActivity { ...@@ -89,11 +148,15 @@ public class ArticleCategoryActivity extends BaseActivity {
} }
protected void createNewCategory(final String name, final JsonNode articleList) throws Exception { protected void createNewCategory(final String name, final JsonNode articleList) throws Exception {
TabHost.TabSpec newTabSpec = this.categoryTabList.newTabSpec(name); ArticleGroupFragment articleGroupFragment = new ArticleGroupFragment(ArticleCategoryActivity.this, articleList, this.panier);
TabHost.TabSpec newTabSpec = this.tabHost.newTabSpec(name);
newTabSpec.setIndicator(name); newTabSpec.setIndicator(name);
newTabSpec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this, articleList)); newTabSpec.setContent(articleGroupFragment);
this.articleGroupFragmentList.add(articleGroupFragment);
this.categoryTabList.addTab(newTabSpec); this.tabHost.addTab(newTabSpec);
nbrCategories++; nbrCategories++;
} }
} }
package fr.utc.simde.payutc.fragments; package fr.utc.simde.payutc.articles;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import fr.utc.simde.payutc.R; import fr.utc.simde.payutc.R;
import fr.utc.simde.payutc.tools.HTTPRequest; import fr.utc.simde.payutc.tools.HTTPRequest;
import static android.content.ContentValues.TAG;
/** /**
* Created by Samy on 28/10/2017. * Created by Samy on 28/10/2017.
*/ */
public class ArticleFragment extends View { public class ArticleAdapter extends BaseAdapter {
private static final String LOG_TAG = "_ArticleFragment"; private static final String LOG_TAG = "_ArticleAdapter";
private Activity activity;
private JsonNode articleList;
private Bitmap[] imageList;
private Integer[] nbrClicksList;
private TextView[] clickViewList;
private int size;
public ArticleAdapter(final Activity activity, final JsonNode articleList, final int nbrColumns) throws Exception {
this.activity = activity;
this.articleList = articleList;
this.imageList = new Bitmap[articleList.size()];
this.nbrClicksList = new Integer[articleList.size()];
this.clickViewList = new TextView[articleList.size()];
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;
}
for (int i = 0; i < this.nbrClicksList.length; i++)
this.nbrClicksList[i] = 0;
}
private int id; @Override
private int price; public int getCount() {
private String name; return this.articleList.size();
private String imageUrl; }
private LayoutInflater layoutInflater; @Override
private View view; public Object getItem(int position) {
private TextView textView; return getArticle(position);
}
private ImageView iv; @Override
private HTTPRequest request; public long getItemId(int position) {
return 0;
}
private LinearLayout linearLayout; @Override
public View getView(int position, View view, ViewGroup viewGroup) {
JsonNode article = this.articleList.get(position);
public ArticleFragment(final Activity activity, final JsonNode article) { if (view == null) {
super(activity); LayoutInflater layoutInflater = LayoutInflater.from(this.activity);
/* view = layoutInflater.inflate(R.layout.fragment_article, null);
this.layoutInflater = LayoutInflater.from(activity); }
this.view = this.layoutInflater.inflate(R.layout.fragment_article, null);
this.textView = view.findViewById(R.id.text_article);
this.textView.setText(article.get("name").textValue());*/
this.id = article.get("id").intValue(); ImageView imageView = view.findViewById(R.id.image_article);
this.price = article.get("price").intValue();
this.name = article.get("name").textValue();
this.imageUrl = article.get("image_url").textValue();
setView(activity); if (clickViewList[position] == null)
clickViewList[position] = view.findViewById(R.id.text_nbr_clicks);
TextView textView = view.findViewById(R.id.text_article);
int imageSize = this.size;
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(imageSize, imageSize);
imageView.setLayoutParams(parms);
setImage(imageView, article.get("image_url").textValue(), position);
setClickView(position);
textView.setText(article.get("name").textValue());
return view;
} }
public void setView(final Activity activity) { public void setClickView(final int position) {
TextView t = new TextView(activity); if (this.clickViewList[position] != null) {
iv = new ImageView(activity); if (this.nbrClicksList[position] == 0) {
iv.setTag("image_article_" + Integer.toString(this.id)); this.clickViewList[position].setText("");
iv.setImageResource(R.mipmap.ic_launcher); this.clickViewList[position].setAlpha(0.0f);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200,200); }
iv.setLayoutParams(parms); else {
this.clickViewList[position].setText(Integer.toString(this.nbrClicksList[position]));
t.setText(this.name + ": "); this.clickViewList[position].setAlpha(1.0f);
TextView t2 = new TextView(activity); }
t2.setText((this.price / 100) + "€" + ((this.price % 100) == 0 ? "" : (((this.price % 100) < 10 ? "0" : "") + (this.price % 100)))); }
this.linearLayout = new LinearLayout(activity); }
linearLayout.addView(iv); public void onClick(final int position) {
linearLayout.addView(t); this.nbrClicksList[position]++;
linearLayout.addView(t2); setClickView(position);
}
if (this.imageUrl != null && !this.imageUrl.equals("")) {
new Thread(){ 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 @Override
public void run() { public void run() {
request = new HTTPRequest(imageUrl); request[0] = new HTTPRequest(url);
if (request.get() == 200) { if (request[0].get() == 200) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
iv.setImageBitmap(request.getImageResponse()); imageList[position] = request[0].getImageResponse();
imageView.setImageBitmap(imageList[position]);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -102,16 +164,18 @@ public class ArticleFragment extends View { ...@@ -102,16 +164,18 @@ public class ArticleFragment extends View {
}); });
} }
} }
}.start(); }.start();*/
//new DownloadImageTask(iv).execute(this.imageUrl); new DownloadImageTask(imageView, imageList[position]).execute(url);
} }
} }
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView; private ImageView imageView;
private Bitmap image; private Bitmap image;
public DownloadImageTask(ImageView imageView) { public DownloadImageTask(ImageView imageView, Bitmap image) {
this.imageView = imageView; this.imageView = imageView;
this.image = image;
} }
protected Bitmap doInBackground(String... urls) { protected Bitmap doInBackground(String... urls) {
...@@ -136,8 +200,4 @@ public class ArticleFragment extends View { ...@@ -136,8 +200,4 @@ public class ArticleFragment extends View {
} }
} }
} }
public View getView() {
return this.linearLayout;
}
} }
package fr.utc.simde.payutc.fragments; package fr.utc.simde.payutc.articles;
import android.app.Activity; import android.app.Activity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.GridLayout; import android.widget.AdapterView;
import android.widget.ImageView; import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TabHost; import android.widget.TabHost;
import android.widget.TextView; import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import fr.utc.simde.payutc.ArticleCategoryActivity;
import fr.utc.simde.payutc.R; import fr.utc.simde.payutc.R;
/** /**
...@@ -24,32 +23,56 @@ public class ArticleGroupFragment implements TabHost.TabContentFactory { ...@@ -24,32 +23,56 @@ public class ArticleGroupFragment implements TabHost.TabContentFactory {
private LayoutInflater layoutInflater; private LayoutInflater layoutInflater;
private View view; private View view;
private ScrollView scrollView; private GridView gridView;
private GridLayout gridView; private ArticleAdapter articleAdapter;
public ArticleGroupFragment(final Activity activity, final JsonNode articleList) throws Exception { private int nbrColumns;
private ArticleCategoryActivity.Panier panier;
public ArticleGroupFragment(final Activity activity, final JsonNode articleList, ArticleCategoryActivity.Panier panier) throws Exception {
this.layoutInflater = LayoutInflater.from(activity); this.layoutInflater = LayoutInflater.from(activity);
this.view = this.layoutInflater.inflate(R.layout.fragment_article_group, null); this.view = this.layoutInflater.inflate(R.layout.fragment_article_group, null);
this.gridView = this.view.findViewById(R.id.grid_articles); this.gridView = this.view.findViewById(R.id.grid_articles);
this.panier = panier;
setGridLayout(3);
createArticles(activity, articleList); createArticles(activity, articleList);
gridView.setAdapter(this.articleAdapter);
}
public void setGridLayout(final int nbrColumns) {
this.nbrColumns = nbrColumns;
gridView.setNumColumns(nbrColumns);
} }
public void createArticles(final Activity activity, final JsonNode articleList) throws Exception { public void createArticles(final Activity activity, final JsonNode articleList) throws Exception {
LinearLayout linearLayout = new LinearLayout(activity); this.articleAdapter = new ArticleAdapter(activity, articleList, this.nbrColumns);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for (JsonNode article : articleList) { gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
if (!article.has("id") || !article.has("price") || !article.has("name") || !article.has("active") || !article.has("cotisant") || !article.has("alcool") || !article.has("categorie_id") || !article.has("image_url") || !article.has("fundation_id")) @Override
throw new Exception("Unexpected JSON"); public void onItemClick(AdapterView parent, View view, int position, long id) {
JsonNode article = ((JsonNode) articleAdapter.getArticle(position));
articleAdapter.onClick(position);
panier.addArticle(article.get("id").intValue(), article.get("price").intValue());
}
});
if (!article.get("active").booleanValue()) gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
continue; @Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
JsonNode article = ((JsonNode) articleAdapter.getArticle(position));
Toast.makeText(activity, article.get("name").textValue() + ": " + String.format("%.2f", new Float(article.get("price").intValue()) / 100.00f) + "€", Toast.LENGTH_LONG).show();
linearLayout.addView(new ArticleFragment(activity, article).getView()); return false;
} }
});
}
this.gridView.addView(linearLayout); public void clear() {
articleAdapter.clear();
} }
@Override @Override
......
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="@color/add" />
<stroke
android:width="2dp"
android:color="@color/white" />
<corners
android:radius="2dp" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"