-
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주차 Step1 #56
base: ddangcong80
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
# android-map-refactoring | ||
|
||
- 데이터베이스를 Room으로 변경한다. | ||
- 가능한 모든 부분에 대해서 의존성 주입을 적용한다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.content.Context | ||
import campus.tech.kakao.map.model.KakaoLocalService | ||
import campus.tech.kakao.map.repository.MapRepository | ||
import campus.tech.kakao.map.repository.MapRepositoryImpl | ||
import campus.tech.kakao.map.repository.SearchRepository | ||
import campus.tech.kakao.map.repository.SearchRepositoryImpl | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideMapRepository( | ||
@ApplicationContext context: Context | ||
): MapRepository { | ||
return MapRepositoryImpl(context) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideSearchRepository( | ||
@ApplicationContext context: Context, | ||
retrofit: KakaoLocalService | ||
): SearchRepository { | ||
return SearchRepositoryImpl(context, retrofit) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideKakaoLocalService(): KakaoLocalService { | ||
return Retrofit.Builder() | ||
.baseUrl("https://dapi.kakao.com/") | ||
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. 이러한 고정된 String들도 주입하는 방법으로 쓰면 유용한데요 https://seosh817.tistory.com/76 위 블로그를 참고하여 String 상수들도 주입해보시기 바랍니다 |
||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
.create(KakaoLocalService::class.java) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.app.Application | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class MyApplication : Application() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package campus.tech.kakao.map.model | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
|
||
@Database(entities = [SavePlace::class], version = 1) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun savePlaceDao(): SavePlaceDao | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
package campus.tech.kakao.map.model | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "savePlace") | ||
data class SavePlace( | ||
val savePlace: String, | ||
) | ||
@PrimaryKey(autoGenerate = true) val id: Int = 0, | ||
val savePlaceName: String | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package campus.tech.kakao.map.model | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.Delete | ||
|
||
@Dao | ||
interface SavePlaceDao { | ||
@Insert | ||
suspend fun insert(savePlace: SavePlace) | ||
|
||
@Delete | ||
suspend fun delete(savePlace: SavePlace) | ||
|
||
@Query("SELECT * FROM savePlace") | ||
suspend fun getAll(): List<SavePlace> | ||
|
||
@Query("SELECT * FROM savePlace WHERE savePlaceName = :name LIMIT 1") | ||
suspend fun getByName(name: String): SavePlace? | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,6 @@ | ||
package campus.tech.kakao.map.repository | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
class MapRepository(context: Context) { | ||
private val spf: SharedPreferences = | ||
context.getSharedPreferences("map_prefs", Context.MODE_PRIVATE) | ||
|
||
fun saveLastPosition(latitude: Double, longitude: Double) { | ||
val editor = spf.edit() | ||
editor.putString("latitude", latitude.toString()) | ||
editor.putString("longitude", longitude.toString()) | ||
editor.apply() | ||
} | ||
|
||
fun getLastPosition(): Pair<Double, Double> { | ||
val latitude = spf.getString("latitude", "37.394660")?.toDouble() ?: 37.394660 | ||
val longitude = spf.getString("longitude", "127.111182")?.toDouble() ?: 127.111182 | ||
return Pair(latitude, longitude) | ||
} | ||
} | ||
interface MapRepository { | ||
fun saveLastPosition(latitude: Double, longitude: Double) | ||
fun getLastPosition(): Pair<Double, Double> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package campus.tech.kakao.map.repository | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class MapRepositoryImpl @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
) : MapRepository { | ||
private val spf: SharedPreferences = | ||
context.getSharedPreferences("map_prefs", Context.MODE_PRIVATE) | ||
|
||
override fun saveLastPosition(latitude: Double, longitude: Double) { | ||
val editor = spf.edit() | ||
editor.putString("latitude", latitude.toString()) | ||
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. "latitude" 이러한 값들은 상수로 사용하면 실수를 줄일 수 있습니다 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. Step2 과제에서 피드백 수정 코드 올렸습니다! |
||
editor.putString("longitude", longitude.toString()) | ||
editor.apply() | ||
} | ||
|
||
override fun getLastPosition(): Pair<Double, Double> { | ||
val latitude = spf.getString("latitude", "37.394660")?.toDouble() ?: 37.394660 | ||
val longitude = spf.getString("longitude", "127.111182")?.toDouble() ?: 127.111182 | ||
return Pair(latitude, longitude) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,11 @@ | ||
package campus.tech.kakao.map.repository | ||
|
||
import android.content.Context | ||
import campus.tech.kakao.map.model.PlaceDBHelper | ||
import campus.tech.kakao.map.model.PlaceInfo | ||
import campus.tech.kakao.map.model.SavePlace | ||
|
||
|
||
class SearchRepository(context: Context) { | ||
private val dbHelper = PlaceDBHelper(context) | ||
|
||
private fun savePlaces(placeName: String) { | ||
dbHelper.savePlaces(placeName) | ||
} | ||
|
||
fun showSavePlace(): MutableList<SavePlace> { | ||
return dbHelper.showSavePlace() | ||
} | ||
|
||
private fun deleteSavedPlace(savedPlaceName: String) { | ||
dbHelper.deleteSavedPlace(savedPlaceName) | ||
} | ||
|
||
fun savePlacesAndUpdate(placeName: String): MutableList<SavePlace> { | ||
savePlaces(placeName) | ||
return showSavePlace() | ||
} | ||
|
||
fun deleteSavedPlacesAndUpdate(savedPlaceName: String): MutableList<SavePlace> { | ||
deleteSavedPlace(savedPlaceName) | ||
return showSavePlace() | ||
} | ||
interface SearchRepository { | ||
suspend fun savePlaces(placeName: String): List<SavePlace> | ||
suspend fun showSavePlace(): List<SavePlace> | ||
suspend fun deleteSavedPlace(savedPlaceName: String): List<SavePlace> | ||
fun getPlaceList(categoryGroupName: String, callback: (List<PlaceInfo>?) -> Unit) | ||
} |
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.
이렇게 Repo자체를 생성해도 가능하지만 이렇게 되면 Repo에서 굳이 inject를 해줄 필요가 없어집니다.
또한 완벽한 클린아키텍쳐 기반의 멀티모듈을 적용했을때 Repo의 구현체를 알고있어야하는 문제가 생기게 되는데요
https://hungseong.tistory.com/29
위의 블로그를 참고해서 Repo interface, RepoImpl을 생성하고 bind하는 방법으로 한번 변경해보시기 바랍니다