Dodaj algorytm rekomendacji do activity Results, komunikat w przypadku braku znalezienia żadnych, usprawnij budowanie listy wszystkich produktów, dodaj zawijanie tekstu kafelka zidentyfikowanego produktu

This commit is contained in:
Kacper Dudzic 2020-02-21 00:46:03 +01:00
parent f4d742f917
commit 7bf66b1f8f
4 changed files with 90 additions and 30 deletions

View File

@ -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'
}

View File

@ -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<Product> products;
private List<Product> 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<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@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<Integer, Integer> 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<Map.Entry<Integer, Integer>> byMapValues = new Ordering<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> left, Map.Entry<Integer, Integer> right) {
return left.getValue().compareTo(right.getValue());
}
};
List<Map.Entry<Integer, Integer>> 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()));
}
}
}
}

View File

@ -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"

View File

@ -11,7 +11,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8">
android:layout_weight="0.785">
<TextView
android:id="@+id/header"
@ -43,11 +43,12 @@
<TextView
android:id="@+id/foundproductname"
android:layout_width="wrap_content"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header" />
@ -98,8 +99,21 @@
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:background="#2196F3">
android:layout_weight="0.215"
android:background="#3889FF">
<TextView
android:id="@+id/noproductsmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="218dp"
android:layout_marginEnd="34dp"
android:layout_marginRight="34dp"
android:text="Nie znaleziono żadnych podobnych produktów..."
android:visibility = "gone"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
@ -112,6 +126,5 @@
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>