-
Notifications
You must be signed in to change notification settings - Fork 31
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
강원대_Android_이아림_5주차과제_STEP2 #70
base: arieum
Are you sure you want to change the base?
Changes from 11 commits
adcaa98
346658d
d9f8467
505f0cf
006b02b
4ac89ac
2b6fb62
52094d6
ed6240b
389d07b
68407b6
360d5dd
6b84113
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package campus.tech.kakao.map.data.di | ||
|
||
import android.content.Context | ||
import campus.tech.kakao.map.data.db.AppDatabase | ||
import campus.tech.kakao.map.data.db.PlaceDao | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DatabaseModule { | ||
@Provides | ||
@Singleton | ||
fun provideDatabase(@ApplicationContext context: Context): AppDatabase { | ||
return AppDatabase.getDatabase(context) | ||
} | ||
|
||
@Provides | ||
fun providePlaceDao(database: AppDatabase): PlaceDao { | ||
return database.placeDao() | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package campus.tech.kakao.map.data.di | ||
|
||
import campus.tech.kakao.map.data.remote.api.KakaoApiService | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
arieum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private const val BASE_URL = "https://dapi.kakao.com/" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofit(): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideKakaoApiService(retrofit: Retrofit): KakaoApiService { | ||
return retrofit.create(KakaoApiService::class.java) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package campus.tech.kakao.map.data.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object PreferencesModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideSharedPreferences(@ApplicationContext context: Context): SharedPreferences { | ||
return context.getSharedPreferences("LastLocation", Context.MODE_PRIVATE) | ||
} | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 방법도 좋은데 SharedPreferences를 관리하는 객체를 따로 만들어도 좋을 것 같습니다. 예를들어 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package campus.tech.kakao.map.data.di | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import campus.tech.kakao.map.base.MyApplication | ||
import campus.tech.kakao.map.data.db.PlaceDao | ||
import campus.tech.kakao.map.data.remote.api.KakaoApiService | ||
import campus.tech.kakao.map.repository.LogRepository | ||
import campus.tech.kakao.map.repository.LogRepositoryInterface | ||
import campus.tech.kakao.map.repository.MapRepository | ||
import campus.tech.kakao.map.repository.MapRepositoryInterface | ||
import campus.tech.kakao.map.repository.PlaceRepository | ||
import campus.tech.kakao.map.repository.PlaceRepositoryInterface | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RepositoryModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideContext(application: Application): Context { | ||
return application.applicationContext | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hilt는 EntryPoint 등록을 통해서 각 Scope에 맞는 Context를 제공합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fun provideContext(@ApplicationContext context: Context): Context { return context } |
||
|
||
|
||
@Provides | ||
@Singleton | ||
fun providePlaceRepository(sharedPreferences: SharedPreferences, kakaoApiService: KakaoApiService): PlaceRepositoryInterface { | ||
return PlaceRepository(sharedPreferences, kakaoApiService) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideLogRepository(context: Context, placeDao: PlaceDao): LogRepositoryInterface { | ||
return LogRepository(context.applicationContext as MyApplication, placeDao) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideMapRepository(context: Context): MapRepositoryInterface { | ||
return MapRepository(context.applicationContext as MyApplication) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,10 @@ import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class LogRepository @Inject constructor (private val application: MyApplication, private val placeDao: PlaceDao): LogRepositoryInterface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 이해가 가지않습니다...! LogRepository는 MyApplication과 PlaceDao에 대해서 의존성 주입을 받고있고 RepositoryModule에서 provideLogRepository를 작성한 이유는 LogViewModel에 의존성을 주입하기 위함이었습니다! 어떤 걸 통일해야하는지 이해가 가지 않습니다... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Module을 통한 주입과 |
||
|
||
class LogRepository(private val application: MyApplication): LogRepositoryInterface { | ||
private val placeDao: PlaceDao = AppDatabase.getDatabase(application).placeDao() | ||
private var logList = mutableListOf<Place>() | ||
|
||
override fun getAllLogs(): List<Place> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
역할에 맞게 Module을 잘 구현하셨습니다! 👍
Hilt에 익숙해지시면 확실히 의존성 관리에 편하실거에요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
꺅>.<😆