Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.32 KB | None | 0 0
  1. package pro.growfood.android.receiver
  2.  
  3. import android.content.BroadcastReceiver
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.net.Uri
  7. import android.os.Bundle
  8. import androidx.annotation.IdRes
  9. import androidx.annotation.NavigationRes
  10. import androidx.core.content.ContextCompat.startActivity
  11. import androidx.navigation.NavDeepLinkBuilder
  12. import com.exponea.sdk.models.NotificationAction
  13. import com.exponea.sdk.services.ExponeaPushReceiver
  14. import org.koin.core.KoinComponent
  15. import org.koin.core.inject
  16. import pro.growfood.android.R
  17. import pro.growfood.android.base.BaseActivity
  18. import pro.growfood.android.extentions.openAppInGooglePlay
  19. import pro.growfood.android.extentions.orFalse
  20. import pro.growfood.android.model.interactors.PropertyInteractor
  21. import pro.growfood.android.view.profile.PageRouter
  22. import pro.growfood.android.view.profile.ProfileViewModel
  23. import timber.log.Timber
  24.  
  25. class ExponeaReceiver : BroadcastReceiver(), KoinComponent {
  26.  
  27.     private val viewModel: ProfileViewModel by inject()
  28.     private val propertyInteractor: PropertyInteractor by inject()
  29.  
  30.     override fun onReceive(context: Context, intent: Intent) {
  31.         val action = intent.getSerializableExtra(ExponeaPushReceiver.EXTRA_ACTION_INFO) as? NotificationAction?
  32.         val buttonClickedIntent = Intent(Intent.ACTION_VIEW)
  33.         buttonClickedIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  34.         val url  = action?.url
  35.         Timber.tag("testLog").d("message ")
  36.         if (url != null && url.isNotEmpty())
  37.             buttonClickedIntent.data = Uri.parse(url)
  38.         if (url?.contains("flutter").orFalse()) {
  39.             val sessionId = url?.split("=")?.get(1).orEmpty()
  40.             viewModel.getProfileValue { profile ->
  41.                 PageRouter.openPageByUrl(context, "gf://flutter/nps",
  42.                     hashMapOf(
  43.                         "profile" to profile,
  44.                         "sessionId" to sessionId,
  45.                         "client_token" to propertyInteractor.getToken(),
  46.                         "baseUrl" to "https://admin.growfood.pro/api/mobile-apps/v2_10/"
  47.                     )
  48.                 )
  49.             }
  50.             return
  51.         }
  52.         when(intent.action) {
  53.             ExponeaPushReceiver.ACTION_CLICKED -> {
  54.                 val launchIntent = Intent(context, BaseActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK }
  55.                 startActivity(context, launchIntent, null)
  56.             }
  57.             ExponeaPushReceiver.ACTION_DEEPLINK_CLICKED -> {
  58.                 if (url != null && url.isNotEmpty()) {
  59.                     deeplinkHandler(context, url)
  60.                 }
  61.                 else {
  62.                     deepLinkNavigate(context, Bundle(), R.navigation.active_orders, R.id.supertFtragment)
  63.                 }
  64.             }
  65.  
  66.         }
  67.     }
  68.  
  69.     private fun deeplinkHandler(context: Context, url: String) {
  70.         val args = Bundle()
  71.         val uri = Uri.parse(url)
  72.         val server = uri.authority
  73. //        uri.pathSegments
  74.  
  75.         when(server){
  76.             SERVER_ORDERS -> {}
  77.             SERVER_RATE -> {}
  78. //                    SERVER_PROFILE -> {deepLinkNavigate(context, args, R.navigation.profile, R.id.profile)}
  79.             SERVER_NEW_ORDER -> {}
  80.             SERVER_RATE_APP -> context.openAppInGooglePlay()
  81.             else -> deepLinkNavigate(context, args, R.navigation.active_orders, R.id.supertFtragment)
  82.         }
  83.     }
  84.  
  85.     private fun deepLinkNavigate(context: Context, args: Bundle, @NavigationRes navGraphId: Int, @IdRes destId: Int) {
  86.         NavDeepLinkBuilder(context)
  87.             .setGraph(navGraphId)
  88.             .setDestination(destId)
  89.             .setArguments(args)
  90.             .createPendingIntent()
  91.             .send()
  92.     }
  93.  
  94.     companion object{
  95.         const val SERVER_ORDERS = "orders"
  96.         const val SECOND_SEGMENT_MENU = "menu"
  97.         const val SECOND_SEGMENT_LONGATE = "longate"
  98.  
  99.         const val SERVER_RATE = "rate"
  100.         const val SECOND_SEGMENT_DELIVERY = "delivery"
  101.         const val SECOND_SEGMENT_DISH = "dish"
  102.  
  103.         const val SERVER_PROFILE = "profile"
  104.         const val SERVER_CHAT = "chat"
  105.         const val SERVER_NEW_ORDER = "neworder"
  106.         const val SERVER_RATE_APP = "rateApp"
  107.  
  108.     }
  109.  
  110. //    override fun onReceive(p0: Context?, p1: Intent?) {
  111. //
  112. //    }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement