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

전남대 Android_장수민_5주차 과제(Step1) #54

Open
wants to merge 5 commits into
base: sumintnals
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("org.jlleitschuh.gradle.ktlint")
id("kotlin-parcelize")
id("kotlin-kapt")
id("com.google.dagger.hilt.android")
id("dagger.hilt.android.plugin")
}

android {
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/campus/tech/kakao/map/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import com.kakao.vectormap.label.Label
import com.kakao.vectormap.label.LabelOptions
import com.kakao.vectormap.label.LabelStyle
import com.kakao.vectormap.label.LabelStyles
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.Address
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var mainViewBinding: ActivityMainBinding
private lateinit var mapView: MapView
private val mainViewModel: MainViewModel by viewModels {
ViewModelFactory(applicationContext, MapApplication.prefs)
}
@Inject lateinit var preferenceManager: PreferenceManager

private val mainViewModel: MainViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/campus/tech/kakao/map/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package campus.tech.kakao.map

import android.content.Context
import androidx.lifecycle.ViewModel
import com.kakao.vectormap.LatLng
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

class MainViewModel(
private val preferenceManager: PreferenceManager
@HiltViewModel
class MainViewModel @Inject constructor(
private val context: Context,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context를 사용하는 부분이 없어 보이는데, 제거해도 괜찮지 않을까요?

private val preferenceManager: PreferenceManager,
) : ViewModel() {

fun setLocation(latitude: Double? = null, longitude: Double? = null): LatLng? {
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/MapAppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package campus.tech.kakao.map

import android.app.Application
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
@Singleton
fun providePreferenceManager(context: Context): PreferenceManager {
return PreferenceManager(context)
}

@Provides
@Singleton
fun provideContext(application: Application): Context {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; 의존성 주입을 받는쪽에서 ApplicationContext 어노테이션을 사용하면, 이런 Provide 함수를 제거 할 수 있습니다.

return application.applicationContext
}
}
10 changes: 6 additions & 4 deletions app/src/main/java/campus/tech/kakao/map/MapApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import android.app.Application
import android.content.SharedPreferences
import android.util.Log
import com.kakao.vectormap.KakaoMapSdk
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MapApplication: Application() {
companion object {
lateinit var prefs: PreferenceManager
}
// companion object {
// lateinit var prefs: PreferenceManager
// }

override fun onCreate() {
val apiKey = getString(R.string.kakao_api_key)
prefs = PreferenceManager(applicationContext)
// prefs = PreferenceManager(applicationContext)
super.onCreate()
KakaoMapSdk.init(this, apiKey)
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package campus.tech.kakao.map

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 RepositoryModule {

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

@Provides
@Singleton
fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://dapi.kakao.com/v2/local/search/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}

@Provides
@Singleton
fun provideRetrofitService(retrofit: Retrofit): RetrofitService {
return retrofit.create(RetrofitService::class.java)
}

@Provides
@Singleton
fun provideRetrofitRepository(retrofitService: RetrofitService): RetrofitRepository {
return RetrofitRepository(retrofitService)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import campus.tech.kakao.map.RetrofitInstance.retrofitService
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject

open class RetrofitRepository @Inject constructor(
private val retrofitService: RetrofitService
) {

open class RetrofitRepository {
open suspend fun getPlace(query: String): List<Document> {
if (query.isEmpty()) {
return emptyList()
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/campus/tech/kakao/map/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import campus.tech.kakao.map.databinding.ActivitySearchBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class SearchActivity : AppCompatActivity() {
private val viewModel: SearchViewModel by viewModels {
ViewModelFactory(applicationContext, MapApplication.prefs)
}
@Inject lateinit var preferenceManager: PreferenceManager
private val viewModel: SearchViewModel by viewModels()

private val placeAdapter: PlaceAdapter by lazy {
PlaceAdapter(
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/campus/tech/kakao/map/SearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SearchViewModel(context: Context) : ViewModel() {
import javax.inject.Inject

@HiltViewModel
class SearchViewModel @Inject constructor(
context: Context,
private val preferenceManager: PreferenceManager,
var repository: RetrofitRepository
) : ViewModel() {
private val dbHelper: DBHelper = DBHelper(context)
private val db = dbHelper.writableDatabase
private val preferenceManager = MapApplication.prefs
var repository = RetrofitRepository()

private var _placeList = MutableLiveData<List<Place>>()
private val _searchHistoryList = MutableLiveData<List<SearchHistory>>()
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/campus/tech/kakao/map/ViewModelFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class ViewModelFactory(private val context: Context
,private val preferenceManager: PreferenceManager)
,private val preferenceManager: PreferenceManager
, private val repository: RetrofitRepository
)
: ViewModelProvider.Factory {

override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SearchViewModel::class.java)) {
return SearchViewModel(context) as T
return SearchViewModel(context, preferenceManager, repository) as T
}
else if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
return MainViewModel(preferenceManager) as T
return MainViewModel(context, preferenceManager) as T
}
else {
throw IllegalArgumentException("Failed to create ViewModel : ${modelClass.name}")
Expand Down