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

GridView implement

Add Gridview management
Add onClick and onLongClick for articles
Handle the panier with clearing
parent 9cc0d29e
......@@ -3,7 +3,10 @@ package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -12,7 +15,7 @@ import java.util.ArrayList;
import java.util.HashMap;
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.
......@@ -21,18 +24,64 @@ import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
public class ArticleCategoryActivity extends BaseActivity {
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;
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
public void onCreate (Bundle 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.categoryTabList.setup();
this.articleGroupFragmentList = new ArrayList<ArticleGroupFragment>();
this.nbrCategories = 0;
try {
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList")));
......@@ -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
......@@ -89,11 +148,15 @@ public class ArticleCategoryActivity extends BaseActivity {
}
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.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this, articleList));
newTabSpec.setContent(articleGroupFragment);
this.articleGroupFragmentList.add(articleGroupFragment);
this.categoryTabList.addTab(newTabSpec);
this.tabHost.addTab(newTabSpec);
nbrCategories++;
}
}
package fr.utc.simde.payutc.fragments;
package fr.utc.simde.payutc.articles;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
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.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import fr.utc.simde.payutc.R;
import fr.utc.simde.payutc.tools.HTTPRequest;
import static android.content.ContentValues.TAG;
/**
* Created by Samy on 28/10/2017.
*/
public class ArticleFragment extends View {
private static final String LOG_TAG = "_ArticleFragment";
public class ArticleAdapter extends BaseAdapter {
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;
private int price;
private String name;
private String imageUrl;
@Override
public int getCount() {
return this.articleList.size();
}
private LayoutInflater layoutInflater;
private View view;
private ImageView imageView;
private TextView textView;
@Override
public Object getItem(int position) {
return getArticle(position);
}
private HTTPRequest request;
@Override
public long getItemId(int position) {
return 0;
}
public ArticleFragment(final Activity activity, final JsonNode article) {
super(activity);
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
JsonNode article = this.articleList.get(position);
this.id = article.get("id").intValue();
this.price = article.get("price").intValue();
this.name = article.get("name").textValue();
this.imageUrl = article.get("image_url").textValue();
if (view == null) {
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.imageView = view.findViewById(R.id.image_article);
this.textView = view.findViewById(R.id.text_article);
ImageView imageView = view.findViewById(R.id.image_article);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(200,200);
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 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);
}
}
}
setView(activity);
public void onClick(final int position) {
this.nbrClicksList[position]++;
setClickView(position);
}
public void setView(final Activity activity) {/*
this.textView.setText(article.get("name").textValue())
TextView t = new TextView(activity);
iv = new ImageView(activity);
iv.setTag("image_article_" + Integer.toString(this.id));
iv.setImageResource(R.mipmap.ic_launcher);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200,200);
iv.setLayoutParams(parms);
t.setText(this.name + ": ");
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);
linearLayout.addView(t);
linearLayout.addView(t2);
*/
if (this.imageUrl != null && !this.imageUrl.equals("")) {
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 = new HTTPRequest(imageUrl);
request[0] = new HTTPRequest(url);
if (request.get() == 200) {
if (request[0].get() == 200) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
imageView.setImageBitmap(request.getImageResponse());
imageList[position] = request[0].getImageResponse();
imageView.setImageBitmap(imageList[position]);
} catch (Exception e) {
e.printStackTrace();
}
......@@ -107,18 +164,18 @@ public class ArticleFragment extends View {
});
}
}
}.start();
//new DownloadImageTask(iv).execute(this.imageUrl);
}.start();*/
new DownloadImageTask(imageView, imageList[position]).execute(url);
}
this.textView.setText(this.name);
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Bitmap image;
public DownloadImageTask(ImageView imageView) {
public DownloadImageTask(ImageView imageView, Bitmap image) {
this.imageView = imageView;
this.image = image;
}
protected Bitmap doInBackground(String... urls) {
......@@ -143,8 +200,4 @@ public class ArticleFragment extends View {
}
}
}
public View getView() {
return this.view;
}
}
package fr.utc.simde.payutc.fragments;
package fr.utc.simde.payutc.articles;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import fr.utc.simde.payutc.ArticleCategoryActivity;
import fr.utc.simde.payutc.R;
/**
......@@ -25,38 +23,56 @@ public class ArticleGroupFragment implements TabHost.TabContentFactory {
private LayoutInflater layoutInflater;
private View view;
private GridLayout gridLayout;
private GridView gridView;
private ArticleAdapter articleAdapter;
private int nbrColumns;
public ArticleGroupFragment(final Activity activity, final JsonNode articleList) throws Exception {
private ArticleCategoryActivity.Panier panier;
public ArticleGroupFragment(final Activity activity, final JsonNode articleList, ArticleCategoryActivity.Panier panier) throws Exception {
this.layoutInflater = LayoutInflater.from(activity);
this.view = this.layoutInflater.inflate(R.layout.fragment_article_group, null);
this.gridLayout = this.view.findViewById(R.id.grid_articles);
this.gridView = this.view.findViewById(R.id.grid_articles);
this.panier = panier;
setGridLayout(3);
createArticles(activity, articleList);
createGroups(activity, articleList);
gridView.setAdapter(this.articleAdapter);
}
public void setGridLayout(final int nbrColumns) {
this.nbrColumns = nbrColumns;
this.gridLayout.setColumnCount(nbrColumns);
gridView.setNumColumns(nbrColumns);
}
public void createGroups(final Activity activity, final JsonNode articleList) throws Exception {
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for (JsonNode article : articleList) {
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"))
throw new Exception("Unexpected JSON");
if (!article.get("active").booleanValue())
continue;
this.gridLayout.addView(new ArticleFragment(activity, article).getView());
}
public void createArticles(final Activity activity, final JsonNode articleList) throws Exception {
this.articleAdapter = new ArticleAdapter(activity, articleList, this.nbrColumns);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
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());
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@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();
return false;
}
});
}
public void clear() {
articleAdapter.clear();
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment