-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivityProductDetail.java
More file actions
302 lines (265 loc) · 11.9 KB
/
Copy pathActivityProductDetail.java
File metadata and controls
302 lines (265 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.app.yourrestaurantapp.activities;
import static com.app.yourrestaurantapp.utilities.Tools.PERMISSIONS_REQUEST;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import com.app.yourrestaurantapp.BuildConfig;
import com.app.yourrestaurantapp.Config;
import com.app.yourrestaurantapp.R;
import com.app.yourrestaurantapp.utilities.DBHelper;
import com.app.yourrestaurantapp.utilities.SharedPref;
import com.app.yourrestaurantapp.utilities.Tools;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.Snackbar;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
@SuppressWarnings("deprecation")
public class ActivityProductDetail extends AppCompatActivity {
long product_id;
TextView txt_product_name, txt_product_price, txt_product_quantity;
private String product_name, product_image, category_name, product_status, product_description;
private double product_price;
private int serve_for;
WebView webView;
ImageView img_product_image;
Button btn_cart;
ImageView cart_badge;
public static DBHelper dbHelper;
final Context context = this;
private AppBarLayout appBarLayout;
SharedPref sharedPref;
CoordinatorLayout parent_view;
TextView toolbar_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
Tools.lightNavigation(this);
dbHelper = new DBHelper(this);
sharedPref = new SharedPref(this);
getData();
initComponent();
displayData();
setupToolbar();
}
public void setupToolbar() {
final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("");
}
onToolbarIconClicked();
toolbar_title.setText(category_name);
}
public void getData() {
Intent intent = getIntent();
product_id = intent.getLongExtra("product_id", 0);
product_name = intent.getStringExtra("title");
product_image = intent.getStringExtra("image");
product_price = intent.getDoubleExtra("product_price", 0);
product_description = intent.getStringExtra("product_description");
serve_for = intent.getIntExtra("serve_for", 0);
product_status = intent.getStringExtra("product_status");
category_name = intent.getStringExtra("category_name");
}
public void initComponent() {
txt_product_name = findViewById(R.id.product_name);
img_product_image = findViewById(R.id.product_image);
txt_product_price = findViewById(R.id.product_price);
webView = findViewById(R.id.product_description);
txt_product_quantity = findViewById(R.id.product_quantity);
btn_cart = findViewById(R.id.btn_add_cart);
cart_badge = findViewById(R.id.cart_badge);
parent_view = findViewById(R.id.parent_view);
toolbar_title = findViewById(R.id.toolbar_title);
}
public void displayData() {
txt_product_name.setText(product_name);
Picasso.with(this)
.load(Config.ADMIN_PANEL_URL + "/upload/product/" + product_image.replace(" ", "%20"))
.placeholder(R.drawable.ic_loading)
.into(img_product_image);
img_product_image.setOnClickListener(view -> {
Intent intent = new Intent(getApplicationContext(), ActivityImageDetail.class);
intent.putExtra("title", product_name);
intent.putExtra("image", Config.ADMIN_PANEL_URL + "/upload/product/" + product_image);
startActivity(intent);
});
txt_product_price.setText(Tools.decimalFormatter(product_price) + " " + sharedPref.getCurrencyCode());
txt_product_quantity.setText(serve_for + " " + getString(R.string.txt_items));
if (product_status.equals("1")) {
refreshCartButton();
btn_cart.setOnClickListener(v -> {
if (dbHelper.isDataExist(product_id)) {
dbHelper.deleteData(product_id);
Snackbar.make(parent_view, getString(R.string.msg_remove_cart), Snackbar.LENGTH_SHORT).show();
btn_cart.setText(R.string.btn_add_to_cart);
new Handler().postDelayed(() -> {
btn_cart.setBackgroundColor(ContextCompat.getColor(this, R.color.color_light_status_bar));
}, 100);
} else {
dbHelper.addData(product_id, product_name, 1, (product_price * 1), product_image, String.valueOf(System.currentTimeMillis()));
Snackbar.make(parent_view, getString(R.string.msg_add_cart), Snackbar.LENGTH_SHORT).show();
btn_cart.setText(R.string.btn_remove_from_cart);
new Handler().postDelayed(() -> {
btn_cart.setBackgroundColor(ContextCompat.getColor(this, R.color.color_yellow));
}, 100);
}
refreshCartBadge();
});
} else {
btn_cart.setEnabled(false);
btn_cart.setText(R.string.btn_out_of_stock);
btn_cart.setBackgroundColor(ContextCompat.getColor(this, R.color.color_sold));
}
Tools.showContentDescription(this, webView, product_description);
}
private void refreshCartButton() {
if (dbHelper.isDataExist(product_id)) {
btn_cart.setText(R.string.btn_remove_from_cart);
btn_cart.setBackgroundColor(ContextCompat.getColor(this, R.color.color_yellow));
} else {
btn_cart.setText(R.string.btn_add_to_cart);
btn_cart.setBackgroundColor(ContextCompat.getColor(this, R.color.color_light_status_bar));
}
}
private void refreshCartBadge() {
if (dbHelper.getAllData().size() > 0) {
cart_badge.setVisibility(View.VISIBLE);
} else {
cart_badge.setVisibility(View.INVISIBLE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int itemId = menuItem.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
} else {
return super.onOptionsItemSelected(menuItem);
}
return true;
}
private void onToolbarIconClicked() {
findViewById(R.id.btn_share).setOnClickListener(view -> {
requestStoragePermission();
});
findViewById(R.id.btn_cart).setOnClickListener(view -> {
Intent intent = new Intent(getApplicationContext(), ActivityCart.class);
startActivity(intent);
});
}
public void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(ActivityProductDetail.this, "android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, PERMISSIONS_REQUEST);
} else {
(new ShareTask(ActivityProductDetail.this)).execute(Config.ADMIN_PANEL_URL + "/upload/product/" + product_image);
}
} else {
(new ShareTask(ActivityProductDetail.this)).execute(Config.ADMIN_PANEL_URL + "/upload/product/" + product_image);
}
}
@Override
public void onResume() {
super.onResume();
refreshCartButton();
refreshCartBadge();
}
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("deprecation")
public class ShareTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
URL myFileUrl;
Bitmap bmImg = null;
File file;
public ShareTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage(getString(R.string.loading_msg));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
try {
myFileUrl = new URL(args[0]);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
String path = myFileUrl.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/" + getResources().getString(R.string.app_name) + "/");
dir.mkdirs();
String fileName = idStr;
file = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(file);
bmImg.compress(Bitmap.CompressFormat.PNG, 99, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String args) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_product_section_one) + " " + product_name + " " + getString(R.string.share_product_section_two) + " " + Tools.decimalFormatter(product_price) + " " + sharedPref.getCurrencyCode() + getString(R.string.share_product_section_three) + "\n" + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID);
startActivity(Intent.createChooser(intent, "Share Image"));
pDialog.dismiss();
}
}
}