mirror of
https://github.com/LiveFastEatTrashRaccoon/RaccoonForLemmy.git
synced 2025-02-03 16:57:41 +01:00
refactor: unifies API calls for data in other instances
This commit is contained in:
parent
739ea6e05b
commit
d08d493b3a
@ -103,7 +103,7 @@ class ModalDrawerViewModel(
|
|||||||
|
|
||||||
mvi.scope?.launch(Dispatchers.IO) {
|
mvi.scope?.launch(Dispatchers.IO) {
|
||||||
mvi.updateState { it.copy(changeInstanceloading = true) }
|
mvi.updateState { it.copy(changeInstanceloading = true) }
|
||||||
val res = communityRepository.getAllInInstance(
|
val res = communityRepository.getAll(
|
||||||
instance = instanceName,
|
instance = instanceName,
|
||||||
page = 1,
|
page = 1,
|
||||||
limit = 1
|
limit = 1
|
||||||
|
@ -5,6 +5,8 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.DefaultMviMode
|
|||||||
import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel
|
import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.CommunityModel
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SearchResultType
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@ -73,12 +75,13 @@ class InstanceInfoViewModel(
|
|||||||
val auth = identityRepository.authToken.value
|
val auth = identityRepository.authToken.value
|
||||||
val refreshing = currentState.refreshing
|
val refreshing = currentState.refreshing
|
||||||
val instance = url.replace("https://", "")
|
val instance = url.replace("https://", "")
|
||||||
val itemList = communityRepository.getAllInInstance(
|
val itemList = communityRepository.getAll(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
instance = instance,
|
instance = instance,
|
||||||
page = currentPage,
|
page = currentPage,
|
||||||
limit = 50,
|
limit = 50,
|
||||||
)
|
resultType = SearchResultType.Communities,
|
||||||
|
)?.filterIsInstance<CommunityModel>()
|
||||||
if (!itemList.isNullOrEmpty()) {
|
if (!itemList.isNullOrEmpty()) {
|
||||||
currentPage++
|
currentPage++
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.utils.to
|
|||||||
|
|
||||||
class CommentRepository(
|
class CommentRepository(
|
||||||
private val services: ServiceProvider,
|
private val services: ServiceProvider,
|
||||||
|
private val customServices: ServiceProvider,
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
const val DEFAULT_PAGE_SIZE = 20
|
const val DEFAULT_PAGE_SIZE = 20
|
||||||
@ -25,13 +26,15 @@ class CommentRepository(
|
|||||||
suspend fun getAll(
|
suspend fun getAll(
|
||||||
postId: Int,
|
postId: Int,
|
||||||
auth: String? = null,
|
auth: String? = null,
|
||||||
|
instance: String? = null,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
|
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
|
||||||
type: ListingType = ListingType.All,
|
type: ListingType = ListingType.All,
|
||||||
sort: SortType = SortType.New,
|
sort: SortType = SortType.New,
|
||||||
maxDepth: Int = 1,
|
maxDepth: Int = 1,
|
||||||
): List<CommentModel>? = runCatching {
|
): List<CommentModel>? = runCatching {
|
||||||
val response = services.comment.getAll(
|
val response = if (instance.isNullOrEmpty()) {
|
||||||
|
services.comment.getAll(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
postId = postId,
|
postId = postId,
|
||||||
page = page,
|
page = page,
|
||||||
@ -40,23 +43,42 @@ class CommentRepository(
|
|||||||
sort = sort.toCommentDto(),
|
sort = sort.toCommentDto(),
|
||||||
maxDepth = maxDepth,
|
maxDepth = maxDepth,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
customServices.changeInstance(instance)
|
||||||
|
customServices.comment.getAll(
|
||||||
|
postId = postId,
|
||||||
|
page = page,
|
||||||
|
limit = limit,
|
||||||
|
type = type.toDto(),
|
||||||
|
sort = sort.toCommentDto(),
|
||||||
|
maxDepth = maxDepth,
|
||||||
|
)
|
||||||
|
}
|
||||||
val dto = response.body()?.comments ?: emptyList()
|
val dto = response.body()?.comments ?: emptyList()
|
||||||
dto.map { it.toModel() }
|
dto.map { it.toModel() }
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
suspend fun getBy(id: Int, auth: String?): CommentModel? = runCatching {
|
suspend fun getBy(id: Int, auth: String?, instance: String? = null): CommentModel? =
|
||||||
services.comment.getBy(id, auth).body()?.commentView?.toModel()
|
runCatching {
|
||||||
|
if (instance.isNullOrEmpty()) {
|
||||||
|
services.comment.getBy(id, auth).body()
|
||||||
|
} else {
|
||||||
|
customServices.changeInstance(instance)
|
||||||
|
customServices.comment.getBy(id).body()
|
||||||
|
}?.commentView?.toModel()
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
suspend fun getChildren(
|
suspend fun getChildren(
|
||||||
parentId: Int,
|
parentId: Int,
|
||||||
auth: String? = null,
|
auth: String? = null,
|
||||||
|
instance: String? = null,
|
||||||
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
|
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
|
||||||
type: ListingType = ListingType.All,
|
type: ListingType = ListingType.All,
|
||||||
sort: SortType = SortType.New,
|
sort: SortType = SortType.New,
|
||||||
maxDepth: Int = 1,
|
maxDepth: Int = 1,
|
||||||
): List<CommentModel>? = runCatching {
|
): List<CommentModel>? = runCatching {
|
||||||
val response = services.comment.getAll(
|
val response = if (instance.isNullOrEmpty()) {
|
||||||
|
services.comment.getAll(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
parentId = parentId,
|
parentId = parentId,
|
||||||
limit = limit,
|
limit = limit,
|
||||||
@ -64,6 +86,16 @@ class CommentRepository(
|
|||||||
sort = sort.toCommentDto(),
|
sort = sort.toCommentDto(),
|
||||||
maxDepth = maxDepth,
|
maxDepth = maxDepth,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
customServices.changeInstance(instance)
|
||||||
|
customServices.comment.getAll(
|
||||||
|
parentId = parentId,
|
||||||
|
limit = limit,
|
||||||
|
type = type.toDto(),
|
||||||
|
sort = sort.toCommentDto(),
|
||||||
|
maxDepth = maxDepth,
|
||||||
|
)
|
||||||
|
}
|
||||||
val dto = response.body()?.comments ?: emptyList()
|
val dto = response.body()?.comments ?: emptyList()
|
||||||
dto.map { it.toModel() }
|
dto.map { it.toModel() }
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
@ -24,12 +24,14 @@ class CommunityRepository(
|
|||||||
suspend fun getAll(
|
suspend fun getAll(
|
||||||
query: String = "",
|
query: String = "",
|
||||||
auth: String? = null,
|
auth: String? = null,
|
||||||
|
instance: String? = null,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int = DEFAULT_PAGE_SIZE,
|
limit: Int = DEFAULT_PAGE_SIZE,
|
||||||
listingType: ListingType = ListingType.All,
|
listingType: ListingType = ListingType.All,
|
||||||
sortType: SortType = SortType.Active,
|
sortType: SortType = SortType.Active,
|
||||||
resultType: SearchResultType = SearchResultType.All,
|
resultType: SearchResultType = SearchResultType.All,
|
||||||
): List<Any>? = runCatching {
|
): List<Any>? = runCatching {
|
||||||
|
if (instance.isNullOrEmpty()) {
|
||||||
val response = services.search.search(
|
val response = services.search.search(
|
||||||
q = query,
|
q = query,
|
||||||
auth = auth,
|
auth = auth,
|
||||||
@ -39,33 +41,21 @@ class CommunityRepository(
|
|||||||
listingType = listingType.toDto(),
|
listingType = listingType.toDto(),
|
||||||
sort = sortType.toDto(),
|
sort = sortType.toDto(),
|
||||||
).body()
|
).body()
|
||||||
|
|
||||||
val posts = response?.posts?.map { it.toModel() }.orEmpty()
|
val posts = response?.posts?.map { it.toModel() }.orEmpty()
|
||||||
val comments = response?.comments?.map { it.toModel() }.orEmpty()
|
val comments = response?.comments?.map { it.toModel() }.orEmpty()
|
||||||
val communities = response?.communities?.map { it.toModel() }.orEmpty()
|
val communities = response?.communities?.map { it.toModel() }.orEmpty()
|
||||||
val users = response?.users?.map { it.toModel() }.orEmpty()
|
val users = response?.users?.map { it.toModel() }.orEmpty()
|
||||||
|
|
||||||
// returns everything
|
// returns everything
|
||||||
posts + comments + communities + users
|
posts + comments + communities + users
|
||||||
}.getOrNull()
|
} else {
|
||||||
|
|
||||||
suspend fun getAllInInstance(
|
|
||||||
instance: String = "",
|
|
||||||
auth: String? = null,
|
|
||||||
page: Int,
|
|
||||||
limit: Int = DEFAULT_PAGE_SIZE,
|
|
||||||
sort: SortType = SortType.Active,
|
|
||||||
): List<CommunityModel>? = runCatching {
|
|
||||||
customServices.changeInstance(instance)
|
customServices.changeInstance(instance)
|
||||||
val response = customServices.community.getAll(
|
val response = customServices.community.getAll(
|
||||||
auth = auth,
|
|
||||||
page = page,
|
page = page,
|
||||||
limit = limit,
|
limit = limit,
|
||||||
sort = sort.toDto(),
|
sort = sortType.toDto(),
|
||||||
).body()
|
).body()
|
||||||
response?.communities?.map {
|
response?.communities?.map { it.toModel() }.orEmpty()
|
||||||
it.toModel()
|
}
|
||||||
}.orEmpty()
|
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
suspend fun getSubscribed(
|
suspend fun getSubscribed(
|
||||||
@ -79,25 +69,21 @@ class CommunityRepository(
|
|||||||
auth: String? = null,
|
auth: String? = null,
|
||||||
id: Int? = null,
|
id: Int? = null,
|
||||||
name: String? = null,
|
name: String? = null,
|
||||||
|
instance: String? = null,
|
||||||
): CommunityModel? = runCatching {
|
): CommunityModel? = runCatching {
|
||||||
val response = services.community.get(
|
val response = if (instance.isNullOrEmpty()) {
|
||||||
|
services.community.get(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
id = id,
|
id = id,
|
||||||
name = name,
|
name = name,
|
||||||
).body()
|
).body()
|
||||||
response?.communityView?.toModel()
|
} else {
|
||||||
}.getOrNull()
|
|
||||||
|
|
||||||
suspend fun getInInstance(
|
|
||||||
auth: String? = null,
|
|
||||||
name: String? = null,
|
|
||||||
instance: String,
|
|
||||||
): CommunityModel? = runCatching {
|
|
||||||
customServices.changeInstance(instance)
|
customServices.changeInstance(instance)
|
||||||
val response = customServices.community.get(
|
customServices.community.get(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
name = name,
|
name = name,
|
||||||
).body()
|
).body()
|
||||||
|
}
|
||||||
response?.communityView?.toModel()
|
response?.communityView?.toModel()
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
|
@ -32,8 +32,10 @@ class PostRepository(
|
|||||||
type: ListingType = ListingType.Local,
|
type: ListingType = ListingType.Local,
|
||||||
sort: SortType = SortType.Active,
|
sort: SortType = SortType.Active,
|
||||||
communityId: Int? = null,
|
communityId: Int? = null,
|
||||||
|
instance: String? = null,
|
||||||
): List<PostModel>? = runCatching {
|
): List<PostModel>? = runCatching {
|
||||||
val response = services.post.getAll(
|
val response = if (instance.isNullOrEmpty()) {
|
||||||
|
services.post.getAll(
|
||||||
auth = auth,
|
auth = auth,
|
||||||
communityId = communityId,
|
communityId = communityId,
|
||||||
page = page,
|
page = page,
|
||||||
@ -41,32 +43,31 @@ class PostRepository(
|
|||||||
type = type.toDto(),
|
type = type.toDto(),
|
||||||
sort = sort.toDto(),
|
sort = sort.toDto(),
|
||||||
)
|
)
|
||||||
val dto = response.body()?.posts ?: emptyList()
|
} else {
|
||||||
dto.map { it.toModel() }
|
|
||||||
}.getOrNull()
|
|
||||||
|
|
||||||
suspend fun getAllInInstance(
|
|
||||||
instance: String,
|
|
||||||
page: Int,
|
|
||||||
limit: Int = DEFAULT_PAGE_SIZE,
|
|
||||||
type: ListingType = ListingType.Local,
|
|
||||||
sort: SortType = SortType.Active,
|
|
||||||
communityId: Int? = null,
|
|
||||||
): List<PostModel>? = runCatching {
|
|
||||||
customServices.changeInstance(instance)
|
customServices.changeInstance(instance)
|
||||||
val response = customServices.post.getAll(
|
customServices.post.getAll(
|
||||||
communityId = communityId,
|
communityId = communityId,
|
||||||
page = page,
|
page = page,
|
||||||
limit = limit,
|
limit = limit,
|
||||||
type = type.toDto(),
|
type = type.toDto(),
|
||||||
sort = sort.toDto(),
|
sort = sort.toDto(),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
val dto = response.body()?.posts ?: emptyList()
|
val dto = response.body()?.posts ?: emptyList()
|
||||||
dto.map { it.toModel() }
|
dto.map { it.toModel() }
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
suspend fun get(id: Int, auth: String? = null): PostModel? = runCatching {
|
suspend fun get(
|
||||||
val dto = services.post.get(auth, id).body()?.postView
|
id: Int,
|
||||||
|
auth: String? = null,
|
||||||
|
instance: String? = null,
|
||||||
|
): PostModel? = runCatching {
|
||||||
|
val dto = if (instance.isNullOrEmpty()) {
|
||||||
|
services.post.get(auth, id).body()?.postView
|
||||||
|
} else {
|
||||||
|
customServices.changeInstance(instance)
|
||||||
|
customServices.post.get(id = id).body()?.postView
|
||||||
|
}
|
||||||
dto?.toModel()
|
dto?.toModel()
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel
|
|||||||
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.AccountRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase.LoginUseCase
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.usecase.LoginUseCase
|
||||||
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SearchResultType
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.resources.MR
|
import com.github.diegoberaldin.raccoonforlemmy.resources.MR
|
||||||
@ -105,10 +106,11 @@ class LoginBottomSheetViewModel(
|
|||||||
mvi.scope?.launch(Dispatchers.IO) {
|
mvi.scope?.launch(Dispatchers.IO) {
|
||||||
mvi.updateState { it.copy(loading = true) }
|
mvi.updateState { it.copy(loading = true) }
|
||||||
|
|
||||||
val res = communityRepository.getAllInInstance(
|
val res = communityRepository.getAll(
|
||||||
instance = instance,
|
instance = instance,
|
||||||
page = 1,
|
page = 1,
|
||||||
limit = 1
|
limit = 1,
|
||||||
|
resultType = SearchResultType.Communities,
|
||||||
) ?: emptyList()
|
) ?: emptyList()
|
||||||
if (res.isEmpty()) {
|
if (res.isEmpty()) {
|
||||||
mvi.updateState {
|
mvi.updateState {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user