Skip to content

Commit

Permalink
added support for notifications, in case a price changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoLuna5 committed Aug 22, 2020
1 parent 14fe8f3 commit 0d7b470
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 22 deletions.
30 changes: 27 additions & 3 deletions app/src/main/java/lunainc/com/mx/pricetracker/UI/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.work.Constraints;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -15,13 +19,15 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import lunainc.com.mx.pricetracker.Adapter.ProductsAdapter;
import lunainc.com.mx.pricetracker.Model.Product;
import lunainc.com.mx.pricetracker.R;
import lunainc.com.mx.pricetracker.Utils.DBHelper;
import lunainc.com.mx.pricetracker.Utils.MyWorker;

public class MainActivity extends AppCompatActivity implements ProductsAdapter.ItemClickListener, ProductsAdapter.ItemLongClickListener {

Expand All @@ -42,7 +48,8 @@ public class MainActivity extends AppCompatActivity implements ProductsAdapter.I
private DBHelper db;
private ArrayList<Product> arrayList;
private ProductsAdapter productsAdapter;

private WorkManager mWorkManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -61,7 +68,7 @@ private void configView() {
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
arrayList = loadData();

mWorkManager = WorkManager.getInstance();
productsAdapter = new ProductsAdapter(this, arrayList);
productsAdapter.notifyDataSetChanged();
productsAdapter.setClickListener(this);
Expand All @@ -74,7 +81,7 @@ private void configView() {
protected void onStart() {
super.onStart();
events();

periodicWork();
}

private ArrayList<Product> loadData() {
Expand Down Expand Up @@ -140,4 +147,21 @@ public void onItemClick(View view, int position) {
public void onItemLongClick(View view, int position) {
deleteProduct(position);
}


public void periodicWork() {
PeriodicWorkRequest mRequest = new PeriodicWorkRequest.Builder(MyWorker.class, 6, TimeUnit.HOURS)
.setConstraints(Constraints.NONE)
.build();

mWorkManager.getWorkInfoByIdLiveData(mRequest.getId()).observe(this, workInfo -> {
if (workInfo != null) {
WorkInfo.State state = workInfo.getState();


}
});
mWorkManager.enqueue(mRequest);
}

}
70 changes: 68 additions & 2 deletions app/src/main/java/lunainc/com/mx/pricetracker/Utils/MyWorker.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package lunainc.com.mx.pricetracker.Utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Objects;

import lunainc.com.mx.pricetracker.Connect.APIService;
import lunainc.com.mx.pricetracker.Connect.ApiUtils;
import lunainc.com.mx.pricetracker.Model.ML.ProductML;
import lunainc.com.mx.pricetracker.Model.Product;
import lunainc.com.mx.pricetracker.R;
import lunainc.com.mx.pricetracker.UI.MainActivity;
import retrofit2.Call;
import retrofit2.Callback;
Expand Down Expand Up @@ -52,8 +64,14 @@ public void onResponse(Call<ProductML> call, Response<ProductML> response) {
if (response.isSuccessful()){

String price = String.valueOf(response.body().getPrice());
db.createPrice(product.getId(), price);
db.updateProduct("status", response.body().getStatus(), String.valueOf(product.getId()));

if (!product.getPrice().equals(price)){
db.createPrice(product.getId(), price);
db.updateProduct("status", response.body().getStatus(), String.valueOf(product.getId()));
sendNotification(product.getName(), "Tu producto "+product.getName()+" ha cambiado de precio, su precio ahora es "+price);
}




}else {
Expand All @@ -74,4 +92,52 @@ public void onFailure(Call<ProductML> call, Throwable t) {

}

private void sendNotification(String productName, String body){
Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_check);
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "pricetrackernotification";
CharSequence name = "pricetrackernotification";
String description = "Channel description";


int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
mChannel.enableVibration(true);
mChannel.setShowBadge(false);
Objects.requireNonNull(notificationManager).createNotificationChannel(mChannel);
}


NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "pricetrackernotification")
.setContentTitle("El producto "+productName+" ha cambiado de precio")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_check)
.setLargeIcon(bitmap)
.setColor(getApplicationContext().getResources().getColor(R.color.colorPrimary))
.setContentText(body)
.setGroup("pricetrackernotify")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);


PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(resultPendingIntent);
Objects.requireNonNull(notificationManager).notify(0, notification.build());

}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
package lunainc.com.mx.pricetracker.Utils;

import android.util.Log;

import androidx.multidex.MultiDexApplication;
import androidx.work.Constraints;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

import lunainc.com.mx.pricetracker.UI.MainActivity;

public class PriceTracker extends MultiDexApplication {
private WorkManager mWorkManager;

@Override
public void onCreate() {
super.onCreate();
mWorkManager = WorkManager.getInstance();
periodicWork();

}


public void periodicWork() {
PeriodicWorkRequest mRequest = new PeriodicWorkRequest.Builder(MyWorker.class, 6, TimeUnit.HOURS)
.setConstraints(Constraints.NONE)
.build();
mWorkManager.enqueue(mRequest);
}

}

0 comments on commit 0d7b470

Please sign in to comment.