Skip to content

Instantly share code, notes, and snippets.

@PetkevichPavel
Created August 24, 2019 08:22
Show Gist options
  • Save PetkevichPavel/edca3956388b1efdb2af64bf8c66a70c to your computer and use it in GitHub Desktop.
Save PetkevichPavel/edca3956388b1efdb2af64bf8c66a70c to your computer and use it in GitHub Desktop.
AN_Cloud Function: Base activity of the application.
abstract class BaseActivity : AppCompatActivity() {
companion object {
private const val TAG = "BaseActivity:"
private const val RECEIVER_TAG = "BaseActivity-Receiver:"
}
private val localBroadcastManager: LocalBroadcastManager by lazy {
LocalBroadcastManager.getInstance(this)
}
private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Get extra data included in the Intent
intent.extras?.apply {
getString(IN_AP_EX_STR_BODY)?.let { body ->
contentView?.snack(body)
}
Timber.tag(RECEIVER_TAG).i("Receive FCM message: $this")
} ?: run {
Timber.tag(RECEIVER_TAG).i("Receive FCM message for remote config.")
fetchRC()
}
}
}
override fun onResume() {
super.onResume()
//Register the broadcast manager.
localBroadcastManager.registerReceiver(mMessageReceiver, IntentFilter(INTENT_FCM))
// Here I will control if it's time to fetch the RC.
app?.remoteConfigManager?.lastFetchedTime?.let {
if (it.isTimeToFetch() || app?.remoteConfigManager?.isRCMSetByDefault == true) fetchRC()
} ?: fetchRC()
//Here I detect if the user come into the application via
//tap on notification and on this notification concerns Remote //configuration I will fetch new RC.
intent.extras?.getString(RC_EXTRA_BACK_NOTIFICATION)?.let {
fetchRC()
}
}
private fun fetchRC() = app?.getRCF()?.fetchRemoteConfig { isItServiceUpdate, isFeatureOnBoardingVisible ->
handleRcUpdate(isItServiceUpdate,isFeatureOnBoardingVisible)
}
override fun onPause() {
super.onPause()
localBroadcastManager.unregisterReceiver(mMessageReceiver)
}
//The open function which I will override in other classes which inherit the BaseActivity.
open fun handleRcUpdate(isItServiceUpdate: Boolean, isFeatureOnBoardingVisible: Boolean) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment