Skip to content

Commit 2c2bc35

Browse files
PratyushSingh07therajanmaurya
authored andcommitted
refactor openMF#1476: migrated data package from java to kotlin
1 parent eed2c75 commit 2c2bc35

File tree

9 files changed

+265
-317
lines changed

9 files changed

+265
-317
lines changed

Diff for: mifospay/src/main/java/org/mifos/mobilewallet/mifospay/data/firebase/api/services/MifosPayMessagingService.java renamed to mifospay/src/main/java/org/mifos/mobilewallet/mifospay/data/firebase/api/services/MifosPayMessagingService.kt

+101-116
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,37 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package org.mifos.mobilewallet.mifospay.data.firebase.api.services;
18-
19-
import android.app.NotificationChannel;
20-
import android.app.NotificationManager;
21-
import android.app.PendingIntent;
22-
import android.content.Context;
23-
import android.content.Intent;
24-
import android.media.RingtoneManager;
25-
import android.net.Uri;
26-
import android.os.Build;
27-
import androidx.core.app.NotificationCompat;
28-
import android.util.Log;
29-
30-
import com.google.firebase.messaging.FirebaseMessagingService;
31-
import com.google.firebase.messaging.RemoteMessage;
32-
33-
import org.json.JSONException;
34-
import org.json.JSONObject;
35-
import org.mifos.mobilewallet.mifospay.R;
36-
import org.mifos.mobilewallet.mifospay.notification.ui.NotificationActivity;
37-
import org.mifos.mobilewallet.mifospay.utils.NotificationUtils;
16+
package org.mifos.mobilewallet.mifospay.data.firebase.api.services
17+
18+
import android.app.NotificationChannel
19+
import android.app.NotificationManager
20+
import android.app.PendingIntent
21+
import android.content.Context
22+
import android.content.Intent
23+
import android.media.RingtoneManager
24+
import android.os.Build
25+
import android.util.Log
26+
import androidx.core.app.NotificationCompat
27+
import com.google.firebase.messaging.FirebaseMessagingService
28+
import com.google.firebase.messaging.RemoteMessage
29+
import org.json.JSONException
30+
import org.json.JSONObject
31+
import org.mifos.mobilewallet.mifospay.R
32+
import org.mifos.mobilewallet.mifospay.notification.ui.NotificationActivity
33+
import org.mifos.mobilewallet.mifospay.utils.NotificationUtils
3834

3935
/**
4036
* Created by ankur on 20/June/2018
4137
*/
42-
43-
public class MifosPayMessagingService extends FirebaseMessagingService {
44-
45-
private static final String TAG = "MifosPayFCM";
46-
47-
38+
class MifosPayMessagingService : FirebaseMessagingService() {
4839
/**
4940
* Called if InstanceID token is updated. This may occur if the security of
5041
* the previous token had been compromised. Note that this is called when the InstanceID token
5142
* is initially generated so this is where you would retrieve the token.
5243
*/
53-
@Override
54-
public void onNewToken(String token) {
55-
super.onNewToken(token);
56-
Log.d(TAG, "Refreshed token: " + token);
44+
override fun onNewToken(token: String) {
45+
super.onNewToken(token)
46+
Log.d(TAG, "Refreshed token: $token")
5747
// If you want to send messages to this application instance or
5848
// manage this apps subscriptions on the server side, send the
5949
// Instance ID token to your app server.
@@ -65,8 +55,7 @@ public void onNewToken(String token) {
6555
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
6656
*/
6757
// [START receive_message]
68-
@Override
69-
public void onMessageReceived(RemoteMessage remoteMessage) {
58+
override fun onMessageReceived(remoteMessage: RemoteMessage) {
7059
// [START_EXCLUDE]
7160
// There are two types of messages data messages and notification messages. Data messages
7261
// are handled
@@ -84,18 +73,17 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
8473
// [END_EXCLUDE]
8574

8675
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
87-
Log.d(TAG, "From: " + remoteMessage.getFrom());
76+
Log.d(TAG, "From: " + remoteMessage.from)
8877

8978
// Check if message contains a data payload.
9079
// We will use data messages and hence our messages will be handled here
91-
if (remoteMessage.getData().size() > 0) {
92-
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
93-
80+
if (remoteMessage.data.size > 0) {
81+
Log.d(TAG, "Message data payload: " + remoteMessage.data)
9482
try {
95-
JSONObject json = new JSONObject(remoteMessage.getData().toString());
96-
handleDataMessage(json);
97-
} catch (Exception e) {
98-
Log.e(TAG, "Exception: " + e.getMessage());
83+
val json = JSONObject(remoteMessage.data.toString())
84+
handleDataMessage(json)
85+
} catch (e: Exception) {
86+
Log.e(TAG, "Exception: " + e.message)
9987
}
10088

10189
// if (/* Check if data needs to be processed by long running job */ true) {
@@ -119,18 +107,16 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
119107
// Also if you intend on generating your own notifications as a result of a received FCM
120108
// message, here is where that should be initiated. See sendNotification method below.
121109
}
122-
// [END receive_message]
123110

124-
125-
@Override
126-
public void onDeletedMessages() {
127-
super.onDeletedMessages();
111+
// [END receive_message]
112+
override fun onDeletedMessages() {
113+
super.onDeletedMessages()
128114
}
129115

130116
/**
131117
* Schedule a job using FirebaseJobDispatcher.
132118
*/
133-
private void scheduleJob() {
119+
private fun scheduleJob() {
134120
// [START dispatch_job]
135121
// FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
136122
// Job myJob = dispatcher.newJobBuilder()
@@ -144,84 +130,76 @@ private void scheduleJob() {
144130
/**
145131
* Handle time allotted to BroadcastReceivers.
146132
*/
147-
private void handleNow() {
148-
149-
Log.d(TAG, "Short lived task is done.");
133+
private fun handleNow() {
134+
Log.d(TAG, "Short lived task is done.")
150135
}
151136

152137
/**
153138
* Create and show a simple notification containing the received FCM message.
154139
*
155140
* @param messageBody FCM message body received.
156141
*/
157-
private void sendNotification(String title, String messageBody) {
158-
Intent intent = new Intent(this, NotificationActivity.class);
159-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
160-
PendingIntent pendingIntent;
161-
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */,
162-
intent,
163-
PendingIntent.FLAG_ONE_SHOT);
164-
165-
String channelId = getString(R.string.app_name);
166-
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
167-
NotificationCompat.Builder notificationBuilder =
168-
new NotificationCompat.Builder(this, channelId)
169-
.setSmallIcon(R.drawable.ic_bank)
170-
.setContentTitle(title)
171-
.setContentText(messageBody)
172-
.setAutoCancel(true)
173-
.setSound(defaultSoundUri)
174-
.setContentIntent(pendingIntent);
175-
176-
NotificationManager notificationManager =
177-
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
142+
private fun sendNotification(title: String, messageBody: String) {
143+
val intent = Intent(this, NotificationActivity::class.java)
144+
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
145+
val pendingIntent: PendingIntent
146+
pendingIntent = PendingIntent.getActivity(
147+
this, 0 /* Request code */,
148+
intent,
149+
PendingIntent.FLAG_ONE_SHOT
150+
)
151+
val channelId = getString(R.string.app_name)
152+
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
153+
val notificationBuilder = NotificationCompat.Builder(this, channelId)
154+
.setSmallIcon(R.drawable.ic_bank)
155+
.setContentTitle(title)
156+
.setContentText(messageBody)
157+
.setAutoCancel(true)
158+
.setSound(defaultSoundUri)
159+
.setContentIntent(pendingIntent)
160+
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
178161

179162
// Since android Oreo notification channel is needed.
180163
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
181-
NotificationChannel channel = new NotificationChannel(channelId,
182-
"Channel human readable title",
183-
NotificationManager.IMPORTANCE_DEFAULT);
184-
notificationManager.createNotificationChannel(channel);
164+
val channel = NotificationChannel(
165+
channelId,
166+
"Channel human readable title",
167+
NotificationManager.IMPORTANCE_DEFAULT
168+
)
169+
notificationManager.createNotificationChannel(channel)
185170
}
186-
187-
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
171+
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
188172
}
189173

190174
/**
191175
* Handles notification messages.
192176
*/
193-
private void handleNotification(String title, String message) {
194-
195-
}
177+
private fun handleNotification(title: String, message: String) {}
196178

197179
/**
198180
* Handles data messages.
199181
*/
200-
private void handleDataMessage(JSONObject json) {
201-
Log.e(TAG, "push json: " + json.toString());
202-
182+
private fun handleDataMessage(json: JSONObject) {
183+
Log.e(TAG, "push json: $json")
203184
try {
204-
JSONObject data = json.getJSONObject("data");
205-
206-
String title = data.getString("title");
207-
String message = data.getString("message");
208-
String imageUrl = data.getString("image");
209-
String type = data.getString("type");
210-
String timestamp = data.getString("timestamp");
211-
JSONObject payload = data.getJSONObject("payload");
212-
213-
Log.e(TAG, "title: " + title);
214-
Log.e(TAG, "message: " + message);
215-
Log.e(TAG, "type: " + type);
185+
val data = json.getJSONObject("data")
186+
val title = data.getString("title")
187+
val message = data.getString("message")
188+
val imageUrl = data.getString("image")
189+
val type = data.getString("type")
190+
val timestamp = data.getString("timestamp")
191+
val payload = data.getJSONObject("payload")
192+
Log.e(TAG, "title: $title")
193+
Log.e(TAG, "message: $message")
194+
Log.e(TAG, "type: $type")
216195
// payload can be used when one needs to show specific notification with some data
217196
// and process it
218197
if (payload != null) {
219-
Log.e(TAG, "payload: " + payload.toString());
198+
Log.e(TAG, "payload: $payload")
220199
}
221-
Log.e(TAG, "imageUrl: " + imageUrl);
222-
Log.e(TAG, "timestamp: " + timestamp);
223-
224-
sendNotification(title, message);
200+
Log.e(TAG, "imageUrl: $imageUrl")
201+
Log.e(TAG, "timestamp: $timestamp")
202+
sendNotification(title, message)
225203

226204
// Below code can be used when you want to show notification with Image.
227205

@@ -237,31 +215,38 @@ private void handleDataMessage(JSONObject json) {
237215
// showNotificationMessageWithBigImage(getApplicationContext(), title, message,
238216
// timestamp, resultIntent, imageUrl);
239217
// }
240-
241-
} catch (JSONException e) {
242-
Log.e(TAG, "Json Exception: " + e.getMessage());
243-
} catch (Exception e) {
244-
Log.e(TAG, "Exception: " + e.getMessage());
218+
} catch (e: JSONException) {
219+
Log.e(TAG, "Json Exception: " + e.message)
220+
} catch (e: Exception) {
221+
Log.e(TAG, "Exception: " + e.message)
245222
}
246223
}
247224

248225
/**
249226
* Showing notification with text only
250227
*/
251-
private void showNotificationMessage(Context context, String title, String message,
252-
String timeStamp, Intent intent) {
253-
NotificationUtils notificationUtils = new NotificationUtils(context);
254-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
255-
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
228+
private fun showNotificationMessage(
229+
context: Context, title: String, message: String,
230+
timeStamp: String, intent: Intent
231+
) {
232+
val notificationUtils = NotificationUtils(context)
233+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
234+
notificationUtils.showNotificationMessage(title, message, timeStamp, intent)
256235
}
257236

258237
/**
259238
* Showing notification with text and image
260239
*/
261-
private void showNotificationMessageWithBigImage(Context context, String title, String message,
262-
String timeStamp, Intent intent, String imageUrl) {
263-
NotificationUtils notificationUtils = new NotificationUtils(context);
264-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
265-
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
240+
private fun showNotificationMessageWithBigImage(
241+
context: Context, title: String, message: String,
242+
timeStamp: String, intent: Intent, imageUrl: String
243+
) {
244+
val notificationUtils = NotificationUtils(context)
245+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
246+
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl)
247+
}
248+
249+
companion object {
250+
private const val TAG = "MifosPayFCM"
266251
}
267252
}

Diff for: mifospay/src/main/java/org/mifos/mobilewallet/mifospay/data/local/LocalRepository.java

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.mifos.mobilewallet.mifospay.data.local
2+
3+
import org.mifos.mobilewallet.core.domain.model.client.Client
4+
import javax.inject.Inject
5+
import javax.inject.Singleton
6+
7+
/**
8+
* Created by naman on 17/6/17.
9+
*/
10+
@Singleton
11+
class LocalRepository @Inject constructor(val preferencesHelper: PreferencesHelper) {
12+
13+
val clientDetails: Client
14+
get() {
15+
val details = Client()
16+
details.name = preferencesHelper.fullName
17+
details.clientId = preferencesHelper.clientId
18+
details.externalId = preferencesHelper.clientVpa
19+
return details
20+
}
21+
22+
fun saveClientData(client: Client) {
23+
preferencesHelper.saveFullName(client.name)
24+
preferencesHelper.clientId = client.clientId
25+
preferencesHelper.clientVpa = client.externalId
26+
}
27+
}

0 commit comments

Comments
 (0)