Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api Issue fixes and code refactoring #85

Open
wants to merge 28 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b00947a
Build(deps): bump kotlin from 1.8.21 to 1.8.22
dependabot[bot] Jun 8, 2023
5e0918c
Merge pull request #60 from CodeCrowCorp/dependabot/gradle/kotlin-1.8.22
mergify[bot] Jun 8, 2023
9ccc72a
Build(deps): bump com.google.devtools.ksp
dependabot[bot] Jun 9, 2023
2fcbe9a
Merge pull request #61 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jun 9, 2023
a9c5b48
Build(deps): bump com.google.accompanist:accompanist-systemuicontroller
dependabot[bot] Jun 22, 2023
fedefc3
Merge pull request #62 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jun 22, 2023
a720baa
Build(deps): bump org.jetbrains.kotlinx:kotlinx-coroutines-test
dependabot[bot] Jun 30, 2023
002b48b
Merge pull request #63 from CodeCrowCorp/dependabot/gradle/org.jetbra…
mergify[bot] Jun 30, 2023
d840ef8
Build(deps): bump com.google.accompanist:accompanist-systemuicontroller
dependabot[bot] Jul 3, 2023
cd91670
Merge pull request #64 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jul 3, 2023
2e2a099
Build(deps): bump kotlin from 1.8.22 to 1.9.0
dependabot[bot] Jul 6, 2023
4754382
Merge pull request #66 from CodeCrowCorp/dependabot/gradle/kotlin-1.9.0
mergify[bot] Jul 6, 2023
9a95cb1
Build(deps): bump com.google.devtools.ksp
dependabot[bot] Jul 7, 2023
bf73ea2
Merge pull request #67 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jul 7, 2023
8fd9c7b
Build(deps): bump hilt from 2.46.1 to 2.47
dependabot[bot] Jul 14, 2023
bb9c8c7
Merge pull request #68 from CodeCrowCorp/dependabot/gradle/hilt-2.47
mergify[bot] Jul 14, 2023
da86da2
Build(deps): bump com.google.devtools.ksp
dependabot[bot] Jul 21, 2023
21dbb43
Merge pull request #69 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jul 21, 2023
49d1a25
Build(deps): bump org.jetbrains.kotlinx:kotlinx-coroutines-test
dependabot[bot] Jul 26, 2023
7bd8bdb
Merge pull request #70 from CodeCrowCorp/dependabot/gradle/org.jetbra…
mergify[bot] Jul 26, 2023
dbcd245
Build(deps): bump com.google.accompanist:accompanist-systemuicontroller
dependabot[bot] Jul 28, 2023
8b19742
Merge pull request #74 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Jul 28, 2023
7ceffe9
Build(deps): bump com.google.devtools.ksp
dependabot[bot] Aug 3, 2023
76e80bf
Merge pull request #75 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Aug 3, 2023
7767c72
Build(deps): bump com.google.accompanist:accompanist-systemuicontroller
dependabot[bot] Aug 8, 2023
f19f9e7
Merge pull request #76 from CodeCrowCorp/dependabot/gradle/com.google…
mergify[bot] Aug 8, 2023
29b7a1f
able to run project by fixing changed json structure
Aug 9, 2023
57051b4
merging with dev
Aug 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ dependencies {

//Accompanist
implementation(libs.accompanist)

implementation (libs.logging.interceptor)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.codecrow.mage.data

import com.google.gson.Gson
import io.codecrow.mage.model.response.ErrorResponse
import io.codecrow.mage.remote.model.response.ErrorResponse
import retrofit2.Response
import java.io.IOException

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.codecrow.mage.data.datasource

import io.codecrow.mage.data.DataException
import io.codecrow.mage.data.Either
import io.codecrow.mage.model.Channel
import io.codecrow.mage.remote.model.Channel
import io.codecrow.mage.remote.utils.Resource
import kotlinx.coroutines.flow.Flow

interface ChannelRemote {
fun getAllChannels(searchQuery: String, skip: Int, limit: Int): Flow<Resource<ArrayList<Channel>>>

suspend fun getChannels(searchQuery: String, skip: Int, limit: Int): Either<DataException, List<Channel>>
fun getChannelByID(channelId: String) : Flow<Resource<Channel>>
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package io.codecrow.mage.data.datasource

import io.codecrow.mage.data.DataException
import io.codecrow.mage.data.Either
import io.codecrow.mage.data.service.ChannelApi
import io.codecrow.mage.data.handle
import io.codecrow.mage.model.Channel
import io.codecrow.mage.remote.mapper.ChannelMapper
import io.codecrow.mage.remote.model.Channel
import io.codecrow.mage.remote.utils.Resource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class ChannelRemoteImpl @Inject constructor(
private val api: ChannelApi
private val channelApi: ChannelApi,
private val channelMapper: ChannelMapper
): ChannelRemote {

override suspend fun getChannels(searchQuery: String, skip: Int, limit: Int): Either<DataException, List<Channel>> {
return handle({
api.getChannels(searchQuery, skip, limit)
}) {
Either.Right(it ?: emptyList())
override fun getAllChannels(
searchQuery: String,
skip: Int,
limit: Int
): Flow<Resource<ArrayList<Channel>>> {
return flow {
emit(Resource.loading())
emit(Resource.success(data = channelMapper.mapAllChannels(channelApi.getChannels(searchQuery, skip, limit))))
}.catch { e ->
emit(Resource.error(e))
}
}

override fun getChannelByID(channelId: String): Flow<Resource<Channel>> {
return flow {
emit(Resource.loading())
emit(Resource.success(data = channelMapper.map(channelApi.getChannel(channelId))))
}.catch { e ->
emit(Resource.error(e))
}
}

}
16 changes: 7 additions & 9 deletions app/src/main/java/io/codecrow/mage/data/di/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.codecrow.mage.data.service.ChannelApi
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
Expand All @@ -14,19 +17,14 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object ApiModule {

@Singleton
@Provides
fun providesGsonBuilder(): Gson =
GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create()

@Singleton
@Provides
fun providesRetrofit(gson: Gson): Retrofit =
fun providesRetrofit(): ChannelApi =
Retrofit.Builder()
.baseUrl("https://dev.api.mage.stream/")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ChannelApi::class.java)

}

This file was deleted.

7 changes: 4 additions & 3 deletions app/src/main/java/io/codecrow/mage/data/service/ChannelApi.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.codecrow.mage.data.service

import io.codecrow.mage.model.Channel
import io.codecrow.mage.remote.model.Channel
import io.codecrow.mage.remote.model.channel_response.ChannelResponseItem
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
Expand All @@ -16,7 +17,7 @@ interface ChannelApi {
suspend fun deleteChannel(@Query("channelId") channelId: String): List<Channel>

@GET("channel")
suspend fun getChannel(@Query("channelId") channelId: String): Channel
suspend fun getChannel(@Query("channelId") channelId: String): ChannelResponseItem

@GET("channels/friend")
suspend fun getFriendChannel(@Query("title") title: String): Channel
Expand All @@ -25,7 +26,7 @@ interface ChannelApi {
suspend fun getMyChannels(@Query("searchQuery") searchQuery: String, @Query("techStack") techStack: String, @Query("skip") skip: String, @Query("limit") limit: String): List<Channel>

@GET("channels")
suspend fun getChannels(@Query("searchQuery") searchQuery: String, @Query("skip") skip: Int, @Query("limit") limit: Int): Response<List<Channel>>
suspend fun getChannels(@Query("searchQuery") searchQuery: String, @Query("skip") skip: Int, @Query("limit") limit: Int): ArrayList<ChannelResponseItem>

@GET("channels/user")
suspend fun getChannelsByUserId(@Query("searchQuery") searchQuery: String, @Query("skip") skip: String, @Query("limit") limit: String): List<Channel>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/codecrow/mage/model/Channel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.codecrow.mage.model
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import io.codecrow.mage.remote.model.UserDetails

data class Channel(
@SerializedName("_id")
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/io/codecrow/mage/remote/mapper/ChannelMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.codecrow.mage.remote.mapper

import android.util.Log
import com.google.gson.GsonBuilder
import io.codecrow.mage.remote.model.Channel
import io.codecrow.mage.remote.model.channel_response.ChannelResponseItem
import io.codecrow.mage.remote.utils.Mapper
import javax.inject.Inject

class ChannelMapper @Inject
constructor() :
Mapper<ChannelResponseItem, Channel> {

override suspend fun map(input: ChannelResponseItem): Channel {
return Channel(
id = input.id.orEmpty(),
title = input.title.orEmpty(),
avatar = input.userDetails?.avatar.orEmpty(),
createdByUsername = input.userDetails?.username.orEmpty(),
memberCount = input.memberCount.toString()
)
}

suspend fun mapAllChannels(botItems: ArrayList<ChannelResponseItem>): ArrayList<Channel> {
val allBots = ArrayList<Channel>()
Log.i("__TAG", GsonBuilder().setPrettyPrinting().create().toJson(botItems))
botItems.forEachIndexed { index, channelResponseItem ->
allBots.add(map(channelResponseItem))
}
return allBots
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/io/codecrow/mage/remote/model/Channel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.codecrow.mage.remote.model
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

data class Channel(
var id : String,
var title: String,
var createdByUsername: String,
var avatar: String,
var memberCount : String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codecrow.mage.model
package io.codecrow.mage.remote.model
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.codecrow.mage.remote.model.channel_response

import com.google.gson.annotations.SerializedName


data class ChannelResponseItem(

@field:SerializedName("mods")
val mods: List<Any?>? = null,

@field:SerializedName("thumbnail")
val thumbnail: String? = null,

@field:SerializedName("isAiChatEnabled")
val isAiChatEnabled: Boolean? = null,

@field:SerializedName("memberCount")
val memberCount: Int? = null,

@field:SerializedName("description")
val description: String? = null,

@field:SerializedName("channelType")
val channelType: String? = null,

@field:SerializedName("bans")
val bans: List<Any?>? = null,

@field:SerializedName("title")
val title: String? = null,

@field:SerializedName("userDetails")
val userDetails: UserDetails? = null,

@field:SerializedName("tags")
val tags: List<String?>? = null,

@field:SerializedName("createdAt")
val createdAt: Long? = null,

@field:SerializedName("isLive")
val isLive: Boolean? = null,

@field:SerializedName("guests")
val guests: List<Any?>? = null,

@field:SerializedName("_id")
val id: String? = null,

@field:SerializedName("category")
val category: List<String?>? = null,

@field:SerializedName("user")
val user: String? = null,

@field:SerializedName("updatedAt")
val updatedAt: Long? = null,

@field:SerializedName("planDetails")
val planDetails: PlanDetails? = null
)


data class PlanDetails(

@field:SerializedName("lastPaymentDate")
val lastPaymentDate: Long? = null,

@field:SerializedName("_id")
val id: String? = null,

@field:SerializedName("planTier")
val planTier: Int? = null,

@field:SerializedName("user")
val user: String? = null,

@field:SerializedName("createdt")
val createdt: Long? = null,

@field:SerializedName("updatedAt")
val updatedAt: Long? = null
)

data class UserDetails(

@field:SerializedName("displayName")
val displayName: String? = null,

@field:SerializedName("_id")
val id: String? = null,

@field:SerializedName("avatar")
val avatar: String? = null,

@field:SerializedName("username")
val username: String? = null
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codecrow.mage.model.response
package io.codecrow.mage.remote.model.response

data class ErrorResponse(
val message: String?
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/io/codecrow/mage/remote/utils/EntityMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.codecrow.mage.remote.utils

interface EntityMapper <Entity, DomainModel>{

fun mapFromEntity(entity: Entity): DomainModel

fun mapToEntity(domainModel: DomainModel): Entity
}


interface Mapper<T, U> {
suspend fun map(input: T): U
}
29 changes: 29 additions & 0 deletions app/src/main/java/io/codecrow/mage/remote/utils/Resource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.codecrow.mage.remote.utils

enum class DataStatus {
SUCCESS,
ERROR,
LOADING,
DEFAULT
}


data class Resource<out T>(val status: DataStatus, val data: T?, val message: String?, val error: Throwable? = null) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(DataStatus.SUCCESS, data, null)
}

fun <T> error(error: Throwable): Resource<T> {
return Resource(DataStatus.ERROR, null, null, error)
}

fun <T> loading(): Resource<T> {
return Resource(DataStatus.LOADING, null, null)
}

fun <T> default(): Resource<T> {
return Resource(DataStatus.DEFAULT, null, null)
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/io/codecrow/mage/ui/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ fun MainNavigation() {
composable("channel/{channelId}") { backStackEntry ->
val channelId = backStackEntry.arguments?.getString("channelId") ?: ""
val viewModel = hiltViewModel<ChannelViewModel>()
viewModel.setChannelId(channelId)
ChannelScreen(
navController = navController,
modifier = Modifier.padding(16.dp),
viewModel = viewModel
viewModel = viewModel,
channelID = channelId
)
}
}
Expand Down
Loading