-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
328 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
72 changes: 72 additions & 0 deletions
72
demo/src/main/java/com/tinder/app/socketio/chatroomservice/view/ChatRoomServiceFragment.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* © 2019 Match Group, LLC. | ||
*/ | ||
|
||
package com.tinder.app.socketio.chatroomservice.view | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.os.Bundle | ||
import android.os.IBinder | ||
import android.os.Messenger | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.tinder.R | ||
import com.tinder.service.ChatRoomSocketIoService | ||
import timber.log.Timber | ||
|
||
class ChatRoomServiceFragment : Fragment() { | ||
|
||
private var messenger: Messenger? = null | ||
private var isBound = false | ||
|
||
private val serviceConnection = object : ServiceConnection { | ||
|
||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) { | ||
messenger = Messenger(service) | ||
isBound = true | ||
|
||
Timber.d("chat onServiceConnected") | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName?) { | ||
messenger = null | ||
isBound = false | ||
|
||
Timber.d("chat onServiceDisconnected") | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
val view = inflater.inflate(R.layout.fragment_chatroom_service, container, false) as View | ||
return view | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
bindService() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
unbindService() | ||
} | ||
|
||
private fun bindService() { | ||
val intent = Intent(activity, ChatRoomSocketIoService::class.java) | ||
activity?.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) | ||
} | ||
|
||
private fun unbindService() { | ||
activity?.unbindService(serviceConnection) | ||
isBound = false | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
demo/src/main/java/com/tinder/service/ChatRoomSocketIoService.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* © 2018 Match Group, LLC. | ||
*/ | ||
|
||
package com.tinder.service | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Intent | ||
import android.os.Handler | ||
import android.os.IBinder | ||
import android.os.Messenger | ||
import androidx.lifecycle.LifecycleService | ||
import com.tinder.R | ||
import com.tinder.app.socketio.chatroom.api.AddUserTopic | ||
import com.tinder.app.socketio.chatroom.api.ChatRoomService | ||
import com.tinder.app.socketio.chatroom.api.NewMessageTopic | ||
import com.tinder.app.socketio.chatroom.api.TypingStartedTopic | ||
import com.tinder.app.socketio.chatroom.api.TypingStoppedTopic | ||
import com.tinder.app.socketio.chatroom.api.UserJoinedTopic | ||
import com.tinder.app.socketio.chatroom.api.UserLeftTopic | ||
import com.tinder.scarlet.Scarlet | ||
import com.tinder.scarlet.lifecycle.android.AndroidLifecycle | ||
import com.tinder.scarlet.messageadapter.moshi.MoshiMessageAdapter | ||
import com.tinder.scarlet.socketio.SocketIoEvent | ||
import com.tinder.scarlet.socketio.client.SocketIoClient | ||
import com.tinder.scarlet.socketio.client.SocketIoEventName | ||
import com.tinder.scarlet.streamadapter.rxjava2.RxJava2StreamAdapterFactory | ||
import io.reactivex.Flowable | ||
import io.reactivex.schedulers.Schedulers | ||
import timber.log.Timber | ||
|
||
class ChatRoomSocketIoService : LifecycleService() { | ||
|
||
var incomingMessageCount = 0 | ||
private val messenger = Messenger(IncomingHandler()) | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
val config = Scarlet.Configuration( | ||
lifecycle = AndroidLifecycle.ofLifecycleServiceForeground(application, this), | ||
messageAdapterFactories = listOf(MoshiMessageAdapter.Factory()), | ||
streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()) | ||
) | ||
val serverUrl = "https://socket-io-chat.now.sh/" | ||
|
||
val scarlet = Scarlet( | ||
SocketIoClient({ serverUrl }), | ||
config | ||
) | ||
|
||
val chatRoomService = scarlet.create<ChatRoomService>() | ||
val addUserTopic = scarlet | ||
.child(SocketIoEventName("add user"), config) | ||
.create<AddUserTopic>() | ||
val newMessageTopic = scarlet | ||
.child(SocketIoEventName("new message"), config) | ||
.create<NewMessageTopic>() | ||
val typingStartedTopic = scarlet | ||
.child(SocketIoEventName("typing"), config) | ||
.create<TypingStartedTopic>() | ||
val typingStoppedTopic = scarlet | ||
.child(SocketIoEventName("stop typing"), config) | ||
.create<TypingStoppedTopic>() | ||
val userJoinedTopic = scarlet | ||
.child(SocketIoEventName("user joined"), config) | ||
.create<UserJoinedTopic>() | ||
val userLeftTopic = scarlet | ||
.child(SocketIoEventName("user left"), config) | ||
.create<UserLeftTopic>() | ||
|
||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
val notificationChannel = | ||
NotificationChannel(channelId, "Default", NotificationManager.IMPORTANCE_HIGH) | ||
notificationManager.createNotificationChannel(notificationChannel) | ||
|
||
Timber.d("chatroom scarlet created") | ||
|
||
chatRoomService.observeStateTransition() | ||
.subscribe { | ||
Timber.d("chatroom service: $it") | ||
|
||
val notification = createNotification { | ||
setContentText("State Transition ${it.toState}") | ||
} | ||
notificationManager.notify(notificationIdCurrentState, notification) | ||
} | ||
|
||
val username = "scarlet service" | ||
|
||
addUserTopic.observeSocketIoEvent() | ||
.filter { it is SocketIoEvent.OnConnectionOpened } | ||
.observeOn(Schedulers.io()) | ||
.subscribe({ | ||
addUserTopic.sendAddUser(username) | ||
|
||
Timber.d("chatroom added user: $it") | ||
val notification = createNotification { | ||
setContentText("Joined chatroom") | ||
} | ||
notificationManager.notify(notificationIdCurrentState, notification) | ||
}, { e -> | ||
Timber.e(e) | ||
}) | ||
|
||
Flowable.fromPublisher(AndroidLifecycle.ofLifecycleServiceForeground(application, this)) | ||
.subscribe({ | ||
Timber.d("chatroom lifecycle: $it") | ||
|
||
val notification = createNotification { | ||
setContentText("Lifecycle State: $it") | ||
} | ||
notificationManager.notify(notificationIdCurrentState, notification) | ||
}) | ||
|
||
Flowable.merge( | ||
newMessageTopic.observeNewMessage().map { "${it.username}: ${it.message}" }, | ||
typingStartedTopic.observeTypingStarted().map { "${it.username} started typing" }, | ||
typingStoppedTopic.observeTypingStopped().map { "${it.username} stopped typing" } | ||
).subscribe { | ||
Timber.d("chatroom new message: $it") | ||
|
||
val notification = createNotification { | ||
setContentText("$it") | ||
} | ||
|
||
val notificationId = notificationIdIncomingMessage + incomingMessageCount | ||
incomingMessageCount += 1 | ||
notificationManager.notify(notificationId, notification) | ||
} | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
super.onBind(intent) | ||
Timber.d("chatroom onbind") | ||
return messenger.binder | ||
} | ||
|
||
private fun createNotification(builder: Notification.Builder.() -> Unit): Notification { | ||
return Notification.Builder(applicationContext, channelId) | ||
.setSmallIcon(R.drawable.ic_action_info) | ||
.setWhen(System.currentTimeMillis()) | ||
.setContentTitle("Chat Room") | ||
.setAutoCancel(true) | ||
.setOngoing(true) | ||
.apply(builder) | ||
.build() | ||
} | ||
|
||
private inner class IncomingHandler : Handler() | ||
|
||
companion object { | ||
const val notificationIdCurrentState = 10000 | ||
const val notificationIdIncomingMessage = 10001 | ||
const val channelId = "default" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ © 2019 Match Group, LLC. | ||
--> | ||
|
||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<TextView | ||
android:id="@+id/stockName" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="@dimen/space_l" | ||
android:layout_marginEnd="@dimen/space_xs" | ||
android:layout_marginStart="@dimen/space_xxl" | ||
android:layout_gravity="center" | ||
android:autoLink="web" | ||
android:linksClickable="true" | ||
android:lines="10" | ||
android:text="Interact with this demo at\n https://socket-io-chat.now.sh/\nIncoming Messages will show up as notifications" /> | ||
|
||
</LinearLayout> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
|
||
android.enableJetifier=true | ||
android.useAndroidX=true | ||
version=0.2.2-alpha4 | ||
version=0.2.3-alpha1 |
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
Oops, something went wrong.