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 #56

Open
wants to merge 3 commits into
base: ddangcong80
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# android-map-refactoring

- 데이터베이스를 Room으로 변경한다.
- 가능한 모든 부분에 대해서 의존성 주입을 적용한다
11 changes: 10 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-parcelize")
id("kotlin-kapt")
id("com.google.dagger.hilt.android")
}

android {
Expand Down Expand Up @@ -61,7 +63,14 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation ("androidx.test.espresso:espresso-intents:3.4.0")
androidTestImplementation("androidx.test.espresso:espresso-intents:3.4.0")
implementation("androidx.room:room-runtime:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")
implementation("androidx.room:room-ktx:2.4.2")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0")
implementation("com.google.dagger:hilt-android:2.48.1")
kapt("com.google.dagger:hilt-compiler:2.48.1")
implementation("androidx.activity:activity-ktx:1.2.3")
}

fun getApiKey(key: String): String = gradleLocalProperties(rootDir, providers).getProperty(key)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/AppModule.kt
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)

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하는 방법으로 한번 변경해보시기 바랍니다

}

@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/")

Choose a reason for hiding this comment

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

이러한 고정된 String들도 주입하는 방법으로 쓰면 유용한데요
(DB, BaseUrl등)
하나 이상의 같은 형태를 주입하기 위해서는 @qualifier 어노테이션을 사용해야합니다.

https://seosh817.tistory.com/76

위 블로그를 참고하여 String 상수들도 주입해보시기 바랍니다

.addConverterFactory(GsonConverterFactory.create())
.build()
.create(KakaoLocalService::class.java)
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/MyApplication.kt
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()
10 changes: 10 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/model/AppDatabase.kt
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
}

16 changes: 0 additions & 16 deletions app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt

This file was deleted.

103 changes: 0 additions & 103 deletions app/src/main/java/campus/tech/kakao/map/model/PlaceDBHelper.kt

This file was deleted.

10 changes: 8 additions & 2 deletions app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt
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
)

22 changes: 22 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/model/SavePlaceDao.kt
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())

Choose a reason for hiding this comment

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

"latitude" 이러한 값들은 상수로 사용하면 실수를 줄일 수 있습니다

Copy link
Author

Choose a reason for hiding this comment

The 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)
}
Loading