diff --git a/app/src/main/kotlin/com/meloda/fast/api/base/ApiError.kt b/app/src/main/kotlin/com/meloda/fast/api/base/ApiError.kt new file mode 100644 index 00000000..75814dd4 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/base/ApiError.kt @@ -0,0 +1,11 @@ +package com.meloda.fast.api.base + +import com.google.gson.annotations.SerializedName +import java.io.IOException + +data class ApiError( + @SerializedName("error_code") + val errorCode: Int, + @SerializedName("error_msg") + override var message: String +) : IOException() diff --git a/app/src/main/kotlin/com/meloda/fast/api/base/ApiResponse.kt b/app/src/main/kotlin/com/meloda/fast/api/base/ApiResponse.kt new file mode 100644 index 00000000..51c3cd35 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/base/ApiResponse.kt @@ -0,0 +1,8 @@ +package com.meloda.fast.api.base + +data class ApiResponse( + val error: ApiError? = null, + val response: T? = null +) { + val isSuccessful get() = error == null && response != null +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKConversation.kt b/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKConversation.kt new file mode 100644 index 00000000..d4ea213b --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKConversation.kt @@ -0,0 +1,130 @@ +package com.meloda.fast.api.model + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class BaseVKConversation( + val peer: Peer, + @SerializedName("last_message_id") + val lastMessageId: Int, + @SerializedName("in_read") + val inRead: Int, + @SerializedName("out_read") + val outRead: Int, + @SerializedName("sort_id") + val sortId: SortId, + @SerializedName("last_conversation_message_id") + val lastConversationMessageId: Int, + @SerializedName("is_marked_unread") + val isMarkedUnread: Boolean, + val important: Boolean, + @SerializedName("push_settings") + val pushSettings: PushSettings, + @SerializedName("can_write") + val canWrite: CanWrite, + @SerializedName("can_send_money") + val canSendMoney: Boolean, + @SerializedName("can_receive_money") + val canReceiveMoney: Boolean, + @SerializedName("chat_settings") + val chatSettings: ChatSettings +) : Parcelable { + + @Parcelize + data class Peer( + val id: Int, + val type: String, + @SerializedName("local_id") + val localId: Int + ) : Parcelable + + @Parcelize + data class SortId( + @SerializedName("major_id") + val majorId: Int, + @SerializedName("minor_id") + val minorId: Int + ) : Parcelable + + @Parcelize + data class PushSettings( + @SerializedName("disabled_forever") + val disabledForever: Boolean, + @SerializedName("no_sound") + val noSound: Boolean, + @SerializedName("disabled_mentions") + val disabledMentions: Boolean, + @SerializedName("disabled_mass_mentions") + val disabledMassMentions: Boolean + ) : Parcelable + + @Parcelize + data class CanWrite( + val allowed: Boolean + ) : Parcelable + + @Parcelize + data class ChatSettings( + @SerializedName("owner_id") + val ownerId: Int, + val title: String, + val state: String, + val acl: Acl, + @SerializedName("members_count") + val membersCount: Int, + @SerializedName("friends_count") + val friendsCount: Int, + val photo: Photo, + @SerializedName("admin_ids") + val adminsIds: List, + @SerializedName("active_ids") + val activeIds: List, + @SerializedName("is_group_channel") + val isGroupChannel: Boolean, + @SerializedName("is_disappearing") + val isDisappearing: Boolean, + @SerializedName("is_service") + val isService: Boolean + ) : Parcelable { + + @Parcelize + data class Acl( + @SerializedName("can_change_info") + val canChangeInfo: Boolean, + @SerializedName("can_change_invite_link") + val canChangeInviteLink: Boolean, + @SerializedName("can_change_pin") + val canChangePin: Boolean, + @SerializedName("can_invite") + val canInvite: Boolean, + @SerializedName("can_promote_users") + val canPromoteUsers: Boolean, + @SerializedName("can_see_invite_link") + val canSeeInviteLink: Boolean, + @SerializedName("can_moderate") + val canModerate: Boolean, + @SerializedName("can_copy_chat") + val canCopyChat: Boolean, + @SerializedName("can_call") + val canCall: Boolean, + @SerializedName("can_use_mass_mentions") + val canUseMassMentions: Boolean, + @SerializedName("can_change_style") + val canChangeStyle: Boolean + ) : Parcelable + + @Parcelize + data class Photo( + @SerializedName("photo_50") + val photo50: String, + @SerializedName("photo_100") + val photo100: String, + @SerializedName("photo_200") + val photo200: String, + @SerializedName("is_default_photo") + val isDefaultPhoto: Boolean + ) : Parcelable + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKMessage.kt b/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKMessage.kt new file mode 100644 index 00000000..0f90bda9 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/BaseVKMessage.kt @@ -0,0 +1,47 @@ +package com.meloda.fast.api.model + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize +import kotlinx.parcelize.RawValue + +@Parcelize +data class BaseVKMessage( + val date: Int, + @SerializedName("from_id") + val fromId: Int, + val id: Int, + val out: Int, + @SerializedName("peer_id") + val peerId: Int, + val text: String, + @SerializedName("conversation_message_id") + val conversationMessageId: Int, + @SerializedName("fwd_messages") + val fwdMessages: List = listOf(), + val important: Boolean, + @SerializedName("random_id") + val randomId: Int, + val attachments: @RawValue List = listOf(), + @SerializedName("is_hidden") + val isHidden: Boolean, + val payload: String, + val geo: Geo? +) : Parcelable { + + @Parcelize + data class Geo( + val type: String, + val coordinates: Coordinates, + val place: Place + ) : Parcelable { + + + @Parcelize + data class Coordinates(val latitude: Float, val longitude: Float) : Parcelable + + @Parcelize + data class Place(val country: String, val city: String, val title: String) : Parcelable + } + +} diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/BaseVKAttachmentItem.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/BaseVKAttachmentItem.kt new file mode 100644 index 00000000..8808bc4a --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/BaseVKAttachmentItem.kt @@ -0,0 +1,16 @@ +package com.meloda.fast.api.model.attachments + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class BaseVKAttachmentItem( + val type: String, + val photo: VKPhotoAttachment?, + val video: VKVideoAttachment?, + val audio: VKAudioAttachment?, + val doc: VKFileAttachment?, + val link: VKLinkAttachment? +) : Parcelable + +abstract class BaseVKAttachment : Parcelable \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKAudioAttachment.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKAudioAttachment.kt new file mode 100644 index 00000000..c4e3423b --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKAudioAttachment.kt @@ -0,0 +1,71 @@ +package com.meloda.fast.api.model.attachments + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class VKAudioAttachment( + val id: Int, + val title: String, + val artist: String, + val duration: Int, + val url: String, + val date: Int, + @SerializedName("owner_id") + val ownerId: Int, + @SerializedName("access_key") + val accessKey: String, + @SerializedName("is_explicit") + val isExplicit: Boolean, + @SerializedName("is_focus_track") + val isFocusTrack: Boolean, + @SerializedName("is_licensed") + val isLicensed: Boolean, + @SerializedName("track_code") + val trackCode: String, + @SerializedName("genre_id") + val genreId: Int, + val album: Album, + @SerializedName("short_videos_allowed") + val shortVideosAllowed: Boolean, + @SerializedName("stories_allowed") + val storiesAllowed: Boolean, + @SerializedName("stories_cover_allowed") + val storiesCoverAllowed: Boolean +) : BaseVKAttachment() { + + @Parcelize + data class Album( + val id: Int, + val title: String, + @SerializedName("owner_id") + val ownerId: Int, + @SerializedName("access_key") + val accessKey: String, + val thumb: Thumb + ) : Parcelable { + + @Parcelize + data class Thumb( + val width: Int, + val height: Int, + @SerializedName("photo_34") + val photo34: String, + @SerializedName("photo_68") + val photo68: String, + @SerializedName("photo_135") + val photo135: String, + @SerializedName("photo_270") + val photo270: String, + @SerializedName("photo_300") + val photo300: String, + @SerializedName("photo_600") + val photo600: String, + @SerializedName("photo_1200") + val photo1200: String + ) : Parcelable + + } + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKFileAttachment.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKFileAttachment.kt new file mode 100644 index 00000000..f2515255 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKFileAttachment.kt @@ -0,0 +1,47 @@ +package com.meloda.fast.api.model.attachments + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class VKFileAttachment( + val id: Int, + @SerializedName("owner_id") + val ownerId: Int, + val title: String, + val size: Int, + val ext: String, + val date: Int, + val type: Int, + val url: String, + val preview: Preview?, + @SerializedName("is_licensed") + val isLicensed: Int, + @SerializedName("access_key") + val accessKey: String, + @SerializedName("web_preview_url") + val webPreviewUrl: String? +) : BaseVKAttachment() { + + @Parcelize + data class Preview( + val photo: Photo?, + val video: Video? + ) : Parcelable { + + @Parcelize + data class Photo(val sizes: List) : Parcelable + + @Parcelize + data class Video( + val src: String, + val width: Int, + val height: Int, + @SerializedName("file_size") + val fileSize: Int + ) : Parcelable + + } + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKLinkAttachment.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKLinkAttachment.kt new file mode 100644 index 00000000..a65be339 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKLinkAttachment.kt @@ -0,0 +1,15 @@ +package com.meloda.fast.api.model.attachments + +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class VKLinkAttachment( + val url: String, + val title: String, + val caption: String, + val photo: VKPhotoAttachment, + val target: String, + @SerializedName("is_favorite") + val isFavorite: Boolean +) : BaseVKAttachment() \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKPhotoAttachment.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKPhotoAttachment.kt new file mode 100644 index 00000000..a62e4e49 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKPhotoAttachment.kt @@ -0,0 +1,32 @@ +package com.meloda.fast.api.model.attachments + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class VKPhotoAttachment( + @SerializedName("album_id") + val albumId: Int, + val date: Int, + val id: Int, + @SerializedName("owner_id") + val ownerId: Int, + @SerializedName("has_tags") + val hasTags: Boolean, + @SerializedName("access_key") + val accessKey: String, + val sizes: List, + val text: String, + @SerializedName("user_id") + val userId: Int? +) : BaseVKAttachment() + +@Parcelize +data class Size( + val height: Int, + val width: Int, + val type: String, + @SerializedName("url", alternate = ["src"]) + val url: String, +) : Parcelable \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKVideoAttachment.kt b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKVideoAttachment.kt new file mode 100644 index 00000000..a6782687 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/model/attachments/VKVideoAttachment.kt @@ -0,0 +1,111 @@ +package com.meloda.fast.api.model.attachments + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class VKVideoAttachment( + val id: Int, + val title: String, + val width: Int, + val height: Int, + val duration: Int, + val date: Int, + val comments: Int, + val description: String, + val player: String, + val added: Int, + val type: String, + val views: Int, + @SerializedName("can_comment") + val canComment: Int, + @SerializedName("can_edit") + val canEdit: Int, + @SerializedName("can_like") + val canLike: Int, + @SerializedName("can_repost") + val canRepost: Int, + @SerializedName("can_subscribe") + val canSubscribe: Int, + @SerializedName("can_add_to_faves") + val canAddToFaves: Int, + @SerializedName("can_add") + val canAdd: Int, + @SerializedName("can_attach_link") + val canAttachLink: Int, + @SerializedName("access_key") + val accessKey: String, + @SerializedName("owner_id") + val ownerId: Int, + @SerializedName("ov_id") + val ovId: String, + @SerializedName("is_favorite") + val isFavorite: Boolean, + @SerializedName("track_code") + val trackCode: String, + val image: List, + @SerializedName("first_frame") + val firstFrame: List, + val files: List, + @SerializedName("timeline_thumbs") + val timelineThumbs: TimelineThumbs + //ads +) : BaseVKAttachment() { + + @Parcelize + data class Image( + val height: Int, + val width: Int, + val url: String, + @SerializedName("with_padding") + val withPadding: Int + ) : Parcelable + + @Parcelize + data class FirstFrame( + val height: Int, + val width: Int, + val url: String + ) : Parcelable + + @Parcelize + data class File( + val mp4_240: String, + val mp4_360: String, + val mp4_480: String, + val mp4_720: String, + val mp4_1080: String, + val mp4_1440: String, + val hls: String, + @SerializedName("dash_uni") + val dashUni: String, + @SerializedName("dash_sep") + val dashSep: String, + @SerializedName("hls_ondemand") + val hlsOnDemand: String, + @SerializedName("dash_ondemand") + val dashOnDemand: String, + @SerializedName("failover_host") + val failOverHost: String + ) : Parcelable + + @Parcelize + data class TimelineThumbs( + @SerializedName("count_per_image") + val countPerImage: Int, + @SerializedName("count_per_row") + val countPerRow: Int, + @SerializedName("count_total") + val countTotal: Int, + @SerializedName("frame_height") + val frameHeight: Int, + @SerializedName("frame_width") + val frameWidth: Float, + val links: List, + @SerializedName("is_uv") + val isUv: Boolean, + val frequency: Int + ) : Parcelable + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/AuthInterceptor.kt b/app/src/main/kotlin/com/meloda/fast/api/network/AuthInterceptor.kt index b97282a4..8649e89e 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/network/AuthInterceptor.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/network/AuthInterceptor.kt @@ -1,5 +1,6 @@ package com.meloda.fast.api.network +import com.meloda.fast.api.UserConfig import com.meloda.fast.api.VKConstants import okhttp3.Interceptor import okhttp3.Response @@ -10,6 +11,12 @@ class AuthInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val builder = chain.request().url.newBuilder() .addQueryParameter("v", URLEncoder.encode(VKConstants.API_VERSION, "utf-8")) + + UserConfig.accessToken.let { + if (it.isNotBlank()) + builder.addQueryParameter("access_token", URLEncoder.encode(it, "utf-8")) + } + return chain.proceed(chain.request().newBuilder().apply { url(builder.build()) }.build()) } diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/VKModules.kt b/app/src/main/kotlin/com/meloda/fast/api/network/VKModules.kt index b7c7ba1d..8c0ec255 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/network/VKModules.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/network/VKModules.kt @@ -3,7 +3,9 @@ package com.meloda.fast.api.network import com.google.gson.Gson import com.google.gson.GsonBuilder import com.meloda.fast.api.network.datasource.AuthDataSource +import com.meloda.fast.api.network.datasource.ConversationsDataSource import com.meloda.fast.api.network.repo.AuthRepo +import com.meloda.fast.api.network.repo.ConversationsRepo import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -62,4 +64,12 @@ class VKModules { fun provideAuthDataSource(repo: AuthRepo): AuthDataSource = AuthDataSource(repo) + @Provides + fun provideConversationsRepo(retrofit: Retrofit): ConversationsRepo = + retrofit.create(ConversationsRepo::class.java) + + @Provides + fun provideConversationsDataSource(repo: ConversationsRepo): ConversationsDataSource = + ConversationsDataSource(repo) + } \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/datasource/ConversationsDataSource.kt b/app/src/main/kotlin/com/meloda/fast/api/network/datasource/ConversationsDataSource.kt new file mode 100644 index 00000000..9135fd36 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/network/datasource/ConversationsDataSource.kt @@ -0,0 +1,13 @@ +package com.meloda.fast.api.network.datasource + +import com.meloda.fast.api.network.repo.ConversationsRepo +import com.meloda.fast.api.network.request.ConversationsGetRequest +import javax.inject.Inject + +class ConversationsDataSource @Inject constructor( + private val repo: ConversationsRepo +) : ConversationsRepo { + + override suspend fun getAllChats(param: ConversationsGetRequest) = repo.getAllChats(param) + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/repo/ConversationsRepo.kt b/app/src/main/kotlin/com/meloda/fast/api/network/repo/ConversationsRepo.kt index 0cd3ba30..b9768bd6 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/network/repo/ConversationsRepo.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/network/repo/ConversationsRepo.kt @@ -1,18 +1,16 @@ 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.GetConversationsResponse -import retrofit2.http.* +import com.meloda.fast.api.network.response.ConversationsGetResponse +import com.meloda.fast.api.network.request.ConversationsGetRequest +import retrofit2.http.Body +import retrofit2.http.POST interface ConversationsRepo { - @FormUrlEncoded @POST(VKUrls.Conversations.get) - suspend fun getAllChats( - @Field("user_id") chatId: Int, - @Field("token") token: String - ): Answer - + suspend fun getAllChats(@Body param: ConversationsGetRequest): Answer> } \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequest.kt b/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequest.kt new file mode 100644 index 00000000..46a3275e --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequest.kt @@ -0,0 +1,16 @@ +package com.meloda.fast.api.network.request + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import kotlinx.parcelize.Parcelize + +@Parcelize +data class ConversationsGetRequest( + val count: Int? = null, + val offset: Int? = null, + val fields: String = "", + val filter: String = "all", + val extended: Boolean? = true, + @SerializedName("start_message_id") + val startMessageId: Int? = null +) : Parcelable \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequests.kt b/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequests.kt deleted file mode 100644 index 9eb8dcd5..00000000 --- a/app/src/main/kotlin/com/meloda/fast/api/network/request/ConversationsRequests.kt +++ /dev/null @@ -1 +0,0 @@ -package com.meloda.fast.api.network.request diff --git a/app/src/main/kotlin/com/meloda/fast/api/network/response/ConversationsResponse.kt b/app/src/main/kotlin/com/meloda/fast/api/network/response/ConversationsResponse.kt index dfe2424b..fcc68551 100644 --- a/app/src/main/kotlin/com/meloda/fast/api/network/response/ConversationsResponse.kt +++ b/app/src/main/kotlin/com/meloda/fast/api/network/response/ConversationsResponse.kt @@ -1 +1,22 @@ -package com.meloda.fast.api.network.response \ No newline at end of file +package com.meloda.fast.api.network.response + +import android.os.Parcelable +import com.google.gson.annotations.SerializedName +import com.meloda.fast.api.model.BaseVKConversation +import com.meloda.fast.api.model.BaseVKMessage +import kotlinx.parcelize.Parcelize + +@Parcelize +data class ConversationsGetResponse( + val count: Int, + val items: List, + @SerializedName("unread_count") + val unreadCount: Int? +) : Parcelable + +@Parcelize +data class ConversationsResponseItems( + val conversation: BaseVKConversation, + @SerializedName("last_message") + val lastMessage: BaseVKMessage +) : Parcelable \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/extensions/Extensions.kt b/app/src/main/kotlin/com/meloda/fast/extensions/Extensions.kt new file mode 100644 index 00000000..64acf668 --- /dev/null +++ b/app/src/main/kotlin/com/meloda/fast/extensions/Extensions.kt @@ -0,0 +1,3 @@ +package com.meloda.fast.extensions + +fun Boolean.toApiStyle() = (if (this) 1 else 0).toString() \ No newline at end of file diff --git a/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsFragment.kt b/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsFragment.kt index e311d331..3af74b8c 100644 --- a/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsFragment.kt +++ b/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsFragment.kt @@ -16,7 +16,8 @@ import dagger.hilt.android.AndroidEntryPoint import kotlin.math.roundToInt @AndroidEntryPoint -class ConversationsFragment : BaseViewModelFragment(R.layout.fragment_conversations) { +class ConversationsFragment : + BaseViewModelFragment(R.layout.fragment_conversations) { override val viewModel: ConversationsViewModel by viewModels() private val binding: FragmentConversationsBinding by viewBinding() @@ -34,14 +35,15 @@ class ConversationsFragment : BaseViewModelFragment(R.la override fun onEvent(event: VKEvent) { super.onEvent(event) when (event) { - StartProgressEvent -> onProgressStarted() - StopProgressEvent -> onProgressStopped() + is ConversationsLoaded -> return + is StartProgressEvent -> onProgressStarted() + is StopProgressEvent -> onProgressStopped() } } private fun onProgressStarted() { if (adapter.isEmpty()) - binding.progressBar.isVisible = true + binding.progressBar.isVisible = true } private fun onProgressStopped() { diff --git a/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsViewModel.kt b/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsViewModel.kt index 035567d5..2f28a95d 100644 --- a/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsViewModel.kt +++ b/app/src/main/kotlin/com/meloda/fast/screens/messages/ConversationsViewModel.kt @@ -1,13 +1,60 @@ package com.meloda.fast.screens.messages import androidx.lifecycle.viewModelScope +import com.meloda.fast.api.VKConstants +import com.meloda.fast.api.model.BaseVKConversation +import com.meloda.fast.api.model.BaseVKMessage +import com.meloda.fast.api.network.repo.ConversationsRepo +import com.meloda.fast.api.network.request.ConversationsGetRequest import com.meloda.fast.base.viewmodel.BaseViewModel +import com.meloda.fast.base.viewmodel.VKEvent +import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import javax.inject.Inject -class ConversationsViewModel : BaseViewModel() { +@HiltViewModel +class ConversationsViewModel @Inject constructor( + private val repo: ConversationsRepo +) : BaseViewModel() { fun loadConversations() = viewModelScope.launch(Dispatchers.Default) { - + makeJob({ + repo.getAllChats( + ConversationsGetRequest( + count = 30, + fields = "${VKConstants.USER_FIELDS},${VKConstants.GROUP_FIELDS}" + ) + ) + }, + onAnswer = { + it.response?.let { response -> + sendEvent( + ConversationsLoaded( + count = response.count, + unreadCount = response.unreadCount ?: 0, + messages = response.items.map { items -> items.lastMessage }, + conversations = response.items.map { items -> items.conversation } + ) + ) + } + }, + onError = { + val er = it + val i = 0 + }, + onStart = { + val i = 0 + }, + onEnd = { + val i = 0 + }) } -} \ No newline at end of file +} + +data class ConversationsLoaded( + val count: Int, + val unreadCount: Int, + val messages: List, + val conversations: List +) : VKEvent() \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 1e4a8ca4..a6be6547 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ buildscript { } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20") - classpath("com.android.tools.build:gradle:7.0.1") + classpath("com.android.tools.build:gradle:7.0.2") classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5") classpath("com.google.dagger:hilt-android-gradle-plugin:2.37")