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

Article management

Print Articles per category for one foundation
frangment_article.xml is useless, can't work with it...
parent a95cc8de
......@@ -8,6 +8,10 @@ import android.widget.TabHost;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
/**
......@@ -18,23 +22,32 @@ public class ArticleCategoryActivity extends BaseActivity {
private static final String LOG_TAG = "_ArticleCategoryActivit";
private TabHost categoryTabList;
private int nbrCategories;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_articles_category);
Log.d(LOG_TAG, "C: " + getIntent().getExtras().getString("categoryList"));
Log.d(LOG_TAG, "A: " + getIntent().getExtras().getString("articleList"));
this.nbrCategories = 0;
this.categoryTabList = findViewById(R.id.tab_categories);
this.categoryTabList.setup();
try {
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")));
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList")));
} 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() {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.error_view), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
finish();
}
});
}
if (nbrCategories == 0) {
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.article_error_0_categorie_not_0), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
finish();
......@@ -46,20 +59,41 @@ public class ArticleCategoryActivity extends BaseActivity {
@Override
protected void onIdentification(String idBadge) {}
protected void createCategories(final JsonNode categoryList) throws Exception {
protected void createCategories(final JsonNode categoryList, final JsonNode articleList) throws Exception {
HashMap<Integer, ArrayList<JsonNode>> articlesPerCategory = new HashMap<Integer, ArrayList<JsonNode>>();
final int foundationId = nemopaySession.getFoundationId();
for (final 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") || article.get("fundation_id").intValue() != foundationId)
throw new Exception("Unexpected JSON");
if (!article.get("active").booleanValue())
continue;
if (articlesPerCategory.containsKey(article.get("categorie_id").intValue()))
articlesPerCategory.get(article.get("categorie_id").intValue()).add(article);
else
articlesPerCategory.put(article.get("categorie_id").intValue(), new ArrayList<JsonNode>(){{ add(article); }});
}
for (JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != nemopaySession.getFoundationId())
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != foundationId)
throw new Exception("Unexpected JSON");
createNewCategory(category.get("name").textValue());
ArrayList<JsonNode> articlesForThisCategory = articlesPerCategory.get(category.get("id").intValue());
if (articlesForThisCategory == null || articlesForThisCategory.size() == 0)
continue;
createNewCategory(category.get("name").textValue(), new ObjectMapper().readTree(articlesForThisCategory.toString()));
}
}
protected void createNewCategory(final String name) {
protected void createNewCategory(final String name, final JsonNode articleList) throws Exception {
TabHost.TabSpec newTabSpec = this.categoryTabList.newTabSpec(name);
newTabSpec.setIndicator(name);
newTabSpec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this));
newTabSpec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this, articleList));
this.categoryTabList.addTab(newTabSpec);
nbrCategories++;
}
}
package fr.utc.simde.payutc.fragments;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 28/10/2017.
*/
public class ArticleFragment extends View {
private static final String LOG_TAG = "_ArticleFragment";
private int articleId;
private int price;
private String name;
private String url;
private LayoutInflater layoutInflater;
private View view;
private TextView textView;
private LinearLayout linearLayout;
public ArticleFragment(Activity activity, final JsonNode article) {
super(activity);
/*
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());*/
TextView t = new TextView(activity);
ImageView iv = new ImageView(activity);
iv.setImageResource(R.mipmap.ic_launcher);
t.setText(article.get("name").textValue() + ": ");
TextView t2 = new TextView(activity);
t2.setText((article.get("price").intValue() / 100) + "€" + ((article.get("price").intValue() % 100) == 0 ? "" : (((article.get("price").intValue() % 100) < 10 ? "0" : "") + (article.get("price").intValue() % 100))));
this.linearLayout = new LinearLayout(activity);
linearLayout.addView(iv);
linearLayout.addView(t);
linearLayout.addView(t2);
}
public View getView() {
return this.linearLayout;
}
}
......@@ -3,10 +3,15 @@ package fr.utc.simde.payutc.fragments;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import fr.utc.simde.payutc.ArticleCategoryActivity;
import com.fasterxml.jackson.databind.JsonNode;
import fr.utc.simde.payutc.R;
/**
......@@ -14,16 +19,38 @@ import fr.utc.simde.payutc.R;
*/
public class ArticleGroupFragment implements TabHost.TabContentFactory {
private LayoutInflater articleLayout;
private static final String LOG_TAG = "_ArticleGroupFragment";
private LayoutInflater layoutInflater;
private View view;
private GridLayout gridView;
public ArticleGroupFragment(Activity activity) {
this.articleLayout = LayoutInflater.from(activity);
public ArticleGroupFragment(final Activity activity, final JsonNode articleList) throws Exception {
this.layoutInflater = LayoutInflater.from(activity);
this.view = this.layoutInflater.inflate(R.layout.fragment_article_group, null);
this.gridView = this.view.findViewById(R.id.grid_article);
createArticles(activity, articleList);
}
@Override
public View createTabContent(String tag) {
View view = this.articleLayout.inflate(R.layout.fragment_article_group, null);
public void createArticles(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");
return view;
if (!article.get("active").booleanValue())
continue;
linearLayout.addView(new ArticleFragment(activity, article).getView());
}
this.gridView.addView(linearLayout);
}
@Override
public View createTabContent(final String tag) {
return this.view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:id="@+id/image_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:id="@+id/text_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
android:layout_height="wrap_content" />
</LinearLayout>
......@@ -5,8 +5,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
<GridLayout
android:id="@+id/grid_article"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent" >
</GridLayout>
</LinearLayout>
......@@ -23,6 +23,7 @@
<string name="username_and_password_required">Login CAS et mot de passe requis !</string>
<string name="error_unexpected">Une erreur improbable a eu lieu</string>
<string name="error_view">Une erreur a empêché la création de la vue</string>
<string name="nfc_not_available">NFC non disponible</string>
<string name="nfc_not_enabled">NFC non activé</string>
......@@ -67,6 +68,7 @@
<string name="category_error_get_list">Impossible de récupérer la liste des catégories</string>
<string name="article_error_0">n\'a aucun article à vendre</string>
<string name="article_error_get_list">Impossible de récupérer la liste des articles</string>
<string name="article_error_0_categorie_not_0">Toutes les catégories sont vides</string>
<string name="user_no_rights">n\'a aucun droit. L\'utilisateur a été automatiquement déconnecté</string>
<string name="no_right">Il est nécessaire d\'avoir au moins le droit suivant:</string>
......
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