Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.greatdealcompare.app.model.repository.pricealert
- import android.annotation.SuppressLint
- import android.app.Service
- import android.content.Intent
- import android.os.*
- import com.greatdealcompare.app.extension.subscribeIgnoreResult
- import com.greatdealcompare.app.model.repository.product.ProductRepository
- import javax.inject.Inject
- import android.app.NotificationManager
- import android.app.PendingIntent
- import android.app.TaskStackBuilder
- import android.content.Context
- import android.support.v4.app.NotificationCompat
- import com.greatdealcompare.app.BuildConfig
- import com.greatdealcompare.app.R
- import com.greatdealcompare.app.entity.search.Offer
- import com.greatdealcompare.app.entity.search.Product
- import com.greatdealcompare.app.toothpick.DI
- import com.greatdealcompare.app.toothpick.module.AppModule
- import com.greatdealcompare.app.toothpick.module.DatabaseModule
- import com.greatdealcompare.app.toothpick.module.MainActivityModule
- import com.greatdealcompare.app.toothpick.module.ServerModule
- import com.greatdealcompare.app.ui.launch.MainActivity
- import toothpick.Toothpick
- @SuppressLint("Registered")
- class PriceAlertService : Service() {
- private val timeToCheck = 1000 * 30
- private var mServiceLooper: Looper? = null
- private var mServiceHandler: ServiceHandler? = null
- @Inject lateinit var priceAlertRepository: PriceAlertRepository
- @Inject lateinit var productRepository: ProductRepository
- private inner class ServiceHandler(looper: Looper) : Handler(looper) {
- override fun handleMessage(msg: Message) {
- var time = System.currentTimeMillis()
- while (true) {
- synchronized(this) {
- try {
- if (System.currentTimeMillis() - time > timeToCheck) {
- time = System.currentTimeMillis()
- priceAlertRepository.getPriceAlerts()
- .doOnSuccess { priceAlert ->
- priceAlert.forEach { product ->
- if (product.upc.isNotEmpty()) {
- productRepository.getProduct(
- upc = product.upc
- )
- .doOnSuccess { updateProduct ->
- if (updateProduct.minPrice <= updateProduct.minPrice) {
- sendPush(updateProduct)
- }
- }
- .subscribeIgnoreResult()
- }
- else if (product.offerId.isNotEmpty()) {
- productRepository.getOffer(
- offerId = product.offerId
- )
- .doOnSuccess { updateProduct ->
- if (updateProduct.basePrice <= updateProduct.basePrice) {
- sendPush(updateProduct)
- }
- }
- .subscribeIgnoreResult()
- }
- }
- }
- .subscribeIgnoreResult()
- }
- } catch (e: Exception) { }
- }
- }
- }
- }
- override fun onCreate() {
- val thread = HandlerThread("ServiceStartArguments",
- Process.THREAD_PRIORITY_BACKGROUND)
- thread.start()
- Toothpick.openScopes(DI.DATA_SCOPE, DI.PRICE_ALERT_SERVICE_SCOPE).apply {
- Toothpick.inject(this@PriceAlertService, this)
- }
- mServiceLooper = thread.looper
- mServiceHandler = ServiceHandler(mServiceLooper!!)
- }
- override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
- val msg = mServiceHandler!!.obtainMessage()
- msg.arg1 = startId
- mServiceHandler!!.sendMessage(msg)
- return START_STICKY
- }
- override fun onBind(intent: Intent): IBinder? {
- return null
- }
- override fun onDestroy() {
- }
- private fun sendPush(product: Product) {
- val mBuilder = NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.ic_alert_green)
- .setContentTitle("New price: " + product.minPrice.toString() + "!")
- .setContentText(product.name)
- val notifyID = 1
- val resultIntent = Intent(this, MainActivity::class.java).putExtra("deepLink_product", product.upcs[0].upc)
- val stackBuilder = TaskStackBuilder.create(this)
- stackBuilder.addParentStack(MainActivity::class.java)
- stackBuilder.addNextIntent(resultIntent)
- val resultPendingIntent = stackBuilder.getPendingIntent(
- 0,
- PendingIntent.FLAG_UPDATE_CURRENT
- )
- mBuilder.setContentIntent(resultPendingIntent)
- val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- mNotificationManager.notify(notifyID, mBuilder.build())
- }
- private fun sendPush(offer: Offer) {
- val mBuilder = NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.ic_alert_green)
- .setContentTitle("New price: " + offer.basePrice.toString() + "!")
- .setContentText(offer.name)
- val notifyID = 1
- val resultIntent = Intent(this, MainActivity::class.java).putExtra("deepLink_offer", offer.id)
- val stackBuilder = TaskStackBuilder.create(this)
- stackBuilder.addParentStack(MainActivity::class.java)
- stackBuilder.addNextIntent(resultIntent)
- val resultPendingIntent = stackBuilder.getPendingIntent(
- 0,
- PendingIntent.FLAG_UPDATE_CURRENT
- )
- mBuilder.setContentIntent(resultPendingIntent)
- val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- mNotificationManager.notify(notifyID, mBuilder.build())
- }
- }
Add Comment
Please, Sign In to add comment