From 7bf66b1f8fbba5217d3f45bc84d1550d8d1089b3 Mon Sep 17 00:00:00 2001 From: Kacper Dudzic Date: Fri, 21 Feb 2020 00:46:03 +0100 Subject: [PATCH] =?UTF-8?q?Dodaj=20algorytm=20rekomendacji=20do=20activity?= =?UTF-8?q?=20Results,=20komunikat=20w=20przypadku=20braku=20znalezienia?= =?UTF-8?q?=20=C5=BCadnych,=20usprawnij=20budowanie=20listy=20wszystkich?= =?UTF-8?q?=20produkt=C3=B3w,=20dodaj=20zawijanie=20tekstu=20kafelka=20zid?= =?UTF-8?q?entyfikowanego=20produktu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lookify/app/build.gradle | 2 + .../java/com/example/lookifyv2/Results.java | 91 ++++++++++++++----- .../main/res/layout/activity_decode_fail.xml | 2 +- .../src/main/res/layout/activity_results.xml | 25 +++-- 4 files changed, 90 insertions(+), 30 deletions(-) diff --git a/Lookify/app/build.gradle b/Lookify/app/build.gradle index 6fa7867..bdeddf8 100644 --- a/Lookify/app/build.gradle +++ b/Lookify/app/build.gradle @@ -39,4 +39,6 @@ dependencies { implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'com.github.bumptech.glide:glide:3.7.0' + implementation group: 'com.google.guava', name: 'guava', version: '15.0' + implementation 'com.android.support:support-v4:26.0.2' } diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/Results.java b/Lookify/app/src/main/java/com/example/lookifyv2/Results.java index 6997ed0..c1bf979 100644 --- a/Lookify/app/src/main/java/com/example/lookifyv2/Results.java +++ b/Lookify/app/src/main/java/com/example/lookifyv2/Results.java @@ -20,6 +20,10 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import com.google.common.base.Functions; +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; import org.json.JSONArray; import org.json.JSONException; @@ -27,7 +31,10 @@ import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -37,6 +44,7 @@ public class Results extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private List products; + private List productstop6; private RecyclerView recyclerView; private GridLayoutManager gridLayout; private ProductsAdapter adapter; @@ -51,34 +59,24 @@ public class Results extends AppCompatActivity { recyclerView = findViewById(R.id.recyclerview); products = new ArrayList<>(); - getProductsFromDB(0); + productstop6 = new ArrayList<>(); + getProductsFromDB(); gridLayout = new GridLayoutManager(this, 2); recyclerView.setLayoutManager(gridLayout); - adapter = new ProductsAdapter(this, products); + adapter = new ProductsAdapter(this, productstop6); recyclerView.setAdapter(adapter); - - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - - if (gridLayout.findLastCompletelyVisibleItemPosition() == products.size() - 1) { - getProductsFromDB(products.get(products.size() - 1).getId()); - } - - } - }); } - private void getProductsFromDB(int id) { + private void getProductsFromDB() { - @SuppressLint("StaticFieldLeak") AsyncTask asyncTask = new AsyncTask() { + @SuppressLint("StaticFieldLeak") AsyncTask asyncTask = new AsyncTask() { @Override - protected Void doInBackground(Integer... productIds) { + protected Void doInBackground(Void... Void) { OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder().url("http://192.168.8.101/products.php?id=" + productIds[0]).build(); + Request request = new Request.Builder().url("http://192.168.8.101/products.php?id=0").build(); try { Response response = client.newCall(request).execute(); @@ -122,13 +120,12 @@ public class Results extends AppCompatActivity { } } if(foundPosition == -1){ - Intent intent_DecodeFail = new Intent(this, DecodeFail.class); + Intent intent_DecodeFail = new Intent(Results.this, DecodeFail.class); startActivity(intent_DecodeFail); finish(); } - */ - - int foundPosition = 1; //TESTESTSRETSTETSTETT +*/ + int foundPosition = 2; //TESTESTSRETSTETSTETT ImageView imageView = findViewById(R.id.foundproductimage); TextView foundProductPrice = findViewById(R.id.foundproductprice); @@ -156,10 +153,11 @@ public class Results extends AppCompatActivity { openDetails(finalFoundPosition); } }); + + getRecommendedProducts(finalFoundPosition); } }; - - asyncTask.execute(id); + asyncTask.execute(); } public void openDetails(int position){ @@ -186,4 +184,51 @@ public class Results extends AppCompatActivity { Toast.LENGTH_SHORT).show(); } + public void getRecommendedProducts(int foundpos){ + HashMap ranking = new HashMap<>(); + for(int i = 0; i < products.size(); i++) { + if (products.get(foundpos).getId() != products.get(i).getId() + && products.get(foundpos).getproductType().equals(products.get(i).getproductType()) + && products.get(foundpos).getproductSex().equals(products.get(i).getproductSex())) { + int counter = 0; + if (products.get(foundpos).getproductColour().equals(products.get(i).getproductColour())) { + counter++; + } + if (products.get(foundpos).getproductStyle().equals(products.get(i).getproductStyle())) { + counter++; + } + if (products.get(foundpos).getproductCollection().equals(products.get(i).getproductCollection()) + || products.get(foundpos).getproductCollection().equals("całoroczna") + || products.get(i).getproductCollection().equals("całoroczna")) { + counter++; + } + ranking.put(i, counter); + } + } + + if(ranking.size() == 0){ + TextView noproductsmessage = findViewById(R.id.noproductsmessage); + noproductsmessage.setVisibility(View.VISIBLE); + } + else{ + Ordering> byMapValues = new Ordering>() { + @Override + public int compare(Map.Entry left, Map.Entry right) { + return left.getValue().compareTo(right.getValue()); + } + }; + + List> rankinglist = Lists.newArrayList(ranking.entrySet()); + Collections.sort(rankinglist, byMapValues.reverse()); + + + + for(int i = 0; i < 6; i++) { + productstop6.add(products.get(rankinglist.get(i).getKey())); + } + } + + } } + + diff --git a/Lookify/app/src/main/res/layout/activity_decode_fail.xml b/Lookify/app/src/main/res/layout/activity_decode_fail.xml index 0dece61..abaf127 100644 --- a/Lookify/app/src/main/res/layout/activity_decode_fail.xml +++ b/Lookify/app/src/main/res/layout/activity_decode_fail.xml @@ -11,7 +11,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" - android:text="Ups! Kodu kreskowego nie znaleziono!" + android:text="Ups! Kodu nie znaleziono lub nie znajduje się on w bazie!" app:layout_constraintBottom_toTopOf="@+id/try_again_button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" diff --git a/Lookify/app/src/main/res/layout/activity_results.xml b/Lookify/app/src/main/res/layout/activity_results.xml index 7506ef6..243d44f 100644 --- a/Lookify/app/src/main/res/layout/activity_results.xml +++ b/Lookify/app/src/main/res/layout/activity_results.xml @@ -11,7 +11,7 @@ + android:layout_weight="0.785"> @@ -98,8 +99,21 @@ + android:layout_weight="0.215" + android:background="#3889FF"> + + - - + \ No newline at end of file