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 all 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으로 변경한다.
1 change: 0 additions & 1 deletion app/src/main/java/campus/tech/kakao/MyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package campus.tech.kakao

import android.app.Application
import com.kakao.vectormap.KakaoMapSdk
import campus.tech.kakao.map.BuildConfig
import campus.tech.kakao.map.R
import dagger.hilt.android.HiltAndroidApp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.R
import campus.tech.kakao.map.viewmodel.OnKeywordItemClickListener
import campus.tech.kakao.map.view.OnKeywordItemClickListener

class KeywordAdapter(private val onKeywordItemClickListener: OnKeywordItemClickListener) :
ListAdapter<String, KeywordAdapter.KeywordViewHolder>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.R
import campus.tech.kakao.map.model.Item
import campus.tech.kakao.map.viewmodel.OnSearchItemClickListener
import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.view.OnSearchItemClickListener

class SearchAdapter(
private val onSearchItemClickListener: OnSearchItemClickListener
) : ListAdapter<Item, SearchAdapter.SearchViewHolder>(
object : DiffUtil.ItemCallback<Item>() {
override fun areItemsTheSame(oldItem: Item, newItem: Item) =
) : ListAdapter<Location, SearchAdapter.SearchViewHolder>(
object : DiffUtil.ItemCallback<Location>() {
override fun areItemsTheSame(oldItem: Location, newItem: Location) =
oldItem.place == newItem.place

override fun areContentsTheSame(oldItem: Item, newItem: Item) =
override fun areContentsTheSame(oldItem: Location, newItem: Location) =
oldItem == newItem
}
) {
Expand All @@ -39,13 +39,13 @@ class SearchAdapter(
private val address: TextView = view.findViewById(R.id.address)
private val category: TextView = view.findViewById(R.id.category)

fun bindViewHolder(item: Item, onSearchItemClickListener: OnSearchItemClickListener) {
place.text = item.place
address.text = item.address
category.text = item.category
fun bindViewHolder(location: Location, onSearchItemClickListener: OnSearchItemClickListener) {
place.text = location.place
address.text = location.address
category.text = location.category

itemList.setOnClickListener {
onSearchItemClickListener.onSearchItemClick(item)
onSearchItemClickListener.onSearchItemClick(location)
}
}
}
Expand Down
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
)
)
12 changes: 6 additions & 6 deletions app/src/main/java/campus/tech/kakao/map/database/AppDatabase.kt
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
import campus.tech.kakao.map.repository.location.LocationDao

@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
}
abstract fun locationDao(): LocationDao
}
18 changes: 1 addition & 17 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.LocationDao
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): LocationDao {
return database.locationDao()
}

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

@Provides
@Singleton
fun provideLocationSearcher(locationDao: LocationDao): LocationSearcher {
return LocationSearcher(locationDao)
}
}
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
)
14 changes: 0 additions & 14 deletions app/src/main/java/campus/tech/kakao/map/model/Item.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/campus/tech/kakao/map/model/Keyword.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import androidx.room.PrimaryKey
data class Keyword(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
val recentKeyword: String
)
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package campus.tech.kakao.map.model

data class Item(
data class Location(
val place: String,
val address: String,
val category: String,
val latitude: Double,
val longitude:Double
)
val longitude: Double
)

This file was deleted.

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

This file was deleted.

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