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

Optimization

Print all categories in tabs
Optimize code
Cleaning
Preparation to ArticleFragment
parent 0fb5e802
package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
/**
......@@ -13,6 +17,8 @@ import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
public class ArticleCategoryActivity extends BaseActivity {
private static final String LOG_TAG = "_ArticleCategoryActivit";
private TabHost categoryTabList;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -21,26 +27,39 @@ public class ArticleCategoryActivity extends BaseActivity {
Log.d(LOG_TAG, "C: " + getIntent().getExtras().getString("categoryList"));
Log.d(LOG_TAG, "A: " + getIntent().getExtras().getString("articleList"));
TabHost host = findViewById(R.id.tab_categories);
host.setup();
this.categoryTabList = findViewById(R.id.tab_categories);
this.categoryTabList.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Softs");
spec.setIndicator("Softs");
spec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this));
try {
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")));
} 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();
}
});
}
}
host.addTab(spec);
@Override
protected void onIdentification(String idBadge) {}
//Tab 1
TabHost.TabSpec spec2 = host.newTabSpec("Bières");
spec2.setIndicator("Bières");
spec2.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this));
protected void createCategories(final JsonNode categoryList) throws Exception {
for (JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
host.addTab(spec2);
createNewCategory(category.get("name").textValue());
}
}
@Override
protected void onIdentification(String idBadge) {
protected void createNewCategory(final String name) {
TabHost.TabSpec newTabSpec = this.categoryTabList.newTabSpec(name);
newTabSpec.setIndicator(name);
newTabSpec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this));
this.categoryTabList.addTab(newTabSpec);
}
}
......@@ -49,170 +49,179 @@ public abstract class BaseActivity extends NFCActivity {
protected void startFoundationListActivity(final Activity activity) {
dialog.startLoading(activity, getString(R.string.information_collection), getString(R.string.foundation_list_collecting));
final Intent intent = new Intent(activity, FoundationListActivity.class);
new Thread() {
@Override
public void run() {
try {
nemopaySession.getFoundations();
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
HTTPRequest request = nemopaySession.getRequest();
try {
// Toute une série de vérifications avant de lancer l'activité
if (request.getResponseCode() != 200)
throw new Exception("HTTP Error: " + Integer.toString(request.getResponseCode()));
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getFoundations() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
JsonNode foundationList = request.getJsonResponse();
String response = request.getResponse();
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode foundationList = request.getJsonResponse();
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("Malformed JSON");
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("Malformed JSON");
if (foundationList.size() == 0) {
if (foundationList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
fatal(activity, getString(R.string.information_collection), nemopaySession.getUsername() + " " + getString(R.string.user_no_rights));
return;
}
});
for (final JsonNode foundation : foundationList) {
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
}
return;
}
if (foundationList.size() == 1) {
dialog.stopLoading();
for (final JsonNode foundation : foundationList) {
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
}
if (foundationList.size() == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
startArticlesActivity(activity, foundationList.get(0).get("fun_id").intValue(), foundationList.get(0).get("name").textValue());
return;
}
});
return;
}
Intent intent = new Intent(activity, FoundationListActivity.class);
intent.putExtra("foundationList", response);
intent.putExtra("foundationList", request.getResponse());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
activity.startActivity(intent);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
}
}
});
});
}
}
}.start();
}
protected void startCategoryArticlesActivity(final Activity activity) {
dialog.startLoading(activity, activity.getResources().getString(R.string.information_collection), activity.getResources().getString(R.string.category_list_collecting));
final Intent intent = new Intent(activity, ArticleCategoryActivity.class);
final Thread categoryThread = new Thread() {
new Thread() {
@Override
public void run() {
try {
nemopaySession.getCategories();
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
HTTPRequest request = nemopaySession.getRequest();
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getCategories() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
try {
// Toute une série de vérifications avant de lancer l'activité
if (request.getResponseCode() != 200)
throw new Exception("HTTP Error: " + Integer.toString(request.getResponseCode()));
JsonNode categoryList = request.getJsonResponse();
String response = request.getResponse();
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode categoryList = request.getJsonResponse();
if (!request.isJsonResponse() || !categoryList.isArray())
throw new Exception("Malformed JSON");
if (!request.isJsonResponse() || !categoryList.isArray())
throw new Exception("Malformed JSON");
if (categoryList.size() == 0) {
if (categoryList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(activity, getString(R.string.information_collection), nemopaySession.getFoundationName() + " " + getString(R.string.category_error_0));
return;
}
});
for (final JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
return;
}
String extra_category = response;
dialog.changeLoading(getResources().getString(R.string.article_list_collecting));
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.category_error_get_list));
}
for (final JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
});
}
};
categoryThread.start();
new Thread() {
@Override
public void run() {
final Intent intent = new Intent(activity, ArticleCategoryActivity.class);
try {
categoryThread.join();
Thread.sleep(100);
intent.putExtra("categoryList", nemopaySession.getRequest().getResponse());
nemopaySession.getArticles();
Thread.sleep(100);
intent.putExtra("categoryList", request.getResponse());
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.category_error_get_list));
}
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
HTTPRequest request = nemopaySession.getRequest();
dialog.changeLoading(getResources().getString(R.string.article_list_collecting));
}
});
try {
// Toute une série de vérifications avant de lancer l'activité
if (request.getResponseCode() != 200)
throw new Exception("HTTP Error: " + Integer.toString(request.getResponseCode()));
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getArticles() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
JsonNode articleList = request.getJsonResponse();
String response = request.getResponse();
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode articleList = request.getJsonResponse();
if (!request.isJsonResponse() || !articleList.isArray())
throw new Exception("Malformed JSON");
if (!request.isJsonResponse() || !articleList.isArray())
throw new Exception("Malformed JSON");
if (articleList.size() == 0) {
if (articleList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(activity, getString(R.string.information_collection), nemopaySession.getFoundationName() + " " + getString(R.string.article_error_0));
return;
}
});
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() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
return;
}
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() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
intent.putExtra("articleList", response);
intent.putExtra("articleList", request.getResponse());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
activity.startActivity(intent);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.article_error_get_list));
}
}
});
});
}
}
}.start();
}
......
package fr.utc.simde.payutc.fragments;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import fr.utc.simde.payutc.ArticleCategoryActivity;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 27/10/2017.
*/
public class ArticleGroupFragment implements TabHost.TabContentFactory {
private TextView nameText;
private LayoutInflater articleLayout;
public ArticleGroupFragment(Activity activity) {
nameText = new TextView(activity);
this.articleLayout = LayoutInflater.from(activity);
}
@Override
public View createTabContent(String tag) {
nameText.setText(tag);
return nameText;
View view = this.articleLayout.inflate(R.layout.fragment_article_group, null);
return view;
}
}
......@@ -15,10 +15,18 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
......
......@@ -6,6 +6,7 @@
android:layout_height="match_parent">
<GridView
android:id="@+id/grid_article"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Supports Markdown
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