Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rocket Chat Integration #2295

Draft
wants to merge 4 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 27 additions & 43 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name="org.mifos.mobile.MifosSelfServiceApp"
android:name=".MifosSelfServiceApp"
android:allowBackup="true"
android:icon="@mipmap/mifos_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".rocketchat.RocketChatActivity"
android:exported="false" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<activity
android:name="org.mifos.mobile.ui.activities.LoginActivity"
android:name=".ui.activities.LoginActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.SplashActivity"
android:name=".ui.activities.SplashActivity"
android:exported="true"
android:theme="@style/SplashTheme">
<intent-filter>
Expand All @@ -42,92 +43,76 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="org.mifos.mobile.ui.activities.HomeActivity"
android:name=".ui.activities.HomeActivity"
android:configChanges="screenLayout|screenSize|orientation"
android:label="@string/home"
android:windowSoftInputMode="adjustResize" />
<activity
android:name="org.mifos.mobile.ui.activities.SavingsAccountContainerActivity"
android:name=".ui.activities.SavingsAccountContainerActivity"
android:configChanges="orientation|screenSize"
android:label="@string/saving_account_details"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.PassCodeActivity"
android:name=".ui.activities.PassCodeActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.LoanAccountContainerActivity"
android:name=".ui.activities.LoanAccountContainerActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.UserProfileActivity"
android:name=".ui.activities.UserProfileActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.RegistrationActivity"
android:name=".ui.activities.RegistrationActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.LoanApplicationActivity"
android:name=".ui.activities.LoanApplicationActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.AccountOverviewActivity"
android:name=".ui.activities.AccountOverviewActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="com.google.android.gms.oss.licenses.OssLicensesActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.NotificationActivity"
android:name=".ui.activities.NotificationActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.AddBeneficiaryActivity"
android:name=".ui.activities.AddBeneficiaryActivity"
android:screenOrientation="sensor"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.EditUserDetailActivity"
android:name=".ui.activities.EditUserDetailActivity"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.SavingsAccountApplicationActivity"
android:name=".ui.activities.SavingsAccountApplicationActivity"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.AboutUsActivity"
android:name=".ui.activities.AboutUsActivity"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.HelpActivity"
android:name=".ui.activities.HelpActivity"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="org.mifos.mobile.ui.activities.PrivacyPolicyActivity"
android:name=".ui.activities.PrivacyPolicyActivity"
android:windowSoftInputMode="adjustResize" />

<receiver
android:name="org.mifos.mobile.ui.widgets.ChargeAppWidget"
android:name=".ui.widgets.ChargeAppWidget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Expand All @@ -139,7 +124,7 @@
</receiver>

<service
android:name="org.mifos.mobile.ui.widgets.ChargeWidgetService"
android:name=".ui.widgets.ChargeWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />

<provider
Expand All @@ -159,12 +144,11 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<service
android:name="org.mifos.mobile.utils.fcm.RegistrationIntentService"
android:exported="false"></service>
android:name=".utils.fcm.RegistrationIntentService"
android:exported="false" />

<activity android:name="org.mifos.mobile.ui.activities.SettingsActivity" />
<activity android:name=".ui.activities.SettingsActivity" />
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mifos.mobile.rocketchat

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import org.mifos.mobile.databinding.ActivityRocketChatBinding

class RocketChatActivity : AppCompatActivity() {
private lateinit var binding : ActivityRocketChatBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRocketChatBinding.inflate(layoutInflater)
setContentView(binding.root)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.mifos.mobile.rocketchat

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.mifos.mobile.databinding.FragmentRocketChatBinding
import org.mifos.mobile.rocketchat.adapter.RocketChatAdapter
import org.mifos.mobile.rocketchat.model.SupportChatMessage

class RocketChatFragment : Fragment() {
private var _binding: FragmentRocketChatBinding? = null
private val binding get() = _binding!!
private lateinit var viewModel: RocketChatViewModel
private var webSocket: WebSocket? = null
private lateinit var webSocketListener: WebSocketListener
private lateinit var adapter: RocketChatAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentRocketChatBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[RocketChatViewModel::class.java]
webSocketListener = RocketChatService(viewModel)
adapter = RocketChatAdapter()
binding.rvSupportChat.adapter = adapter
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(binding) {
viewModel.socketStatus.observe(viewLifecycleOwner) {
statusTV.text = if (it) "Connected" else "Disconnected"
}

viewModel.messages.observe(viewLifecycleOwner) {
adapter.addNewMessage(it)
}

connectButton.setOnClickListener {
webSocket = viewModel.createWebSocketConnection(webSocketListener)
}

disconnectButton.setOnClickListener {
webSocket?.close(1000, "Canceled manually.")
}

sendButton.setOnClickListener {
viewModel.sendCustomerMessage(webSocket, messageET.text.toString())
viewModel.addMessage(
SupportChatMessage(
messageET.text.toString().trim(),
SupportChatMessage.MessageType.USER
)
)
messageET.text = null
}
}

}

}
92 changes: 92 additions & 0 deletions app/src/main/java/org/mifos/mobile/rocketchat/RocketChatService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.mifos.mobile.rocketchat

import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.json.JSONObject
import org.mifos.mobile.rocketchat.model.SupportChatMessage
import javax.inject.Inject

class RocketChatService @Inject constructor(
private val viewModel: RocketChatViewModel
) : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
super.onOpen(webSocket, response)
viewModel.setStatus(true)
val connectMessage = """
{
"msg": "connect",
"version": "1",
"support": ["1"]
}
""".trimIndent()
webSocket.send(connectMessage)

val loginRequest = """
{
"msg": "method",
"method": "login",
"id": "42",
"params": [
{ "resume": "EDFOiOPiwS5k-ptDA9-bDQ7qM2DVXiDDh-5YrzrLfxA" }
]
}
""".trimIndent()
webSocket.send(loginRequest)

val subscribeRequest = """
{
"msg": "sub",
"id": "sub1",
"name": "stream-room-messages",
"params": ["GENERAL", false]
}
""".trimIndent()
webSocket.send(subscribeRequest)

}

override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)

if (text.contains("\"msg\":\"changed\"") && text.contains("\"collection\":\"stream-room-messages\"") && !text.contains(
"\"username\":\"CustomerTestMifosRCWorkspace\""
)
) {
val jsonObject = JSONObject(text)
val fieldsObject = jsonObject.getJSONObject("fields")
val argsArray = fieldsObject.getJSONArray("args")
val messageObject = argsArray.getJSONObject(0)
val message = messageObject.getString("msg")

viewModel.addMessage(
SupportChatMessage(
message,
SupportChatMessage.MessageType.SUPPORT
)
)
}

if (text.contains("\"msg\":\"ping\"")) {
val pongMessage = """
{
"msg": "pong"
}
""".trimIndent()
webSocket.send(pongMessage)
}
}

override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
super.onClosing(webSocket, code, reason)
}

override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
super.onClosed(webSocket, code, reason)
viewModel.setStatus(false)
}

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
super.onFailure(webSocket, t, response)
}
}
Loading