153 lines
5.3 KiB
Java
153 lines
5.3 KiB
Java
package com.example.lookifyv2;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.PorterDuff;
|
|
import android.os.AsyncTask;
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class Favourites extends AppCompatActivity {
|
|
|
|
private List<Product> products;
|
|
private List<Product> favourites;
|
|
private RecyclerView recyclerView;
|
|
private GridLayoutManager gridLayout;
|
|
private FavouritesAdapter adapter;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_favourites);
|
|
|
|
assert getSupportActionBar() != null;
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
CheckInternet check = new CheckInternet();
|
|
if(!check.isOnline()){
|
|
TextView nointernetmessage = findViewById(R.id.nointernetmessage);
|
|
nointernetmessage.setVisibility(View.VISIBLE);
|
|
|
|
Button button_refresh;
|
|
button_refresh = findViewById(R.id.refresh_button);
|
|
button_refresh.getBackground().setColorFilter(0xFFFFFFFF, PorterDuff.Mode.MULTIPLY);
|
|
button_refresh.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
recreate();
|
|
}
|
|
});
|
|
button_refresh.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
recyclerView = findViewById(R.id.favouriterecyclerview);
|
|
products = new ArrayList<>();
|
|
favourites = new ArrayList<>();
|
|
getProductsFromDB();
|
|
|
|
gridLayout = new GridLayoutManager(this, 2);
|
|
recyclerView.setLayoutManager(gridLayout);
|
|
|
|
adapter = new FavouritesAdapter(this, favourites);
|
|
recyclerView.setAdapter(adapter);
|
|
}
|
|
|
|
private void getProductsFromDB() {
|
|
|
|
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
|
|
@Override
|
|
protected Void doInBackground(Void... Void) {
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
Request request = new Request.Builder().url("http://192.168.0.87/products.php?id=0").build();
|
|
try {
|
|
Response response = client.newCall(request).execute();
|
|
|
|
JSONArray array = new JSONArray(response.body().string());
|
|
|
|
for (int i = 0; i < array.length(); i++) {
|
|
|
|
JSONObject object = array.getJSONObject(i);
|
|
|
|
Product product = new Product(object.getInt("id"), object.getString("product_code"),
|
|
object.getString("product_name"), object.getString("product_image"),
|
|
object.getString("product_price"), object.getString("product_retailer"),
|
|
object.getString("product_colour"), object.getString("product_style"),
|
|
object.getString("product_collection"),
|
|
object.getString("product_type"), object.getString("product_sex"));
|
|
|
|
Favourites.this.products.add(product);
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected void onPostExecute(Void aVoid) {
|
|
adapter.notifyDataSetChanged();
|
|
|
|
getFavourites();
|
|
}
|
|
};
|
|
asyncTask.execute();
|
|
}
|
|
|
|
private void getFavourites() {
|
|
SharedPreferences pref = MyApplication.getInstance().getSharedPreferences("Favourites", 0);
|
|
Map<String, ?> codes = pref.getAll();
|
|
ArrayList<String> codeslist = Lists.newArrayList(codes.keySet());
|
|
if (codeslist.size() == 0) {
|
|
TextView noproductsmessage = findViewById(R.id.nofavouritesmessage);
|
|
noproductsmessage.setVisibility(View.VISIBLE);
|
|
} else {
|
|
for (int i = 0; i < products.size(); i++) {
|
|
if (codeslist.size() != 0) {
|
|
for (int j = 0; j < codeslist.size(); j++) {
|
|
if (products.get(i).getproductCode().equals(codeslist.get(j))) {
|
|
favourites.add(products.get(i));
|
|
codeslist.remove(j);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@Override
|
|
public boolean onSupportNavigateUp(){
|
|
finish();
|
|
return true;
|
|
}
|
|
}
|