Skip to content

Commit

Permalink
refactor: 리뷰사항에 따른 변경 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
minhyukseul committed Aug 29, 2023
1 parent 65b0bee commit 2a48792
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import camp.nextstep.edu.github.ui.UiStatus
import camp.nextstep.edu.github.ui.main.GithubMainSideEffect
import camp.nextstep.edu.github.ui.main.GithubMainState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
Expand All @@ -30,17 +29,15 @@ class GithubRepositoriesViewModel @Inject constructor(
reduce { state.copy(status = UiStatus.Loading) }
viewModelScope.launch {
runCatching {
async {
val repositories = dataSource.fetchRepositories()
reduce {
state.copy(
status = UiStatus.Success,
repositories = repositories
)
}
val repositories = dataSource.fetchRepositories()
reduce {
state.copy(
status = UiStatus.Success,
repositories = repositories
)
}
}.getOrElse {
reduce { state.copy(status = UiStatus.Failed("로딩 실패")) }
reduce { state.copy(status = UiStatus.Failed("Error : ${it.message}")) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
package camp.nextstep.edu.github.data.network.di

import camp.nextstep.edu.github.data.network.GithubRepositoriesDataSource
import camp.nextstep.edu.github.data.network.GithubService
import camp.nextstep.edu.github.domain.network.GithubDataSource
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

@InstallIn(SingletonComponent::class)
@Module
abstract class DataModule {

@Binds
internal abstract fun bindDataSource(dataSource: GithubRepositoriesDataSource): GithubDataSource

companion object {
private const val BASE_URL = "https://api.github.com/"

@Provides
fun provideClient(): OkHttpClient {
return OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS)
.build()
}

@Provides
fun provideGithubService(client: OkHttpClient): GithubService {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GithubService::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package camp.nextstep.edu.github.data.network.di

import camp.nextstep.edu.github.data.network.GithubService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

@InstallIn(SingletonComponent::class)
@Module
abstract class NetworkModule {

companion object {
private const val BASE_URL = "https://api.github.com/"

@Provides
fun provideClient(): OkHttpClient {
return OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS)
.build()
}

@Provides
fun provideGithubService(retrofit: Retrofit): GithubService {
return retrofit.create(GithubService::class.java)
}

@Provides
fun provideRetrofit(client: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
}

0 comments on commit 2a48792

Please sign in to comment.