Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class BpLocationDataSource(private val locationTracker: LocationTracker) : IBpLo
locationTracker.startTracking()
return locationTracker.getLocationsFlow().map {
Location(
lat = it.latitude,
lng = it.longitude,
addressName = ""
latitude = it.latitude,
longitude = it.longitude,
)
}
} catch (deniedAlways: DeniedAlwaysException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ class LocalConfigurationGateway(private val realm: Realm) : ILocalConfigurationG
override suspend fun clearTokens() {
realm.write { delete(query<UserConfigurationCollection>()) }
}
override suspend fun saveTaxiId(taxiId: String) {
realm.write {
query<UserConfigurationCollection>("$ID == $CONFIGURATION_ID").first()
.find()?.taxiId = taxiId
}
}

override suspend fun getTaxiId(): String {
return realm.query<UserConfigurationCollection>("$ID == $CONFIGURATION_ID").first()
.find()?.taxiId ?: ""
}

override suspend fun saveKeepMeLoggedInFlag(isChecked: Boolean) {
realm.write {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class UserConfigurationCollection : RealmObject {
var refreshToken: String = ""
var username: String = ""
var isKeepMeLoggedInMeChecked: Boolean = false
var taxiId: String = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import domain.InvalidPasswordException
import domain.InvalidUserNameException
import domain.PermissionDenied
import domain.entity.Session
import domain.entity.Taxi
import domain.entity.TaxiRequestPermission
import domain.gateway.remote.IIdentityRemoteGateway

class IdentityFakeGateway : IIdentityRemoteGateway {
Expand All @@ -27,11 +29,13 @@ class IdentityFakeGateway : IIdentityRemoteGateway {
return Pair("wertqyhgt", "qazswxza")
}

override suspend fun createRequestPermission(
driverFullName: String,
driverEmail: String,
description: String
) {
override suspend fun createRequestPermission(taxiRequestPermission: TaxiRequestPermission): Boolean {
TODO("Not yet implemented")
}

override suspend fun getAllVehicles(): List<Taxi> {
TODO("Not yet implemented")
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import kotlinx.coroutines.flow.flow


class LocationFakeGateway : ILocationGateway {
override suspend fun startTracking() {
TODO("Not yet implemented")
}

override suspend fun trackCurrentLocation() = flow {
var lattiude = 30.044420
while (true) {
lattiude += 0.00004
emit(
Location(
lat = lattiude,
lng = 31.235712,
addressName = "Cairo, Egypt",
latitude = lattiude,
longitude = 31.235712,
)
)
delay(2000)
}
}

override suspend fun stopTracking() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ package data.remote.fakegateway

import data.remote.mapper.toEntity
import data.remote.model.LocationDto
import data.remote.model.OrderDto
import domain.entity.Order
import data.remote.model.TaxiTripDto
import domain.entity.Trip
import domain.gateway.IOrderGateway
import kotlinx.coroutines.delay

class OrderFakeGateway : IOrderGateway {
override suspend fun findingNewOrder(): Order {
override suspend fun findingNewOrder(): Trip {
delay(4000)
return OrderDto(
return TaxiTripDto(
id = "djsahdjadhjadjas45dsadas",
passengerId = "sjdadjsadsa-dsa4d8sa4dsa",
passengerName = "Cristiano Ronaldo",
pickUpAddress = LocationDto(
lat = 40.6790229,
lng = -73.8740306,
addressName = "45, Faisal St., Riyadh, KSA",
clientName = "Cristiano Ronaldo",
startPoint = LocationDto(
latitude = 40.6790229,
longitude = -73.8740306,
),
dropOffAddress = LocationDto(
lat = 30.8859508,
lng = 31.4453136,
addressName = "Nirmala,girsls HSS",
destination = LocationDto(
latitude = 30.8859508,
longitude = 31.4453136,
),
startPointAddress = "ooveofe",
destinationAddress = "h9viife",
price = 100.0,
tripStatus = 1,
).toEntity()
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package data.remote.gateway.local

import data.service.ILocationService
import dev.icerock.moko.geo.LocationTracker
import dev.icerock.moko.permissions.DeniedAlwaysException
import domain.LocationPermissionDeniedException
import domain.entity.Location
import domain.gateway.ILocationGateway
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class LocationRemoteGateway(
private val locationService: ILocationService,
private val locationTracker: LocationTracker
) : ILocationGateway {

override suspend fun startTracking() {
try {
locationTracker.startTracking()
if (!locationService.isGPSEnabled()) {
locationService.openLocationSettings()
}
} catch (e: DeniedAlwaysException) {
throw LocationPermissionDeniedException(e.message)
}
}

override suspend fun trackCurrentLocation(): Flow<Location> {
return locationTracker.getLocationsFlow().map {
Location(it.latitude, it.longitude)
}
}

override suspend fun stopTracking() {
locationTracker.stopTracking()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package data.remote.gateway
package data.remote.gateway.remote

import data.local.gateway.LocalConfigurationGateway
import io.ktor.client.HttpClient
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package data.remote.gateway.remote

import data.remote.model.BaseResponse
import domain.AlreadyExistException
import domain.InvalidPasswordException
import domain.InvalidUserNameException
import domain.NoInternetException
import domain.SocketException
import domain.UnAuthorizedException
import domain.UnknownErrorException
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.websocket.receiveDeserialized
import io.ktor.client.plugins.websocket.sendSerialized
import io.ktor.client.plugins.websocket.wss
import io.ktor.client.statement.HttpResponse
import io.ktor.util.network.UnresolvedAddressException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn


abstract class BaseRemoteGateway(val client: HttpClient) {

protected suspend inline fun <reified T> tryToExecute(
method: HttpClient.() -> HttpResponse,
): T {
try {
return client.method().body()
} catch (e: ClientRequestException) {
val errorMessages = e.response.body<BaseResponse<String>>().status?.errorMessages
errorMessages?.let { throwMatchingException(it) }
throw UnknownErrorException(e.message)
} catch (e: UnresolvedAddressException) {
throw NoInternetException()
} catch (e: Exception) {
println("exception: $e")
throw UnknownErrorException(e.message.toString())
}
}
suspend inline fun <reified T> HttpClient.tryToExecuteWebSocket(path: String): Flow<T> {
return flow {
wss(path = path) {
while (true) {
try {
emit(receiveDeserialized<T>())
} catch (e: Exception) {
throw SocketException(e.message.orEmpty())
}
}
}
}.flowOn(Dispatchers.IO)
}
suspend inline fun <reified T> HttpClient.tryToSendWebSocketData(
data: T,
path: String,
) {
wss(path) {
try {
sendSerialized(data)
} catch (e: Exception) {
throw SocketException(e.message.orEmpty())
}
}
}

fun throwMatchingException(errorMessages: Map<String, String>) {
when {
errorMessages.containsErrors(WRONG_PASSWORD) ->
throw InvalidPasswordException(errorMessages.getOrEmpty(WRONG_PASSWORD))

errorMessages.containsErrors(USER_NOT_EXIST) ->
throw InvalidUserNameException(errorMessages.getOrEmpty(USER_NOT_EXIST))
errorMessages.containsErrors(ALREADY_EXIST) ->
throw AlreadyExistException(errorMessages.getOrEmpty(ALREADY_EXIST))

errorMessages.containsErrors(INVALID_PERMISSION) ->
throw UnAuthorizedException(errorMessages.getOrEmpty(INVALID_PERMISSION))
}
}


private fun Map<String, String>.containsErrors(vararg errorCodes: String): Boolean =
keys.containsAll(errorCodes.toList())

private fun Map<String, String>.getOrEmpty(key: String): String = get(key) ?: ""

companion object {
const val WRONG_PASSWORD = "1013"
const val USER_NOT_EXIST = "1043"
const val INVALID_PERMISSION = "1014"
const val ALREADY_EXIST = "3010"
}
}
Loading