refactor: unifies API calls for data in other instances

This commit is contained in:
Diego Beraldin 2023-10-16 18:59:34 +02:00
parent 739ea6e05b
commit d08d493b3a
6 changed files with 133 additions and 109 deletions

View File

@ -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

View File

@ -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++
}

View File

@ -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,45 +26,76 @@ 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(
auth = auth,
postId = postId,
page = page,
limit = limit,
type = type.toDto(),
sort = sort.toCommentDto(),
maxDepth = maxDepth,
)
val response = if (instance.isNullOrEmpty()) {
services.comment.getAll(
auth = auth,
postId = postId,
page = page,
limit = limit,
type = type.toDto(),
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()
}.getOrNull()
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(
auth = auth,
parentId = parentId,
limit = limit,
type = type.toDto(),
sort = sort.toCommentDto(),
maxDepth = maxDepth,
)
val response = if (instance.isNullOrEmpty()) {
services.comment.getAll(
auth = auth,
parentId = parentId,
limit = limit,
type = type.toDto(),
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()

View File

@ -24,48 +24,38 @@ 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 {
val response = services.search.search(
q = query,
auth = auth,
page = page,
limit = limit,
type = resultType.toDto(),
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 {
customServices.changeInstance(instance)
val response = customServices.community.getAll(
auth = auth,
page = page,
limit = limit,
sort = sort.toDto(),
).body()
response?.communities?.map {
it.toModel()
}.orEmpty()
if (instance.isNullOrEmpty()) {
val response = services.search.search(
q = query,
auth = auth,
page = page,
limit = limit,
type = resultType.toDto(),
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
} else {
customServices.changeInstance(instance)
val response = customServices.community.getAll(
page = page,
limit = limit,
sort = sortType.toDto(),
).body()
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(
auth = auth,
id = id,
name = name,
).body()
response?.communityView?.toModel()
}.getOrNull()
suspend fun getInInstance(
auth: String? = null,
name: String? = null,
instance: String,
): CommunityModel? = runCatching {
customServices.changeInstance(instance)
val response = customServices.community.get(
auth = auth,
name = name,
).body()
val response = if (instance.isNullOrEmpty()) {
services.community.get(
auth = auth,
id = id,
name = name,
).body()
} else {
customServices.changeInstance(instance)
customServices.community.get(
auth = auth,
name = name,
).body()
}
response?.communityView?.toModel()
}.getOrNull()

View File

@ -32,41 +32,42 @@ class PostRepository(
type: ListingType = ListingType.Local,
sort: SortType = SortType.Active,
communityId: Int? = null,
instance: String? = null,
): List<PostModel>? = runCatching {
val response = services.post.getAll(
auth = auth,
communityId = communityId,
page = page,
limit = limit,
type = type.toDto(),
sort = sort.toDto(),
)
val response = if (instance.isNullOrEmpty()) {
services.post.getAll(
auth = auth,
communityId = communityId,
page = page,
limit = limit,
type = type.toDto(),
sort = sort.toDto(),
)
} else {
customServices.changeInstance(instance)
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 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)
val response = 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()

View File

@ -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 {