From 421a2f8317731b96a259d8f7b63640fe13113ec9 Mon Sep 17 00:00:00 2001 From: Karn Saheb Date: Sun, 29 Apr 2018 23:11:32 -0400 Subject: [PATCH] README updates, breaking changes. The README has been updated with a new anatomy.svg asset which explicitly relies on outlines as opposed to fonts. Additionally, there are two breaking changes. - `Creator#getBuilder()` is now `Creator#asBuilder()` which is inline with the function of the same name in `Notify`. - `Creator#send()` and `Notify#send()` are now `Creator#show()` and `Notify#show()` respectively. --- README.md | 3 +- docs/assets/anatomy.svg | 194 ++++++++++-------- .../src/main/java/io/karn/notify/Creator.kt | 42 ++-- .../src/main/java/io/karn/notify/Notify.kt | 5 +- .../java/io/karn/notify/NotifyActionsTest.kt | 4 +- .../java/io/karn/notify/NotifyContentTest.kt | 10 +- .../java/io/karn/notify/NotifyHeaderTest.kt | 4 +- .../main/java/presentation/MainActivity.kt | 10 +- 8 files changed, 146 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index ce5b565..fb76798 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Notify ![Basic usecase](./docs/assets/default.svg) +If you run into a case in which the library does not provide the requisite builder functions you can get the `NotificationCompat.Builder` object and continue to use it as you would normally by calling `Creator#asBuilder()`. ###### NOTIFICATION ANATOMY @@ -56,7 +57,7 @@ Notify | ID | Name | Description | | ---- | ------------ | ------------------------------------------------------------------------------------------------------- | | 1 | Icon | Set using the `Header#icon` field. | -| 2 | App Name | Application Icon, immutable. | +| 2 | App Name | Application name, immutable. | | 3 | Header Text | Optional description text. Set using the `Header#headerText` field. | | 4 | Timestamp | Timestamp of the notification. | | 5 | Expand Icon | Indicates that the notification is expandable. | diff --git a/docs/assets/anatomy.svg b/docs/assets/anatomy.svg index 384544a..a46411f 100644 --- a/docs/assets/anatomy.svg +++ b/docs/assets/anatomy.svg @@ -13,106 +13,126 @@ + + + + + + + + + - - - - - - SAVE FOR LATER - - - Chocolate brownie sundae - - - Try our newest dessert option! - - - - - now - - - + + + + + + + + + + + + + + - - - - Dessert - - - - Notify - - - - + + + + + + + + - - - - - - 1 - + + + + + 1 + + + - - - - - - - 2 - + + + + + 2 + + + - - - - - - - - 3 - + + + + + + 3 + + - - - - - - 4 - + + + + + 4 + + + - - - - - - - - 5 - + + + + + + 5 + + - - - - - - - 6 - + + + + + + 6 + + + + + + + + + 7 + + - - - - - - 7 - + + + + + + + + + + + + + + + + + + + diff --git a/library/src/main/java/io/karn/notify/Creator.kt b/library/src/main/java/io/karn/notify/Creator.kt index afd1ef4..7cb5caa 100644 --- a/library/src/main/java/io/karn/notify/Creator.kt +++ b/library/src/main/java/io/karn/notify/Creator.kt @@ -21,8 +21,8 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * Scoped function for modifying the Metadata of a notification, such as click intents, * notification category, and priority among other options. */ - fun meta(meta: Payload.Meta.() -> Unit): Creator { - meta(this.meta) + fun meta(init: Payload.Meta.() -> Unit): Creator { + this.meta.init() return this } @@ -32,8 +32,8 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * modification of the notificationIcon, color, the headerText (optional text next to the * appName), and finally the channel of the notification if targeting Android O. */ - fun header(header: Payload.Header.() -> Unit): Creator { - header(this.header) + fun header(init: Payload.Header.() -> Unit): Creator { + this.header.init() return this } @@ -41,45 +41,45 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon /** * Scoped function for modifying the content of a 'Default' notification. */ - fun content(block: Payload.Content.Default.() -> Unit): Creator { + fun content(init: Payload.Content.Default.() -> Unit): Creator { this.content = Payload.Content.Default() - block(this.content as Payload.Content.Default) + (this.content as Payload.Content.Default).init() return this } /** * Scoped function for modifying the content of a 'TextList' notification. */ - fun asTextList(block: Payload.Content.TextList.() -> Unit): Creator { + fun asTextList(init: Payload.Content.TextList.() -> Unit): Creator { this.content = Payload.Content.TextList() - block(this.content as Payload.Content.TextList) + (this.content as Payload.Content.TextList).init() return this } /** * Scoped function for modifying the content of a 'BigText' notification. */ - fun asBigText(block: Payload.Content.BigText.() -> Unit): Creator { + fun asBigText(init: Payload.Content.BigText.() -> Unit): Creator { this.content = Payload.Content.BigText() - block(this.content as Payload.Content.BigText) + (this.content as Payload.Content.BigText).init() return this } /** * Scoped function for modifying the content of a 'BigPicture' notification. */ - fun asBigPicture(block: Payload.Content.BigPicture.() -> Unit): Creator { + fun asBigPicture(init: Payload.Content.BigPicture.() -> Unit): Creator { this.content = Payload.Content.BigPicture() - block(this.content as Payload.Content.BigPicture) + (this.content as Payload.Content.BigPicture).init() return this } /** * Scoped function for modifying the content of a 'Message' notification. */ - fun asMessage(block: Payload.Content.Message.() -> Unit): Creator { + fun asMessage(init: Payload.Content.Message.() -> Unit): Creator { this.content = Payload.Content.Message() - block(this.content as Payload.Content.Message) + (this.content as Payload.Content.Message).init() return this } @@ -87,9 +87,9 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * Scoped function for modifying the 'Actions' of a notification. The transformation * relies on adding standard notification Action objects. */ - fun actions(block: ArrayList.() -> Unit): Creator { + fun actions(init: ArrayList.() -> Unit): Creator { this.actions = ArrayList() - block(this.actions as ArrayList) + (this.actions as ArrayList).init() return this } @@ -97,9 +97,9 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * Scoped function for modifying the behaviour of 'Stacked' notifications. The transformation * relies on the 'summaryText' of a stackable notification. */ - fun stackable(block: Payload.Stackable.() -> Unit): Creator { + fun stackable(init: Payload.Stackable.() -> Unit): Creator { this.stackable = Payload.Stackable() - block(this.stackable as Payload.Stackable) + (this.stackable as Payload.Stackable).init() return this } @@ -107,7 +107,7 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * Return the standard {@see NotificationCompat.Builder} after applying fluent API * transformations (if any) from the {@see Creator} builder object. */ - fun getBuilder(): NotificationCompat.Builder { + fun asBuilder(): NotificationCompat.Builder { return notify.asBuilder(RawNotification(meta, header, content, stackable, actions)) } @@ -120,7 +120,7 @@ class Creator internal constructor(private val notify: Notify, config: NotifyCon * @return An integer corresponding to the ID of the system notification. Any updates should use * this returned integer to make updates or to cancel the notification. */ - fun send(): Int { - return notify.send(getBuilder()) + fun show(): Int { + return notify.show(asBuilder()) } } diff --git a/library/src/main/java/io/karn/notify/Notify.kt b/library/src/main/java/io/karn/notify/Notify.kt index 01eb3b9..f0b5f7d 100644 --- a/library/src/main/java/io/karn/notify/Notify.kt +++ b/library/src/main/java/io/karn/notify/Notify.kt @@ -31,8 +31,7 @@ class Notify internal constructor(internal var context: Context) { /** * Modify the default configuration. * - * Takes a function that receives a lambda with the NotifyConfig immutable object which has - * mutable fields. + * Takes a receiver with the NotifyConfig immutable object which has mutable fields. */ fun defaultConfig(block: (NotifyConfig) -> Unit) { block(defaultConfig) @@ -78,7 +77,7 @@ class Notify internal constructor(internal var context: Context) { * @return An integer corresponding to the ID of the system notification. Any updates should use * this returned integer to make updates or to cancel the notification. */ - internal fun send(builder: NotificationCompat.Builder): Int { + internal fun show(builder: NotificationCompat.Builder): Int { return NotificationInterop.showNotification(context, builder) } } diff --git a/library/src/test/java/io/karn/notify/NotifyActionsTest.kt b/library/src/test/java/io/karn/notify/NotifyActionsTest.kt index a2b35b4..8687b01 100644 --- a/library/src/test/java/io/karn/notify/NotifyActionsTest.kt +++ b/library/src/test/java/io/karn/notify/NotifyActionsTest.kt @@ -19,7 +19,7 @@ class NotifyActionsTest { title = "New dessert menu" text = "The Cheesecake Factory has a new dessert for you to try!" } - .getBuilder() + .asBuilder() .build() Assert.assertNull(notification.actions) @@ -39,7 +39,7 @@ class NotifyActionsTest { null )) } - .getBuilder() + .asBuilder() .build() Assert.assertNotNull(notification.actions) diff --git a/library/src/test/java/io/karn/notify/NotifyContentTest.kt b/library/src/test/java/io/karn/notify/NotifyContentTest.kt index f6425c7..a13ebc2 100644 --- a/library/src/test/java/io/karn/notify/NotifyContentTest.kt +++ b/library/src/test/java/io/karn/notify/NotifyContentTest.kt @@ -69,7 +69,7 @@ class NotifyContentTest { title = testTitle text = testText } - .getBuilder() + .asBuilder() .build() Assert.assertNull(notification.extras.getCharSequence(NotificationCompat.EXTRA_TEMPLATE)) @@ -95,7 +95,7 @@ class NotifyContentTest { title = testTitle text = testText } - .getBuilder() + .asBuilder() .build() Assert.assertEquals("android.app.Notification\$InboxStyle", notification.extras.getCharSequence(NotificationCompat.EXTRA_TEMPLATE).toString()) @@ -121,7 +121,7 @@ class NotifyContentTest { collapsedText = testExpandedText bigText = testBigText } - .getBuilder() + .asBuilder() .build() Assert.assertEquals("android.app.Notification\$BigTextStyle", notification.extras.getCharSequence(NotificationCompat.EXTRA_TEMPLATE).toString()) @@ -144,7 +144,7 @@ class NotifyContentTest { text = testText image = testImage } - .getBuilder() + .asBuilder() .build() Assert.assertEquals("android.app.Notification\$BigPictureStyle", notification.extras.getCharSequence(NotificationCompat.EXTRA_TEMPLATE).toString()) @@ -183,7 +183,7 @@ class NotifyContentTest { conversationTitle = testConversationTitle messages = testMessages } - .getBuilder() + .asBuilder() .build() Assert.assertEquals("android.app.Notification\$MessagingStyle", notification.extras.getCharSequence(NotificationCompat.EXTRA_TEMPLATE).toString()) diff --git a/library/src/test/java/io/karn/notify/NotifyHeaderTest.kt b/library/src/test/java/io/karn/notify/NotifyHeaderTest.kt index 367ce62..1c8dfe9 100644 --- a/library/src/test/java/io/karn/notify/NotifyHeaderTest.kt +++ b/library/src/test/java/io/karn/notify/NotifyHeaderTest.kt @@ -21,7 +21,7 @@ class NotifyHeaderTest { title = "New dessert menu" text = "The Cheesecake Factory has a new dessert for you to try!" } - .getBuilder() + .asBuilder() .build() Assert.assertEquals(Icon.createWithResource(this.context, R.drawable.ic_app_icon).describeContents(), notification.smallIcon.describeContents()) @@ -48,7 +48,7 @@ class NotifyHeaderTest { title = "New dessert menu" text = "The Cheesecake Factory has a new dessert for you to try!" } - .getBuilder() + .asBuilder() .build() Assert.assertEquals(Icon.createWithResource(this.context, testIcon).describeContents(), notification.smallIcon.describeContents()) diff --git a/sample/src/main/java/presentation/MainActivity.kt b/sample/src/main/java/presentation/MainActivity.kt index dcf06e1..0dc22ff 100644 --- a/sample/src/main/java/presentation/MainActivity.kt +++ b/sample/src/main/java/presentation/MainActivity.kt @@ -41,7 +41,7 @@ class MainActivity : AppCompatActivity() { null )) } - .send() + .show() } fun notifyTextList(view: View) { @@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity() { title = "New menu items!" text = lines.size.toString() + " new dessert menu items found." } - .send() + .show() } @@ -69,7 +69,7 @@ class MainActivity : AppCompatActivity() { "\n" + "Come try this delicious new dessert and get two for the price of one!" } - .send() + .show() } fun notifyBigPicture(view: View) { @@ -80,7 +80,7 @@ class MainActivity : AppCompatActivity() { text = "Get a look at this amazing dessert!" image = BitmapFactory.decodeResource(this@MainActivity.resources, R.drawable.chocolate_brownie_sundae) } - .send() + .show() } fun notifyMessage(view: View) { @@ -101,6 +101,6 @@ class MainActivity : AppCompatActivity() { "Moez") ) } - .send() + .show() } }