Skip to content

Commit

Permalink
[ref] API models and UI models
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamsafarovrs committed Jun 25, 2024
1 parent b88fb6d commit ef9380d
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 78 deletions.
22 changes: 11 additions & 11 deletions app/src/main/java/tj/rsdevteam/inmuslim/data/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package tj.rsdevteam.inmuslim.data.api
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import tj.rsdevteam.inmuslim.data.models.network.GetRegionsResponse
import tj.rsdevteam.inmuslim.data.models.network.GetTimingBody
import tj.rsdevteam.inmuslim.data.models.network.GetTimingResponse
import tj.rsdevteam.inmuslim.data.models.network.RegisterUserBody
import tj.rsdevteam.inmuslim.data.models.network.RegisterUserResponse
import tj.rsdevteam.inmuslim.data.models.network.UpdateMessagingIdBody
import tj.rsdevteam.inmuslim.data.models.network.UpdateMessagingIdResponse
import tj.rsdevteam.inmuslim.data.models.api.GetRegionsDTO
import tj.rsdevteam.inmuslim.data.models.api.GetTimingBodyDTO
import tj.rsdevteam.inmuslim.data.models.api.GetTimingDTO
import tj.rsdevteam.inmuslim.data.models.api.RegisterUserBodyDTO
import tj.rsdevteam.inmuslim.data.models.api.RegisterUserDTO
import tj.rsdevteam.inmuslim.data.models.api.UpdateMessagingIdBodyDTO
import tj.rsdevteam.inmuslim.data.models.api.UpdateMessagingIdDTO

/**
* Created by Rustam Safarov on 8/13/23.
Expand All @@ -19,14 +19,14 @@ import tj.rsdevteam.inmuslim.data.models.network.UpdateMessagingIdResponse
interface Api {

@GET("GetRegions")
suspend fun getRegions(): Result<GetRegionsResponse>
suspend fun getRegions(): Result<GetRegionsDTO>

@POST("GetTiming")
suspend fun getTiming(@Body body: GetTimingBody): Result<GetTimingResponse>
suspend fun getTiming(@Body body: GetTimingBodyDTO): Result<GetTimingDTO>

@POST("RegisterUser")
suspend fun registerUser(@Body body: RegisterUserBody): Result<RegisterUserResponse>
suspend fun registerUser(@Body body: RegisterUserBodyDTO): Result<RegisterUserDTO>

@POST("UpdateMessagingId")
suspend fun updateMessagingId(@Body body: UpdateMessagingIdBody): Result<UpdateMessagingIdResponse>
suspend fun updateMessagingId(@Body body: UpdateMessagingIdBodyDTO): Result<UpdateMessagingIdDTO>
}
10 changes: 10 additions & 0 deletions app/src/main/java/tj/rsdevteam/inmuslim/data/models/Message.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tj.rsdevteam.inmuslim.data.models

/**
* Created by Rustam Safarov on 6/25/24.
* github.com/rustamsafarovrs
*/

data class Message(
val message: String
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tj.rsdevteam.inmuslim.data.models

import androidx.compose.runtime.mutableStateOf
import com.squareup.moshi.JsonClass

/**
* Created by Rustam Safarov on 14/08/23.
* Created by Rustam Safarov on 6/25/24.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class Region(
val id: Long,
val name: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models

/**
* Created by Rustam Safarov on 8/13/23.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package tj.rsdevteam.inmuslim.data.models

import com.squareup.moshi.JsonClass

/**
* Created by Rustam Safarov on 8/13/23.
* Created by Rustam Safarov on 6/25/24.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class Timing(
val fajr: String,
val sunrise: String,
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/tj/rsdevteam/inmuslim/data/models/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tj.rsdevteam.inmuslim.data.models

/**
* Created by Rustam Safarov on 6/25/24.
* github.com/rustamsafarovrs
*/

data class User(
val id: Long
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass
import tj.rsdevteam.inmuslim.data.models.Region

/**
* Created by Rustam Safarov on 14/08/23.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class GetRegionsResponse(
data class GetRegionsDTO(
val result: Int,
val msg: String,
val regions: List<Region>
val regions: List<RegionDTO>
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
Expand All @@ -9,7 +9,7 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class GetTimingBody(
data class GetTimingBodyDTO(
@Json(name = "region_id")
val regionId: Long
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import tj.rsdevteam.inmuslim.data.models.Timing

/**
* Created by Rustam Safarov on 8/13/23.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class GetTimingResponse(
data class GetTimingDTO(
val result: Int,
val msg: String,
val region: String,
@Json(name = "begin_date")
val beginDate: String,
val timing: Timing
val timing: TimingDTO
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

Expand All @@ -8,7 +8,7 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class MessageResponse(
data class MessageDTO(
val result: Int,
val msg: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

/**
* Created by Rustam Safarov on 14/08/23.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class RegionDTO(
val id: Long,
val name: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

Expand All @@ -8,6 +8,6 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class RegisterUserBody(
data class RegisterUserBodyDTO(
val name: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

Expand All @@ -8,7 +8,7 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class RegisterUserResponse(
data class RegisterUserDTO(
val result: Int,
val msg: String,
val id: Long
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

/**
* Created by Rustam Safarov on 8/13/23.
* github.com/rustamsafarovrs
*/

@JsonClass(generateAdapter = true)
data class TimingDTO(
val fajr: String,
val sunrise: String,
val zuhr: String,
val asr: String,
val sunset: String,
val maghrib: String,
val isha: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

Expand All @@ -8,7 +8,7 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class UpdateMessagingIdBody(
data class UpdateMessagingIdBodyDTO(
val id: Long,
val msgid: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tj.rsdevteam.inmuslim.data.models.network
package tj.rsdevteam.inmuslim.data.models.api

import com.squareup.moshi.JsonClass

Expand All @@ -8,7 +8,7 @@ import com.squareup.moshi.JsonClass
*/

@JsonClass(generateAdapter = true)
data class UpdateMessagingIdResponse(
data class UpdateMessagingIdDTO(
val result: Int,
val msg: String
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import retrofit2.HttpException
import tj.rsdevteam.inmuslim.data.exceptions.ApiException
import tj.rsdevteam.inmuslim.data.exceptions.ConnectionTimeoutException
import tj.rsdevteam.inmuslim.data.exceptions.UnknownException
import tj.rsdevteam.inmuslim.data.models.network.MessageResponse
import tj.rsdevteam.inmuslim.data.models.network.Resource
import tj.rsdevteam.inmuslim.data.models.Resource
import tj.rsdevteam.inmuslim.data.models.api.MessageDTO
import java.io.IOException
import java.net.UnknownHostException

Expand All @@ -17,8 +17,8 @@ import java.net.UnknownHostException
class ErrorHandler {

fun <T : Any> getError(result: Result<*>): Resource<T> {
return if (result.getOrNull() is MessageResponse) {
Resource.Error(error = ApiException((result.getOrThrow() as MessageResponse).msg))
return if (result.getOrNull() is MessageDTO) {
Resource.Error(error = ApiException((result.getOrThrow() as MessageDTO).msg))
} else if (result.exceptionOrNull() is UnknownHostException) {
Resource.Error(error = ConnectionTimeoutException())
} else if (result.exceptionOrNull() is HttpException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tj.rsdevteam.inmuslim.data.repositories

import tj.rsdevteam.inmuslim.data.models.Message
import tj.rsdevteam.inmuslim.data.models.Region
import tj.rsdevteam.inmuslim.data.models.Timing
import tj.rsdevteam.inmuslim.data.models.User
import tj.rsdevteam.inmuslim.data.models.api.MessageDTO
import tj.rsdevteam.inmuslim.data.models.api.RegionDTO
import tj.rsdevteam.inmuslim.data.models.api.RegisterUserDTO
import tj.rsdevteam.inmuslim.data.models.api.TimingDTO
import tj.rsdevteam.inmuslim.data.models.api.UpdateMessagingIdDTO

/**
* Created by Rustam Safarov on 6/25/24.
* github.com/rustamsafarovrs
*/

fun RegionDTO.toRegion(): Region {
return Region(
id = this.id,
name = this.name
)
}

fun TimingDTO.toTiming(): Timing {
return Timing(
fajr = this.fajr,
sunrise = this.sunrise,
zuhr = this.zuhr,
asr = this.asr,
sunset = this.sunset,
maghrib = this.maghrib,
isha = this.isha
)
}

fun MessageDTO.toMessage(): Message {
return Message(
message = this.msg
)
}

fun RegisterUserDTO.toUser(): User {
return User(
id = this.id
)
}

fun UpdateMessagingIdDTO.toMessage(): Message {
return Message(
message = this.msg
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.flow
import tj.rsdevteam.inmuslim.data.api.Api
import tj.rsdevteam.inmuslim.data.constants.Constants
import tj.rsdevteam.inmuslim.data.models.Region
import tj.rsdevteam.inmuslim.data.models.network.Resource
import tj.rsdevteam.inmuslim.data.models.Resource
import tj.rsdevteam.inmuslim.data.preferences.Preferences
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -34,7 +34,7 @@ class RegionRepository
val result = api.getRegions()
delay(Constants.MIDDLE_DELAY)
if (result.isSuccess && result.getOrNull()?.result == 0) {
emit(Resource.Success(result.getOrThrow().regions))
emit(Resource.Success(result.getOrThrow().regions.map { it.toRegion() }))
} else {
emit(errorHandler.getError(result))
}
Expand Down
Loading

0 comments on commit ef9380d

Please sign in to comment.