Skip to content

Commit

Permalink
third commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soufiane22 committed Feb 10, 2022
1 parent e1b804a commit a1ba52e
Show file tree
Hide file tree
Showing 19 changed files with 551 additions and 123 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ dependencies {
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'

implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
implementation 'com.squareup.okhttp3:okhttp:3.6.0'
implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.0.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
//implementation 'com.google.android.material:material:1.4.0'
}
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.example.gproduitfront">
<!--Internet Permission-->
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
129 changes: 122 additions & 7 deletions src/main/java/com/example/gproduitfront/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.gproduitfront;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -50,9 +54,12 @@ public class MainActivity extends AppCompatActivity {
RecyclerView recycler_view;
Button btn ;
Spinner myspinner ;
TextView nbr_tv;
TextView moyenne_tv;
List<RetroProduit> produitList = new ArrayList<>();
List<Categorie> categoriesList = new ArrayList<>();
ArrayList<String> categories = new ArrayList<>() ;
String category_item ;
public static String selecteCategorie ="test";

@Override
Expand All @@ -63,7 +70,9 @@ protected void onCreate(Bundle savedInstanceState) {
progressDoalog.show();
setContentView(R.layout.activity_main);
recycler_view = findViewById(R.id.recycler_view);
myspinner = (Spinner) findViewById(R.id.spinner);
myspinner = (Spinner) findViewById(R.id.spinner);
nbr_tv =findViewById(R.id.nbr);
moyenne_tv = findViewById(R.id.moyenne);

ArrayList<String> values = new ArrayList<>();
values.add(0,"TV");
Expand All @@ -77,35 +86,138 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selecteCategorie = parent.getItemAtPosition(position).toString();

String idCat = getIdCat();
Toast.makeText(MainActivity.this,selecteCategorie, Toast.LENGTH_LONG).show();

getListProduits(idCat);

}


@Override
public void onNothingSelected(AdapterView<?> parent) {
getAllProduits();

}
});
//View v = (View) findViewById(R.layout.activity_main2) ;

// set value selected in spinner
category_item = getIntent().getStringExtra("category");



btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,MainActivity2.class);
i.putExtra("category" , selecteCategorie);
i.putExtra("mode" , "mode_add");
// startActivity(i);
startActivityForResult(i, 0);
}
});

getListProduits(selecteCategorie);
String nbr1 = getIntent().getStringExtra("nbr_produit1");
String moyenne1 = getIntent().getStringExtra("moyenne1");

// get statistic after delete product
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("statistic"));

}

private int getIndex(Spinner myspinner, String category_item) {

for(int i =0 ; i < myspinner.getCount() ; i++){
if(myspinner.getItemAtPosition(i).toString().equals(category_item)){
//System.out.println(" index of category selected "+i);
return i;
}
}
return 0;
}

public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

// Get extra data included in the Intent
String nbr = intent.getStringExtra("nbr_produit");

String moyenne = intent.getStringExtra("moyenne");

getAllProduits();
nbr_tv.setText(String.valueOf(nbr));
moyenne_tv.setText(moyenne);
}
};

private void getStatistic(List<RetroProduit> produitList) {
Double somme_prix =0.0;
Double moyenne = 0.0;
int nbr_produit =produitList.size();
nbr_tv.setText(String.valueOf(nbr_produit) );
for (RetroProduit p : produitList){
somme_prix += p.getPu();
}
moyenne = somme_prix/nbr_produit;
moyenne = (double)((int)(moyenne*100))/100;
Log.d("moyenne","moyenne : "+moyenne);
moyenne_tv.setText(String.valueOf(moyenne));
}

private String getIdCat() {
String id_cat = null;
for(Categorie c:categoriesList){

if(c.getDesignation().equals(selecteCategorie)){
id_cat = c.getId();

}



}
return id_cat;
}

private void getAllProduits() {

GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<List<RetroProduit>> call = service.getAllProduits();

call.enqueue(new Callback<List<RetroProduit>>() {
@Override
public void onResponse(Call<List<RetroProduit>> call, Response<List<RetroProduit>> response) {
progressDoalog.dismiss();
produitList = response.body();
generateDataList(response.body());
setRecycleview();

if (!response.isSuccessful()){
try {
Toast.makeText(MainActivity.this, "Server returned error : "+response.errorBody().string(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Server returned error : unknown error ", Toast.LENGTH_LONG).show();
}
}
//response.body();

}

@Override
public void onFailure(Call<List<RetroProduit>> call, Throwable t) {
progressDoalog.dismiss();
Toast.makeText(getApplicationContext(), call.toString(), Toast.LENGTH_LONG).show();
System.out.println("onFailure=======>"+t.getMessage());
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();

}
});

}

private void getAllCategories() {
Expand Down Expand Up @@ -133,6 +245,9 @@ public void onResponse(Call<List<Categorie>> call, Response<List<Categorie>> res
myspinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("adapter is here"+categories.size());
int index = getIndex(myspinner,category_item);
System.out.println("index of category selected"+index);
myspinner.setSelection(index);
//System.out.println("response.body =======>"+response.body().toString());
if (!response.isSuccessful()){
try {
Expand All @@ -158,10 +273,10 @@ public void onFailure(Call<List<Categorie>> call, Throwable t) {

}

private void getAllProduits() {
private void getListProduits(String cat) {
/*Create handle for the RetrofitInstance interface*/
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<List<RetroProduit>> call = service.getAllProduits();
Call<List<RetroProduit>> call = service.getListProduits(cat);

call.enqueue(new Callback<List<RetroProduit>>() {
@Override
Expand All @@ -170,8 +285,8 @@ public void onResponse(Call<List<RetroProduit>> call, Response<List<RetroProduit
produitList = response.body();
generateDataList(response.body());
setRecycleview();
getStatistic(produitList);

//System.out.println("response.body =======>"+response.body().toString());
if (!response.isSuccessful()){
try {
Toast.makeText(MainActivity.this, "Server returned error : "+response.errorBody().string(), Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -212,7 +327,7 @@ private void generateDataList(List<RetroProduit> produitList) {
String content ="";
String libelle = p.getLabel();
Double pu = p.getPu();
content += "libellé :"+libelle+"\n";
content += "3------libellé :"+libelle+"\n";
content += "prix unitaire :"+pu+"\n\n";
System.out.println("produit :"+content);
// textReselt.append(content);
Expand Down
Loading

0 comments on commit a1ba52e

Please sign in to comment.