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
| fun main(args: Array<String>) { | |
| RequestBody(null, 25) | |
| } | |
| data class RequestBody(val name: String?, val age: Int?) { | |
| init { | |
| checkNotNull(name) { | |
| "This can't be null" // Custom message for IllegalStateException |
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
| Kotlin builder pattern best practice: | |
| https://medium.com/@piotr.slesarew/dsl-builder-in-kotlin-be5816ba3ca7 |
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
| NotificationManagerCompat.from(context).apply { | |
| notify(101, NotificationCompat.Builder(context).run { | |
| setStyle(NotificationCompatV4.InboxStyle().addLine("You have new message")) | |
| setContentTitle(getNotificationTitleForPreNougat(dialog, dialogMessages)) | |
| setAutoCancel(true) | |
| setContentIntent(getPendingIntent())) | |
| setSmallIcon(R.drawable.ic_launcher) | |
| build() | |
| }) |
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
| object Api { | |
| val instance: RestAPI = Retrofit.Builder().run { | |
| baseUrl(AppConst.API.BASE_URL) | |
| addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | |
| addConverterFactory(JacksonConverterFactory.create(ObjectMapper())) | |
| client(OkHttpClient.Builder() | |
| .apply { | |
| addInterceptor(HttpLoggingInterceptor() | |
| .apply { level = HttpLoggingInterceptor.Level.BODY }) | |
| .takeIf { BuildConfig.DEBUG } |
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
| class Factory { | |
| public static Api create() { | |
| OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
| if (BuildConfig.DEBUG) { | |
| HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
| logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
| httpClient.addInterceptor(logging); | |
| } | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) |
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
| NotificationCompat.Builder(this).apply builder@ { // object 1 reference | |
| setSmallIcon(R.drawable.ic_launcher) | |
| setContentTitle("My notification") | |
| setContentText("Hello world!") | |
| Intent(this, HomeActivity::class.java).apply resultIntent@ { // object 2 reference | |
| TaskStackBuilder.create(this).apply { | |
| addParentStack(HomeActivity::class.java) | |
| addNextIntent(this@resultIntent) | |
| [email protected](getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)) |
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
| // object 1 | |
| NotificationCompat.Builder mBuilder = | |
| new NotificationCompat.Builder(this) | |
| .setSmallIcon(R.drawable.notification_icon) | |
| .setContentTitle("My notification") | |
| .setContentText("Hello World!"); | |
| // object 2 | |
| Intent resultIntent = new Intent(this, ResultActivity.class); |
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
| val contact = ContactModel() | |
| val isSuccess = contact.run { | |
| var isSuccess = false | |
| try { | |
| ActiveAndroid.beginTransaction() | |
| groupName = "GROUP NAME" | |
| type "PRIVATE" | |
| subject = "PHYSICS GROUP" | |
| save() |
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
| data class Person(var name: String? = null, var address: Address? = null, var age: Int? = null, var sex: String? = null, var lat: Double? = null, var lng: Double? = null) | |
| data class Address(var streetName: String? = null, var pinCode: String? = null) | |
| Person().apply { | |
| name = "PERSON NAME" | |
| sex = "MALE" | |
| address = Address().apply { | |
| streetName = "STREET NAME" | |
| pinCode = "560029" | |
| } |
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
| AlertDialog.Builder(this).apply { | |
| setMessage("Kotlin message") | |
| this.setPositiveButton("button 1") { dialog, _ -> | |
| dialog.dismiss() | |
| } | |
| setNegativeButton("button 2") { dialog, _ -> | |
| dialog.dismiss() | |
| } | |
| create() | |
| show() |