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주차 과제 Step2 #65

Open
wants to merge 28 commits into
base: njiyeon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f646b6
update README.md
nJiyeon Jul 22, 2024
c11466c
android-map-location의 코드 가져오기
nJiyeon Jul 22, 2024
4668c04
테스트코드 가져오기
nJiyeon Jul 23, 2024
aa5836c
KAKAO_API_KEY를 BuildConfig를 통해 가져오도록 변경
nJiyeon Jul 24, 2024
af98667
update README.md
nJiyeon Jul 24, 2024
331daeb
의존성 주입을 위한 AppModule 생성
nJiyeon Jul 24, 2024
5558289
update Viewmodel - 의존성 주입 & Factory 파일 삭제
nJiyeon Jul 24, 2024
ef9d540
update MainActivity & SearchActivity
nJiyeon Jul 24, 2024
c7707ac
update repository
nJiyeon Jul 24, 2024
780930e
update api & model package
nJiyeon Jul 24, 2024
bb11170
update MyApplication
nJiyeon Jul 24, 2024
981ac9b
update Step1
nJiyeon Jul 25, 2024
c432f98
MainActivity에 dataBinding 추가하기
nJiyeon Jul 26, 2024
0206173
update README.md
nJiyeon Jul 26, 2024
ee465f3
AppDatabase를 Singleton으로 Hilt을 통해 주입하기
nJiyeon Jul 28, 2024
d2fbc7c
Merge branch 'njiyeon' into feat-njiyeonStep2
nJiyeon Jul 28, 2024
a06df9f
Merge branch 'njiyeon' into feat-njiyeonStep2
nJiyeon Jul 28, 2024
12cb523
Delete app/src/main/java/campus/tech/kakao/map/model/item.kt
nJiyeon Jul 29, 2024
2c0fa80
Delete app/src/main/java/campus/tech/kakao/map/repository/keyword/Key…
nJiyeon Jul 29, 2024
694c187
Delete app/src/main/java/campus/tech/kakao/map/repository/keyword/Key…
nJiyeon Jul 29, 2024
bbdcde3
Delete app/src/main/java/campus/tech/kakao/map/repository/location/Lo…
nJiyeon Jul 29, 2024
3b17d38
OnKeywordItemClickListener, OnSearchItemClickListener를 view로 옮기기
nJiyeon Jul 29, 2024
ea80f63
Rebase 오류 해결
nJiyeon Jul 29, 2024
8eb54f6
by viewmodel을 활용하여 viewmodel을 lazy하게 받기
nJiyeon Jul 29, 2024
2b38b12
update DatabaseModule
nJiyeon Jul 29, 2024
76fc3d1
itemDao를 locationDao로 바꾸기
nJiyeon Jul 29, 2024
7ce9a82
delete LocationContract
nJiyeon Jul 29, 2024
874ee98
item.kt를 location.kt로 변경
nJiyeon Jul 29, 2024
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# android-map-refactoring
## 기능 요구 사항
- 데이터베이스를 Room으로 변경한다.
- 가능한 모든 부분에 대해서 의존성 주입을 적용한다.
## 프로그래밍 요구 사항
- 의존성 주입을 위해서 Hilt를 사용한다.
- 코드 컨벤션을 준수하며 프로그래밍한다.
- MVVM 아키텍처 패턴을 적용한다.
- DataBinding, LiveData를 사용한다.
- 비동기 처리를 Coroutine으로 변경한다.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ data class Meta(

@SerializedName("is_end")
val isEnd: Boolean
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package campus.tech.kakao.map.database

import androidx.room.Database
import androidx.room.RoomDatabase
import campus.tech.kakao.map.model.Keyword
import campus.tech.kakao.map.model.Item
import campus.tech.kakao.map.entity.KeywordEntity
import campus.tech.kakao.map.entity.LocationEntity
import campus.tech.kakao.map.repository.keyword.KeywordDao
import campus.tech.kakao.map.repository.location.ItemDao

@Database(entities = [Keyword::class, Item::class], version = 1)
@Database(entities = [KeywordEntity::class, LocationEntity::class], version = 2)
Copy link

Choose a reason for hiding this comment

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

Item -> LocationEntity로 변경하셨으니 ItemDao도 그에 맞게 변경하면 좋겠네요

abstract class AppDatabase : RoomDatabase() {
abstract fun keywordDao(): KeywordDao
abstract fun itemDao(): ItemDao
}
}
20 changes: 2 additions & 18 deletions app/src/main/java/campus/tech/kakao/map/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package campus.tech.kakao.di
package campus.tech.kakao.map.di

import android.content.Context
import campus.tech.kakao.map.api.KakaoLocalApi
import campus.tech.kakao.map.repository.keyword.KeywordRepository
import campus.tech.kakao.map.repository.location.LocationSearcher
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
Expand All @@ -31,16 +27,4 @@ object AppModule {
fun provideKakaoLocalApi(retrofit: Retrofit): KakaoLocalApi {
return retrofit.create(KakaoLocalApi::class.java)
}

@Provides
@Singleton
fun provideKeywordRepository(@ApplicationContext context: Context): KeywordRepository {
return KeywordRepository(context)
}

@Provides
@Singleton
fun provideLocationSearcher(@ApplicationContext context: Context): LocationSearcher {
return LocationSearcher(context)
}
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package campus.tech.kakao.map.di

import android.content.Context
import androidx.room.Room
import campus.tech.kakao.map.database.AppDatabase
import campus.tech.kakao.map.repository.keyword.KeywordDao
import campus.tech.kakao.map.repository.location.ItemDao
import campus.tech.kakao.map.repository.keyword.KeywordRepository
import campus.tech.kakao.map.repository.location.LocationSearcher
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 Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_database"
)
.fallbackToDestructiveMigration() //필요시 마이그레이션 재생성
.build()
}

@Provides
@Singleton
fun provideKeywordDao(database: AppDatabase): KeywordDao {
return database.keywordDao()
}

@Provides
@Singleton
fun provideItemDao(database: AppDatabase): ItemDao {
return database.itemDao()
}

@Provides
@Singleton
fun provideKeywordRepository(keywordDao: KeywordDao): KeywordRepository {
return KeywordRepository(keywordDao)
}

@Provides
@Singleton
fun provideLocationSearcher(itemDao: ItemDao): LocationSearcher {
return LocationSearcher(itemDao)
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/entity/KeywordEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package campus.tech.kakao.map.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "keywords")
data class KeywordEntity(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "keyword") val keyword: String
)
15 changes: 15 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/entity/LocationEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package campus.tech.kakao.map.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "location")
data class LocationEntity(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "place_name") val placeName: String,
@ColumnInfo(name = "address_name") val addressName: String,
@ColumnInfo(name = "category_group_name") val categoryGroupName: String,
@ColumnInfo(name = "latitude") val latitude: Double,
@ColumnInfo(name = "longitude") val longitude: Double
)
5 changes: 0 additions & 5 deletions app/src/main/java/campus/tech/kakao/map/model/Item.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package campus.tech.kakao.map.model

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "location")
data class Item(
@PrimaryKey(autoGenerate = true) val id: Int = 0, // 기본값 추가
val place: String,
val address: String,
val category: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package campus.tech.kakao.map.repository.keyword

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Delete
import campus.tech.kakao.map.model.Keyword
import campus.tech.kakao.map.entity.KeywordEntity

@Dao
interface KeywordDao {
@Insert
suspend fun insert(keyword: Keyword)
@Query("SELECT * FROM keywords")
suspend fun getAllKeywords(): List<KeywordEntity>

@Query("SELECT * FROM keyword")
suspend fun getAllKeywords(): List<Keyword>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertKeyword(keyword: KeywordEntity)

@Delete
suspend fun delete(keyword: Keyword)
suspend fun deleteKeyword(keyword: KeywordEntity)
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package campus.tech.kakao.map.repository.keyword

import android.content.Context
import androidx.room.Room
import campus.tech.kakao.map.database.AppDatabase
import campus.tech.kakao.map.model.Keyword
import campus.tech.kakao.map.entity.KeywordEntity
import javax.inject.Inject
import javax.inject.Singleton

class KeywordRepository(context: Context) {
private val db = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java, "app-database"
).build()
private val keywordDao = db.keywordDao()
@Singleton
class KeywordRepository @Inject constructor(private val keywordDao: KeywordDao) {
Comment on lines +7 to +8
Copy link

Choose a reason for hiding this comment

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

DatabaseModule에서도 KeywordRepsoitroy 의존성을 생성하고 여기서도 생성하시는 이유가 있을까요?
특별한 이유가 없으면 하나의 방법으로 통일시키는게 좋겠습니다.

Copy link
Author

Choose a reason for hiding this comment

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

DatabaseModule에서만 생성하도록 수정하였습니다 !


suspend fun update(keyword: String) {
keywordDao.insert(Keyword(recentKeyword = keyword))
suspend fun read(): List<String> {
return keywordDao.getAllKeywords().map { it.keyword }
}

suspend fun read(): List<String> {
return keywordDao.getAllKeywords().map { it.recentKeyword }
suspend fun update(keyword: String) {
keywordDao.insertKeyword(KeywordEntity(keyword = keyword))
}

suspend fun delete(keyword: String) {
val keywordEntity = keywordDao.getAllKeywords().find { it.recentKeyword == keyword }
keywordEntity?.let { keywordDao.delete(it) }
keywordDao.deleteKeyword(KeywordEntity(keyword = keyword))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package campus.tech.kakao.map.repository.location

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Delete
import campus.tech.kakao.map.model.Item
import campus.tech.kakao.map.entity.LocationEntity

@Dao
interface ItemDao {
@Query("SELECT * FROM location WHERE category LIKE :keyword")
suspend fun search(keyword: String): List<Item>

@Insert
suspend fun insert(item: Item)

@Delete
suspend fun delete(item: Item)
@Query("SELECT * FROM location WHERE category_group_name = :category")
suspend fun searchByCategory(category: String): List<LocationEntity>
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package campus.tech.kakao.map.repository.location

import android.content.Context
import androidx.room.Room
import campus.tech.kakao.map.database.AppDatabase
import campus.tech.kakao.map.model.Item
import javax.inject.Inject
import javax.inject.Singleton

class LocationSearcher(context: Context) {
private val db = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java, "app-database"
).build()
private val itemDao = db.itemDao()
@Singleton
class LocationSearcher @Inject constructor(private val itemDao: ItemDao) {

suspend fun search(keyword: String): List<Item> {
return itemDao.search("%$keyword%")

val locationEntities = itemDao.searchByCategory(keyword)
return locationEntities.map {
Item(
place = it.placeName,
address = it.addressName,
category = it.categoryGroupName,
latitude = it.latitude,
longitude = it.longitude
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@ class MainActivity : AppCompatActivity(), OnSearchItemClickListener, OnKeywordIt
private const val DEFAULT_ZOOM_LEVEL = 1
private const val CAMERA_ANIMATION_DURATION = 10
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import javax.inject.Inject
@AndroidEntryPoint
class SearchActivity : AppCompatActivity(), OnSearchItemClickListener, OnKeywordItemClickListener {
private lateinit var binding: ActivitySearchBinding

@Inject
lateinit var api: KakaoLocalApi
Copy link

Choose a reason for hiding this comment

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

KakaoLocalApi를 주입받는 이유가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

Viewmodel에서 처리를 하고 있으므로 따로 주입받을 필요가 없습니다 ! 해당 부분을 삭제하도록 하겠습니다 !


private lateinit var searchViewModel: SearchViewModel
private lateinit var keywordViewModel: KeywordViewModel
Copy link

Choose a reason for hiding this comment

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

ViewModel를 by viewModels 를 통해 ViewModel을 LAZY 하게 providing 받을 수 있는 기능을 제공합니다. 한 번 적용해보세요!

private lateinit var searchAdapter: SearchAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import campus.tech.kakao.map.model.Item

interface OnSearchItemClickListener {
fun onSearchItemClick(item: Item)
}
}
Copy link

Choose a reason for hiding this comment

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

ViewModel에 리스너가 있는건 어색하네요. View로 옮겨주세요

Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ class MainViewModel @Inject constructor(
private const val PREF_PLACE_NAME = "lastPlaceName"
private const val PREF_ROAD_ADDRESS_NAME = "lastRoadAddressName"
}
}
}
Loading