From b31d5697592eb0b8d787d8d85170adbe8d9d9349 Mon Sep 17 00:00:00 2001 From: Kacper Dudzic Date: Fri, 14 Feb 2020 23:22:23 +0100 Subject: [PATCH] =?UTF-8?q?dodaj=20wst=C4=99pn=C4=85=20wersj=C4=99=20ekran?= =?UTF-8?q?u=20wynik=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lookify/app/src/main/AndroidManifest.xml | 5 +- .../example/lookifyv2/ConnectDatabase.java | 14 -- .../com/example/lookifyv2/MainActivity.java | 18 +++ .../java/com/example/lookifyv2/Product.java | 137 ++++++++++++++++++ .../example/lookifyv2/ProductsAdapter.java | 107 ++++++++++++++ .../com/example/lookifyv2/PullDatabase.java | 14 -- .../java/com/example/lookifyv2/Results.java | 110 ++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 13 ++ ...nect_database.xml => activity_product.xml} | 2 +- .../res/layout/activity_products_adapter.xml | 9 ++ ...pull_database.xml => activity_results.xml} | 6 +- Lookify/app/src/main/res/layout/card.xml | 10 +- Lookify/app/src/main/res/values/colors.xml | 12 +- 13 files changed, 409 insertions(+), 48 deletions(-) delete mode 100644 Lookify/app/src/main/java/com/example/lookifyv2/ConnectDatabase.java create mode 100644 Lookify/app/src/main/java/com/example/lookifyv2/Product.java create mode 100644 Lookify/app/src/main/java/com/example/lookifyv2/ProductsAdapter.java delete mode 100644 Lookify/app/src/main/java/com/example/lookifyv2/PullDatabase.java create mode 100644 Lookify/app/src/main/java/com/example/lookifyv2/Results.java rename Lookify/app/src/main/res/layout/{activity_connect_database.xml => activity_product.xml} (78%) create mode 100644 Lookify/app/src/main/res/layout/activity_products_adapter.xml rename Lookify/app/src/main/res/layout/{activity_pull_database.xml => activity_results.xml} (84%) diff --git a/Lookify/app/src/main/AndroidManifest.xml b/Lookify/app/src/main/AndroidManifest.xml index b1ff686..7f59857 100644 --- a/Lookify/app/src/main/AndroidManifest.xml +++ b/Lookify/app/src/main/AndroidManifest.xml @@ -14,8 +14,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - - + + + diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/ConnectDatabase.java b/Lookify/app/src/main/java/com/example/lookifyv2/ConnectDatabase.java deleted file mode 100644 index 74fc932..0000000 --- a/Lookify/app/src/main/java/com/example/lookifyv2/ConnectDatabase.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.lookifyv2; - -import androidx.appcompat.app.AppCompatActivity; - -import android.os.Bundle; - -public class ConnectDatabase extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_connect_database); - } -} diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/MainActivity.java b/Lookify/app/src/main/java/com/example/lookifyv2/MainActivity.java index 87652d1..eaa2a0f 100644 --- a/Lookify/app/src/main/java/com/example/lookifyv2/MainActivity.java +++ b/Lookify/app/src/main/java/com/example/lookifyv2/MainActivity.java @@ -15,6 +15,8 @@ public class MainActivity extends AppCompatActivity { private Button button_help; private Button button_about; + private Button button_results; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -24,6 +26,8 @@ public class MainActivity extends AppCompatActivity { button_help = findViewById(R.id.help_start_button); button_about = findViewById(R.id.about_start_button); + button_results = findViewById(R.id.results_start_button); + button_scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -43,6 +47,13 @@ public class MainActivity extends AppCompatActivity { } }); + button_results.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openResults(); + } + }); + //Dezaktywacja przycisku skanowania, jeśli nie ma dostępu do kamery; żądanie dostępu do kamery i zapisu if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { button_scan.setEnabled(false); @@ -75,4 +86,11 @@ public class MainActivity extends AppCompatActivity { Intent intent_Scan = new Intent(this, TakePhoto.class); startActivity(intent_Scan); } + + public void openResults(){ + Intent intent_Results = new Intent(this, Results.class); + startActivity(intent_Results); + } + + } diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/Product.java b/Lookify/app/src/main/java/com/example/lookifyv2/Product.java new file mode 100644 index 0000000..5ef8190 --- /dev/null +++ b/Lookify/app/src/main/java/com/example/lookifyv2/Product.java @@ -0,0 +1,137 @@ +package com.example.lookifyv2; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; + +public class Product extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_product); + } + + /** + * Created by AndroidBash on 09/05/2016. + */ + + private int id; + private String imageLink; + private String productPrice; + + private String productCode; + private String productName; + private String productRetailer; + private String productColour; + private String productStyle; + private String productCollection; + private String productType; + private String productSex; + + + public Product(int id, String imageLink, String productPrice, String productCode, String productName, String productRetailer, + String productColour, String productStyle, String productCollection, String productType, String productSex) { + + this.id = id; + this.imageLink = imageLink; + this.productPrice = productPrice; + + this.productCode = productCode; + this.productName = productName; + this.productRetailer = productRetailer; + this.productColour = productColour; + this.productStyle = productStyle; + this.productCollection = productCollection; + this.productType = productType; + this.productSex = productSex; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getImageLink() { + return imageLink; + } + + public void setImageLink(String imageLink) { + this.imageLink = imageLink; + } + + public String getproductPrice() { + return productPrice; + } + + public void setproductPrice(String productPrice) { + this.productPrice = productPrice; + } + + public String getproductCode() { + return productCode; + } + + public void setproductCode(String productCode) { + this.productCode = productCode; + } + + public String getproductName() { + return productName; + } + + public void setproductName(String productName) { + this.productName = productName; + } + + public String getproductRetailer() { + return productRetailer; + } + + public void setproductRetailer(String productRetailer) { + this.productRetailer = productRetailer; + } + + public String getproductColour() { + return productColour; + } + + public void setproductColour(String productColour) { + this.productColour = productColour; + } + + public String getproductStyle() { + return productStyle; + } + + public void setproductStyle(String productStyle) { + this.productStyle = productStyle; + } + + public String getproductCollection() { + return productCollection; + } + + public void setproductCollection(String productCollection) { + this.productCollection = productCollection; + } + + public String getproductType() { + return productType; + } + + public void setproductType(String productType) { + this.productType = productType; + } + + public String getproductSex() { + return productSex; + } + + public void setproductSex(String productSex) { + this.productSex = productSex; + } + } diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/ProductsAdapter.java b/Lookify/app/src/main/java/com/example/lookifyv2/ProductsAdapter.java new file mode 100644 index 0000000..31fbd48 --- /dev/null +++ b/Lookify/app/src/main/java/com/example/lookifyv2/ProductsAdapter.java @@ -0,0 +1,107 @@ +package com.example.lookifyv2; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; +import android.content.Context; +import androidx.recyclerview.widget.RecyclerView; + +import android.view.LayoutInflater; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.PopupMenu; //????????????????????? +import android.widget.TextView; +import android.widget.Toast; + +import com.bumptech.glide.Glide; + +import java.util.List; + +public class ProductsAdapter extends RecyclerView.Adapter{ + + private Context context; + private List products; + + public ProductsAdapter(Context context, List products) { + this.context = context; + this.products = products; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + + View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false); + + return new ViewHolder(itemView); + } + + @Override + public void onBindViewHolder(final ViewHolder holder, int position) { + + holder.productName.setText(products.get(position).getproductName()); + holder.productPrice.setText(products.get(position).getproductPrice()); //???????????????????????? + Glide.with(context).load(products.get(position).getImageLink()).into(holder.imageView); + } + + @Override + public int getItemCount() { + return products.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ + + public TextView productName; + public TextView productPrice; + public ImageView imageView; + + public ViewHolder(View itemView) { + super(itemView); + productName = (TextView) itemView.findViewById(R.id.productname); + imageView = (ImageView) itemView.findViewById(R.id.image); + productPrice = (TextView) itemView.findViewById(R.id.productprice); + imageView.setOnClickListener(this); + + } + + @Override + public void onClick(View v) { + int position = getAdapterPosition(); + showPopupMenu(v,position); + } + } + + private void showPopupMenu(View view, int position) { + PopupMenu popup = new PopupMenu(context, view); + MenuInflater inflater = popup.getMenuInflater(); + inflater.inflate(R.menu.menu_context, popup.getMenu()); + popup.setOnMenuItemClickListener(new MenuClickListener(position)); + popup.show(); + } + + class MenuClickListener implements PopupMenu.OnMenuItemClickListener { + Integer pos; + public MenuClickListener(int pos) { + this.pos=pos; + } + + @Override + public boolean onMenuItemClick(MenuItem menuItem) { + switch (menuItem.getItemId()) { + case R.id.action_favourite: + Toast.makeText(context, products.get(pos).getproductName()+" is added to favourite", Toast.LENGTH_SHORT).show(); + return true; + case R.id.action_watch: + Toast.makeText(context, products.get(pos).getproductName()+" is added to watchlist", Toast.LENGTH_SHORT).show(); + return true; + case R.id.action_book: + Toast.makeText(context, "Booked Ticket for "+ products.get(pos).getproductName(), Toast.LENGTH_SHORT).show(); + return true; + default: + } + return false; + } + } +} diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/PullDatabase.java b/Lookify/app/src/main/java/com/example/lookifyv2/PullDatabase.java deleted file mode 100644 index ba9b384..0000000 --- a/Lookify/app/src/main/java/com/example/lookifyv2/PullDatabase.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.lookifyv2; - -import androidx.appcompat.app.AppCompatActivity; - -import android.os.Bundle; - -public class PullDatabase extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_pull_database); - } -} diff --git a/Lookify/app/src/main/java/com/example/lookifyv2/Results.java b/Lookify/app/src/main/java/com/example/lookifyv2/Results.java new file mode 100644 index 0000000..063deea --- /dev/null +++ b/Lookify/app/src/main/java/com/example/lookifyv2/Results.java @@ -0,0 +1,110 @@ +package com.example.lookifyv2; + +import androidx.appcompat.app.AppCompatActivity; + +import android.annotation.SuppressLint; +import android.os.Bundle; + +import android.os.AsyncTask; + +import android.os.Bundle; + +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +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 okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class Results extends AppCompatActivity { + + private static final String TAG = MainActivity.class.getSimpleName(); + private List products; + private RecyclerView recyclerView; + private GridLayoutManager gridLayout; + private ProductsAdapter adapter; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_results); + + recyclerView = findViewById(R.id.recyclerview); + products = new ArrayList<>(); + getProductsFromDB(0); + + gridLayout = new GridLayoutManager(this, 2); + recyclerView.setLayoutManager(gridLayout); + + adapter = new ProductsAdapter(this, products); + 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) { + + @SuppressLint("StaticFieldLeak") AsyncTask asyncTask = new AsyncTask() { + @Override + protected Void doInBackground(Integer... productIds) { + + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url("http://192.168.8.101/products.php?id=" + productIds[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")); + + Results.this.products.add(product); + } + + + } catch (IOException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + adapter.notifyDataSetChanged(); + } + }; + + asyncTask.execute(id); + } + +} diff --git a/Lookify/app/src/main/res/layout/activity_main.xml b/Lookify/app/src/main/res/layout/activity_main.xml index c6f2a1c..dd370ca 100644 --- a/Lookify/app/src/main/res/layout/activity_main.xml +++ b/Lookify/app/src/main/res/layout/activity_main.xml @@ -43,4 +43,17 @@ app:layout_constraintTop_toBottomOf="@+id/help_start_button" app:layout_constraintVertical_bias="0.0" /> +