Like #556, there is room for discussion for this.
I prefer top-level function to (companion) object object class.
This opinion is based on Kotlin in Action(chapter 4.4.2).
public final class NotificationHelper {
public static final NotificationHelper INSTANCE;
public final void initNotificationChannel(@NotNull Context context) {
Intrinsics.checkParameterIsNotNull(context, "context");
if(VERSION.SDK_INT >= 26) {
Object[] $receiver$iv = (Object[])NotificationHelper.ChannelType.values();
int var3 = $receiver$iv.length;
for(int var4 = 0; var4 < var3; ++var4) {
Object element$iv = $receiver$iv[var4];
NotificationHelper.ChannelType it = (NotificationHelper.ChannelType)element$iv;
INSTANCE.createNotificationChannel(context, it);
}
}
}
@RequiresApi(26)
private final void createNotificationChannel(Context context, NotificationHelper.ChannelType channelType) {
Object var10000 = context.getSystemService("notification");
if(var10000 == null) {
throw new TypeCastException("null cannot be cast to non-null type android.app.NotificationManager");
} else {
NotificationManager manager = (NotificationManager)var10000;
String channelName = context.getString(channelType.getNameRes());
NotificationChannel channel = new NotificationChannel(channelType.getId(), (CharSequence)channelName, channelType.getImportance());
manager.createNotificationChannel(channel);
}
}
public final void showNotification(@NotNull Context context, @NotNull NotificationContent content) {
Intrinsics.checkParameterIsNotNull(context, "context");
Intrinsics.checkParameterIsNotNull(content, "content");
Notification notification = (new Builder(context, content.getChannelType().getId())).setContentTitle((CharSequence)content.getTitle()).setContentText((CharSequence)content.getText()).setFullScreenIntent(content.createPendingContentIntent(context), true).setAutoCancel(true).setColor(ContextCompat.getColor(context, 2131099750)).setSmallIcon(2131230882).build();
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(content.getText().hashCode(), notification);
}
static {
NotificationHelper var0 = new NotificationHelper();
INSTANCE = var0;
}
public static enum ChannelType {
FAVORITE_SESSION_START;
@NotNull
private final String id;
private final int nameRes;
private final int importance;
@NotNull
public final String getId() {
return this.id;
}
public final int getNameRes() {
return this.nameRes;
}
public final int getImportance() {
return this.importance;
}
protected ChannelType(@NotNull String id, @StringRes int nameRes, int importance) {
Intrinsics.checkParameterIsNotNull(id, "id");
super($enum_name_or_ordinal$0, $enum_name_or_ordinal$1);
this.id = id;
this.nameRes = nameRes;
this.importance = importance;
}
}
}
NotificationHelper is singleton.
After
public final class NotificationHelperKt {
public static final void initNotificationChannel(@NotNull Context $receiver) {
Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
if(VERSION.SDK_INT >= 26) {
Object[] $receiver$iv = (Object[])NotificationChannelType.values();
int var2 = $receiver$iv.length;
for(int var3 = 0; var3 < var2; ++var3) {
Object element$iv = $receiver$iv[var3];
NotificationChannelType p1 = (NotificationChannelType)element$iv;
createNotificationChannel($receiver, p1);
}
}
}
@RequiresApi(26)
private static final void createNotificationChannel(@NotNull Context $receiver, NotificationChannelType channelType) {
Object var10000 = $receiver.getSystemService("notification");
if(var10000 == null) {
throw new TypeCastException("null cannot be cast to non-null type android.app.NotificationManager");
} else {
((NotificationManager)var10000).createNotificationChannel(new NotificationChannel(channelType.getId(), (CharSequence)$receiver.getString(channelType.getNameRes()), channelType.getImportance()));
}
}
public static final void showNotification(@NotNull Context $receiver, @NotNull NotificationContent content) {
Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
Intrinsics.checkParameterIsNotNull(content, "content");
NotificationManagerCompat.from($receiver).notify(content.getText().hashCode(), (new Builder($receiver, content.getChannelType().getId())).setContentTitle((CharSequence)content.getTitle()).setContentText((CharSequence)content.getText()).setFullScreenIntent(content.createPendingContentIntent($receiver), true).setAutoCancel(true).setColor(ContextCompat.getColor($receiver, 2131099750)).setSmallIcon(2131230882).build());
}
}
public enum NotificationChannelType {
FAVORITE_SESSION_START;
@NotNull
private final String id;
private final int nameRes;
private final int importance;
@NotNull
public final String getId() {
return this.id;
}
public final int getNameRes() {
return this.nameRes;
}
public final int getImportance() {
return this.importance;
}
protected NotificationChannelType(@NotNull String id, @StringRes int nameRes, int importance) {
Intrinsics.checkParameterIsNotNull(id, "id");
super($enum_name_or_ordinal$0, $enum_name_or_ordinal$1);
this.id = id;
this.nameRes = nameRes;
this.importance = importance;
}
}
Other change.
context parameter of BroadcastReceiver::onReceive is changed to Non-Null.
This is because it is assumed that it will not become Non-Null.
Even if it falls to Null, it falls before the calling context (falls by Intrinsics.checkParameterIsNotNull(context, "context");), so you can quickly find a bug.
I guess that this also is certainly described in Kotlin in Action.
Issue
Overview (Required)
Like #556, there is room for discussion for this. I prefer top-level function to (companion) object object class. This opinion is based on
Kotlin in Action
(chapter 4.4.2).The detailed explanation is in the following blog(in Japanese). http://hydrakecat.hatenablog.jp/entry/2017/12/02/companion_object_vs._top-level
Changes
These are decompiled java code.
Before
NotificationHelper
is singleton.After
Other change.
context
parameter ofBroadcastReceiver::onReceive
is changed to Non-Null.This is because it is assumed that it will not become Non-Null. Even if it falls to Null, it falls before the calling
context
(falls byIntrinsics.checkParameterIsNotNull(context, "context");
), so you can quickly find a bug. I guess that this also is certainly described inKotlin in Action
.Links
Screenshot
Nothing has changed. And this changes is hard to test π .