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