• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

help me fix this app

erisodjey

Lurker
package com.example.mobilemoneytransfer

import java.util.*

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.coroutines.*
import retrofit2.*
import retrofit2.converter.gson.*

@Serializable
data class TransactionRecord(
val senderPhoneNumber: String,
val recipientPhoneNumber: String,
val amount: Int,
val timestamp: Long
)

interface MobileMoneyApi {
@POST("send-money")
suspend fun sendMoney(
phoneNumber: String,
pin: String,
recipientPhoneNumber: String,
amount: Int
): Response<Unit>

@POST("pay-bill")
suspend fun makePayment(
billNumber: String,
amount: Double,
phoneNumber: String
): Response<Unit>

@GET("transaction-history")
suspend fun getTransactionHistory(
phoneNumber: String,
pin: String,
fromDate: Date?,
toDate: Date?
): Response<List<TransactionRecord>>
}

class SendMoneyService {

private val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY })
.build()

private val json = Json {
prettyPrint = true
}

private val api = Retrofit.Builder()
.baseUrl("https://api.mobilemoney.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MobileMoneyApi::class.java)

fun sendMoney(
senderPhoneNumber: String,
senderPin: String,
recipientPhoneNumber: String,
amount: Int
): suspend (Result<Unit>) {
return withContext(Dispatchers.IO) {
val response = try {
api.sendMoney(senderPhoneNumber, senderPin, recipientPhoneNumber, amount)
} catch (e: Exception) {
return@withContext Result.failure(e)
}

if (response.isSuccessful) {
return@withContext Result.success(Unit)
} else {
return@withContext Result.failure(RuntimeException("Error sending money"))
}
}
}

fun getTransactionHistory(
phoneNumber: String,
pin: String,
fromDate: Date?,
toDate: Date?
): suspend (Result<List<TransactionRecord>>) {
return withContext(Dispatchers.IO) {
val response = try {
api.getTransactionHistory(phoneNumber, pin, fromDate, toDate)
} catch (e: Exception) {
return@withContext Result.failure(e)
}

if (response.isSuccessful) {
val transactionRecords = response.body() ?: emptyList()
// Cache the transaction records.
TransactionRecordCache.putTransactionRecords(transactionRecords)
return@withContext Result.success(transactionRecords)
} else {
return@withContext Result.failure(RuntimeException("Error getting transaction history"))
}
}
}
}

class MakePaymentService {

private val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY })
.build()

private val json = Json {
prettyPrint = true
}

private val api = Retrofit.Builder()
.baseUrl("https://api.mobilemoney.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MobileMoneyApi::class.java)

fun makePayment(
billNumber: String,
amount: Double,
phoneNumber: String
): suspend (Result<Unit>) {
return withContext(Dispatchers.IO) {
val response = try {
api.makePayment(billNumber, amount, phoneNumber)
} catch (e: Exception) {
return@withContext Result.failure(e)
}

if (response.isSuccessful) {
return@withContext Result.success(
 
Back
Top Bottom