Skip to content

Commit

Permalink
feat: modify about Hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
kimseongyu committed Jul 29, 2024
1 parent 78e0aa0 commit 7c667f2
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 67 deletions.
6 changes: 0 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
android:supportsRtl="true"
android:theme="@style/Theme.Map"
tools:targetApi="31">
<activity
android:name=".view.kakaomap.KakaoMapActivity"
android:exported="false" />
<activity
android:name=".view.search.SearchWindowActivity"
android:exported="false" />
<activity
android:name=".view.kakaomap.KakaoMapActivity"
android:exported="false" />
Expand Down
25 changes: 9 additions & 16 deletions app/src/main/java/campus/tech/kakao/map/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package campus.tech.kakao.map

import android.content.Context
import android.content.SharedPreferences
import campus.tech.kakao.map.model.search.SearchKeywordDao
import campus.tech.kakao.map.repository.kakaomap.LastPositionRepository
import campus.tech.kakao.map.repository.search.KakaoSearchKeywordAPI
import campus.tech.kakao.map.repository.search.SavedSearchKeywordRepository
import campus.tech.kakao.map.repository.search.SearchRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Qualifier
import javax.inject.Singleton

@Module
Expand All @@ -18,25 +20,16 @@ object AppModule {

@Provides
@Singleton
fun provideContext(@ApplicationContext context: Context): Context {
return context
}
fun provideSavedSearchKeywordRepository(searchKeywordDao: SearchKeywordDao) =
SavedSearchKeywordRepository(searchKeywordDao)

@Provides
@Singleton
fun provideSavedSearchKeywordRepository(@ApplicationContext context: Context): SavedSearchKeywordRepository {
return SavedSearchKeywordRepository(context)
}
fun provideSearchRepository(retrofitKakaoSearchKeyword: KakaoSearchKeywordAPI): SearchRepository =
SearchRepository(retrofitKakaoSearchKeyword)

@Provides
@Singleton
fun provideSearchRepository(): SearchRepository {
return SearchRepository()
}

@Provides
@Singleton
fun provideLastPositionRepository(@ApplicationContext context: Context): LastPositionRepository {
return LastPositionRepository(context)
}
fun provideLastPositionRepository(sharedPreferences: SharedPreferences) =
LastPositionRepository(sharedPreferences)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package campus.tech.kakao.map.repository.kakaomap

import android.content.Context
import android.content.SharedPreferences
import campus.tech.kakao.map.view.ActivityKeys
import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.kakao.vectormap.LatLng
import javax.inject.Inject

class LastPositionRepository @Inject constructor(context: Context) {
private val sharedPreferences = context.getSharedPreferences(
ActivityKeys.PREFS,
Context.MODE_PRIVATE
)
class LastPositionRepository @Inject constructor(private val sharedPreferences: SharedPreferences) {

fun saveLastPosition(position: LatLng) {
val editor = sharedPreferences.edit()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package campus.tech.kakao.map.repository.kakaomap

import android.content.Context
import campus.tech.kakao.map.view.ActivityKeys
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 SharedPreferencesModule {

@Provides
@Singleton
fun provideSharedPreferences(@ApplicationContext context: Context) =
context.getSharedPreferences(
ActivityKeys.PREFS,
Context.MODE_PRIVATE
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package campus.tech.kakao.map.repository.search

import android.content.Context
import androidx.room.Room
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 DBModule {
@Provides
@Singleton
fun provideDB(@ApplicationContext context: Context) =
Room.databaseBuilder(
context,
SearchKeywordDB::class.java,
"searchKeyword"
).build()

@Provides
fun provideDao(db: SearchKeywordDB) =
db.searchKeywordDao()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package campus.tech.kakao.map.repository.search

import campus.tech.kakao.map.BuildConfig
import campus.tech.kakao.map.model.search.SearchResults
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query

object KakaoAPISetting {
const val BASE_URL = "https://dapi.kakao.com"
const val API_KEY = "KakaoAK ${BuildConfig.KAKAO_REST_API_KEY}"
}

interface KakaoSearchKeywordAPI {
@GET("/v2/local/search/keyword.json")
suspend fun getSearchKeyWord(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package campus.tech.kakao.map.repository.search

import campus.tech.kakao.map.repository.search.KakaoAPISetting.BASE_URL
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 RetrofitModule {

@Provides
@Singleton
fun provideRetrofitKakaoSearchAPI() =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(KakaoSearchKeywordAPI::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package campus.tech.kakao.map.repository.search

import android.content.Context
import campus.tech.kakao.map.model.search.SearchKeyword
import campus.tech.kakao.map.model.search.SearchKeywordDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import javax.inject.Inject

class SavedSearchKeywordRepository @Inject constructor(context: Context) {

private val db = SearchKeywordDB.getInstace(context)!!.searchKeywordDao()
class SavedSearchKeywordRepository @Inject constructor(private val db: SearchKeywordDao) {

suspend fun saveSearchKeyword(searchKeyword: SearchKeyword) {
withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,4 @@ import kotlinx.coroutines.coroutineScope
@Database(entities = [SearchKeyword::class], version = 1)
abstract class SearchKeywordDB : RoomDatabase() {
abstract fun searchKeywordDao(): SearchKeywordDao

companion object {
private var INSTANCE: SearchKeywordDB? = null
fun getInstace(context: Context): SearchKeywordDB? {
return INSTANCE ?: synchronized(this) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
SearchKeywordDB::class.java, "searchKeyword"
)
.build()
INSTANCE
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
package campus.tech.kakao.map.repository.search

import campus.tech.kakao.map.BuildConfig
import campus.tech.kakao.map.model.search.Place
import campus.tech.kakao.map.model.search.SearchKeyword
import kotlinx.coroutines.coroutineScope
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import campus.tech.kakao.map.repository.search.KakaoAPISetting.API_KEY
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

class SearchRepository @Inject constructor() {
companion object {
const val BASE_URL = "https://dapi.kakao.com"
const val API_KEY = "KakaoAK ${BuildConfig.KAKAO_REST_API_KEY}"
}

private val retrofitKakaoSearchKeyword = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(KakaoSearchKeywordAPI::class.java)
class SearchRepository @Inject constructor(private val retrofitKakaoSearchKeyword: KakaoSearchKeywordAPI) {

suspend fun Search(searchKeyword: SearchKeyword): List<Place> {
suspend fun search(searchKeyword: SearchKeyword): List<Place> {
if (searchKeyword.searchKeyword == "")
return emptyList()

return coroutineScope {
return withContext(Dispatchers.IO) {
retrofitKakaoSearchKeyword
.getSearchKeyWord(API_KEY, searchKeyword.searchKeyword)
.places
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SearchViewModel @Inject constructor(

fun getSearchResults(searchKeyword: SearchKeyword) {
viewModelScope.launch {
_searchResults.value = searchRepository.Search(searchKeyword)
_searchResults.value = searchRepository.search(searchKeyword)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import campus.tech.kakao.map.model.search.SearchKeyword
import campus.tech.kakao.map.repository.search.SavedSearchKeywordRepository
import campus.tech.kakao.map.repository.search.SearchRepository
import campus.tech.kakao.map.viewmodel.search.SearchViewModel
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.junit4.MockKRule
import io.mockk.just
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.withContext
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -57,14 +54,14 @@ class SearchViewModelTest {
TEST_PLACE_Y
)
val searchResults = listOf(place)
coEvery { searchRepository.Search(searchKeyword) } returns searchResults
coEvery { searchRepository.search(searchKeyword) } returns searchResults

// when
searchViewModel.getSearchResults(searchKeyword)

// then
assert(searchViewModel.searchResults.value == searchResults)
coVerify { searchRepository.Search(searchKeyword) }
coVerify { searchRepository.search(searchKeyword) }

}

Expand Down

0 comments on commit 7c667f2

Please sign in to comment.