Created
April 20, 2017 05:54
-
-
Save PrashamTrivedi/b59b1cb1510fbcde9ae877da6af6c298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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