Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created April 20, 2017 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/b59b1cb1510fbcde9ae877da6af6c298 to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/b59b1cb1510fbcde9ae877da6af6c298 to your computer and use it in GitHub Desktop.
//DataRepository.kt, which handles all db handling, where I have passed 3 callbacks, finished, error or maximum tabs reached
public fun addTab(tabModelToInsert: TabModel,
onFinished: (tabModel: TabModel) -> Unit = {},
onError: (tabModel: TabModel, message: String) -> Unit = { tabModel, message -> },
onMaxTabReached: (tabModel: TabModel) -> Unit = {}) {
if (canAddMoreTabs()) {
val currentMaxId = getCurrentMaxId() ?: 0
try {
realm.executeTransaction {
addTabModelToRealm()
onFinished(tabModel)
}
} catch(e: Exception) {
onError(tabModelToInsert, e.toString())
v("Error in inserting tab")
}
} else {
onMaxTabReached(tabModelToInsert)
}
}
//In Main activity view model
dataRepository?.addTab(tabModel, onFinished = {
pageAdapter?.addTab(it)
}, onError = { tabModel, error ->
FirebaseCrash.log(error)
}, onMaxTabReached = {
FirebaseAnalytics.getInstance(activity).logEvent("AddingTabsMaxEventReached", null)
showSnackbar(view)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment