Last active
April 12, 2017 05:58
-
-
Save PrashamTrivedi/64b6f23521b0bf4fcf37a569d3dd0f0f to your computer and use it in GitHub Desktop.
Kotlin examples for my post
This file contains hidden or 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) | |
}) |
This file contains hidden or 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
//Part of Functions.kt, this and below 3 functions helps to create dialog. | |
public fun Context.showDialog(cancelable: Boolean = false, cancelableTouchOutside: Boolean = false, builderFunction: AlertDialog.Builder.() -> Any) { | |
val builder = AlertDialog.Builder(this) | |
builder.builderFunction() | |
val dialog = builder.create(); | |
dialog.setCancelable(cancelable) | |
dialog.setCanceledOnTouchOutside(cancelableTouchOutside) | |
dialog.show() | |
} | |
public fun AlertDialog.Builder.positiveButton(text: String = "OK", handleClick: (i: Int) -> Unit = {}) { | |
this.setPositiveButton(text, { dialogInterface, i -> handleClick(i) }) | |
} | |
public fun AlertDialog.Builder.negativeButton(text: String = "CANCEL", handleClick: (i: Int) -> Unit = {}) { | |
this.setNegativeButton(text, { dialogInterface, i -> handleClick(i) }) | |
} | |
public fun AlertDialog.Builder.neutralButton(text: String, handleClick: (i: Int) -> Unit = {}) { | |
this.setNeutralButton(text, { dialogInterface, i -> handleClick(i) }) | |
} | |
//Sample of this code in MainActivityViewModel | |
activity.showDialog { | |
setTitle("Pick Path") | |
positiveButton("Pick Files", { pickPathToRead() }) | |
negativeButton() | |
} |
This file contains hidden or 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
//Functions.kt | |
public fun Context.setNotification(id: Int = 0, builderMethod: NotificationCompat.Builder.() -> Any) { | |
val builder = NotificationCompat.Builder(this) | |
builder.apply { | |
builderMethod() | |
} | |
notificationManager().notify(id, builder.build()) | |
} | |
//FCM Service.kt | |
[email protected](2488, { | |
setContentTitle(it.title) | |
setContentText(it.body) | |
setColor(R.color.colorPrimary) | |
setSmallIcon(R.drawable.ic_notification_icon) | |
setAutoCancel(true) | |
val bigTextStyle = NotificationCompat.BigTextStyle(this) | |
bigTextStyle.setBigContentTitle(it.title) | |
bigTextStyle.bigText(it.body) | |
bigTextStyle.setSummaryText(it.body) | |
setStyle(bigTextStyle) | |
val intent = Intent(this@MyFCMService, MainActivity::class.java) | |
intent.putExtras(remoteMessage.data.extractBundle()) | |
val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this@MyFCMService) | |
stackBuilder.addNextIntent(intent) | |
val pendingIntent = stackBuilder.getPendingIntent(2488, PendingIntent.FLAG_UPDATE_CURRENT) | |
setContentIntent(pendingIntent) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment