DataSource wrapping
Some refactoring
This commit is contained in:
+37
-1
@@ -1,10 +1,20 @@
|
||||
package com.meloda.fast.api.network
|
||||
package com.meloda.fast.api
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.meloda.fast.api.datasource.AuthDataSource
|
||||
import com.meloda.fast.api.datasource.ConversationsDataSource
|
||||
import com.meloda.fast.api.datasource.MessagesDataSource
|
||||
import com.meloda.fast.api.datasource.UsersDataSource
|
||||
import com.meloda.fast.api.network.AuthInterceptor
|
||||
import com.meloda.fast.api.network.ResultCallFactory
|
||||
import com.meloda.fast.api.network.repo.AuthRepo
|
||||
import com.meloda.fast.api.network.repo.ConversationsRepo
|
||||
import com.meloda.fast.api.network.repo.MessagesRepo
|
||||
import com.meloda.fast.api.network.repo.UsersRepo
|
||||
import com.meloda.fast.database.dao.ConversationsDao
|
||||
import com.meloda.fast.database.dao.MessagesDao
|
||||
import com.meloda.fast.database.dao.UsersDao
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -67,4 +77,30 @@ class VKModules {
|
||||
fun provideUsersRepo(retrofit: Retrofit): UsersRepo =
|
||||
retrofit.create(UsersRepo::class.java)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAuthDataSource(
|
||||
repo: AuthRepo
|
||||
): AuthDataSource = AuthDataSource(repo)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUsersDataSource(
|
||||
repo: UsersRepo,
|
||||
dao: UsersDao
|
||||
): UsersDataSource = UsersDataSource(repo, dao)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideConversationsDataSource(
|
||||
repo: ConversationsRepo,
|
||||
dao: ConversationsDao
|
||||
): ConversationsDataSource = ConversationsDataSource(repo, dao)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideMessagesDataSource(
|
||||
repo: MessagesRepo,
|
||||
dao: MessagesDao
|
||||
): MessagesDataSource = MessagesDataSource(repo, dao)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.meloda.fast.api
|
||||
|
||||
import androidx.annotation.WorkerThread
|
||||
import com.meloda.fast.api.model.*
|
||||
import com.meloda.fast.api.model.old.*
|
||||
import com.meloda.fast.api.network.VKErrors
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.meloda.fast.api.datasource
|
||||
|
||||
import com.meloda.fast.api.network.repo.AuthRepo
|
||||
import com.meloda.fast.api.network.request.RequestAuthDirect
|
||||
import javax.inject.Inject
|
||||
|
||||
class AuthDataSource @Inject constructor(
|
||||
private val repo: AuthRepo
|
||||
) {
|
||||
|
||||
suspend fun auth(params: RequestAuthDirect) = repo.auth(params.map)
|
||||
|
||||
suspend fun sendSms(validationSid: String) = repo.sendSms(validationSid)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.meloda.fast.api.datasource
|
||||
|
||||
import com.meloda.fast.api.model.VkConversation
|
||||
import com.meloda.fast.api.network.repo.ConversationsRepo
|
||||
import com.meloda.fast.api.network.request.ConversationsGetRequest
|
||||
import com.meloda.fast.database.dao.ConversationsDao
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationsDataSource @Inject constructor(
|
||||
private val repo: ConversationsRepo,
|
||||
private val dao: ConversationsDao
|
||||
) {
|
||||
|
||||
suspend fun getAllChats(params: ConversationsGetRequest) = repo.getAllChats(params)
|
||||
|
||||
suspend fun storeConversations(conversations: List<VkConversation>) = dao.insert(conversations)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.meloda.fast.api.datasource
|
||||
|
||||
import com.meloda.fast.api.network.repo.MessagesRepo
|
||||
import com.meloda.fast.database.dao.MessagesDao
|
||||
import javax.inject.Inject
|
||||
|
||||
class MessagesDataSource @Inject constructor(
|
||||
private val repo: MessagesRepo,
|
||||
private val dao: MessagesDao
|
||||
) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.meloda.fast.api.datasource
|
||||
|
||||
import com.meloda.fast.api.model.VkUser
|
||||
import com.meloda.fast.api.network.repo.UsersRepo
|
||||
import com.meloda.fast.api.network.request.UsersGetRequest
|
||||
import com.meloda.fast.database.dao.UsersDao
|
||||
import javax.inject.Inject
|
||||
|
||||
class UsersDataSource @Inject constructor(
|
||||
private val repo: UsersRepo,
|
||||
private val dao: UsersDao
|
||||
) {
|
||||
|
||||
suspend fun getById(params: UsersGetRequest) = repo.getById(params.map)
|
||||
|
||||
suspend fun storeUsers(users: List<VkUser>) = dao.insert(users)
|
||||
|
||||
}
|
||||
@@ -1,15 +1,9 @@
|
||||
package com.meloda.fast.api.loader
|
||||
|
||||
import com.meloda.fast.api.model.VkUser
|
||||
import com.meloda.fast.api.network.repo.UsersRepo
|
||||
import com.meloda.fast.api.network.request.UsersGetRequest
|
||||
import javax.inject.Inject
|
||||
|
||||
class UsersLoader : Loader<VkUser>() {
|
||||
|
||||
@Inject
|
||||
lateinit var repo: UsersRepo
|
||||
|
||||
suspend fun load(
|
||||
usersIds: List<Int>,
|
||||
fields: String = ""
|
||||
@@ -24,12 +18,12 @@ class UsersLoader : Loader<VkUser>() {
|
||||
val usersIds: String = params["usersIds"] as String
|
||||
val fields: String = params["fields"] as String
|
||||
|
||||
val users = repo.getById(
|
||||
UsersGetRequest(
|
||||
usersIds = usersIds.split(",").map { it.toInt() },
|
||||
fields = fields
|
||||
)
|
||||
)
|
||||
// val users = repo.getById(
|
||||
// UsersGetRequest(
|
||||
// usersIds = usersIds.split(",").map { it.toInt() },
|
||||
// fields = fields
|
||||
// )
|
||||
// )
|
||||
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
package com.meloda.fast.api.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Ignore
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "conversations")
|
||||
data class VkConversation(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
val id: Int,
|
||||
val title: String?,
|
||||
val lastMessage: VkMessage
|
||||
)
|
||||
) {
|
||||
@Ignore
|
||||
var lastMessage: VkMessage? = null
|
||||
}
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
package com.meloda.fast.api.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "messages")
|
||||
data class VkMessage(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
val id: Int,
|
||||
val text: String?,
|
||||
val isOut: Boolean,
|
||||
val peerId: Int,
|
||||
val fromId: Int,
|
||||
val date: Int
|
||||
)
|
||||
) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.meloda.fast.api.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "users")
|
||||
data class VkUser(
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
val id: Int,
|
||||
val firstName: String,
|
||||
val lastName: String
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.meloda.fast.api.model.base
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.meloda.fast.api.model.VkConversation
|
||||
import com.meloda.fast.api.model.VkMessage
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -32,6 +34,11 @@ data class BaseVkConversation(
|
||||
val chatSettings: ChatSettings?
|
||||
) : Parcelable {
|
||||
|
||||
fun asVkConversation(lastMessage: VkMessage? = null) = VkConversation(
|
||||
id = peer.id,
|
||||
title = chatSettings?.title,
|
||||
).apply { this.lastMessage = lastMessage }
|
||||
|
||||
@Parcelize
|
||||
data class Peer(
|
||||
val id: Int,
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.meloda.fast.api.model.base
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.meloda.fast.api.model.VkMessage
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import kotlinx.parcelize.RawValue
|
||||
|
||||
@@ -29,6 +30,15 @@ data class BaseVkMessage(
|
||||
val geo: Geo?
|
||||
) : Parcelable {
|
||||
|
||||
fun asVkMessage() = VkMessage(
|
||||
id = id,
|
||||
text = text,
|
||||
isOut = out == 1,
|
||||
peerId = peerId,
|
||||
fromId = fromId,
|
||||
date = date
|
||||
)
|
||||
|
||||
@Parcelize
|
||||
data class Geo(
|
||||
val type: String,
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.meloda.fast.api.model.base
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.meloda.fast.api.model.VkUser
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -46,4 +47,10 @@ data class BaseVkUser(
|
||||
val appId: Int?
|
||||
) : Parcelable
|
||||
|
||||
fun asVkUser() = VkUser(
|
||||
id = id,
|
||||
firstName = firstName,
|
||||
lastName = lastName
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONArray
|
||||
import java.util.*
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.io.Serializable
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.io.Serializable
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import java.util.*
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import com.meloda.fast.api.model.old.VKAttachments
|
||||
import com.meloda.fast.base.adapter.BaseItem
|
||||
import java.io.Serializable
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.util.*
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.util.*
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import com.meloda.fast.api.model.old.VKAttachments
|
||||
import com.meloda.fast.api.model.old.VKModel
|
||||
import org.json.JSONObject
|
||||
|
||||
class VKVideo() : VKModel() {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import android.util.ArrayMap
|
||||
import com.meloda.fast.api.VKUtil
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.meloda.fast.api.model
|
||||
package com.meloda.fast.api.model.old
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
@@ -3,14 +3,14 @@ package com.meloda.fast.api.network.repo
|
||||
import com.meloda.fast.api.base.ApiResponse
|
||||
import com.meloda.fast.api.network.Answer
|
||||
import com.meloda.fast.api.network.VKUrls
|
||||
import com.meloda.fast.api.network.response.ConversationsGetResponse
|
||||
import com.meloda.fast.api.network.request.ConversationsGetRequest
|
||||
import com.meloda.fast.api.network.response.ConversationsGetResponse
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.POST
|
||||
|
||||
interface ConversationsRepo {
|
||||
|
||||
@POST(VKUrls.Conversations.get)
|
||||
suspend fun getAllChats(@Body param: ConversationsGetRequest): Answer<ApiResponse<ConversationsGetResponse>>
|
||||
suspend fun getAllChats(@Body params: ConversationsGetRequest): Answer<ApiResponse<ConversationsGetResponse>>
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.meloda.fast.api.network.repo
|
||||
|
||||
interface MessagesRepo {
|
||||
}
|
||||
@@ -4,14 +4,12 @@ import com.meloda.fast.api.base.ApiResponse
|
||||
import com.meloda.fast.api.model.base.BaseVkUser
|
||||
import com.meloda.fast.api.network.Answer
|
||||
import com.meloda.fast.api.network.VKUrls
|
||||
import com.meloda.fast.api.network.request.UsersGetRequest
|
||||
import dagger.Component
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.QueryMap
|
||||
|
||||
interface UsersRepo {
|
||||
|
||||
@POST(VKUrls.Users.getById)
|
||||
suspend fun getById(@Body param: UsersGetRequest): Answer<ApiResponse<List<BaseVkUser>>>
|
||||
@GET(VKUrls.Users.getById)
|
||||
suspend fun getById(@QueryMap params: Map<String, String>): Answer<ApiResponse<List<BaseVkUser>>>
|
||||
|
||||
}
|
||||
@@ -1,14 +1,21 @@
|
||||
package com.meloda.fast.api.network.request
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class UsersGetRequest(
|
||||
@SerializedName("user_ids")
|
||||
val usersIds: List<Int>,
|
||||
val fields: String? = null,
|
||||
@SerializedName("nom_case")
|
||||
val nomCase: String? = null
|
||||
) : Parcelable
|
||||
) : Parcelable {
|
||||
|
||||
val map
|
||||
get() = mutableMapOf(
|
||||
"user_ids" to usersIds.joinToString { it.toString() }
|
||||
).apply {
|
||||
fields?.let { this["fields"] = it }
|
||||
nomCase?.let { this["nom_case"] = it }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user