-
Notifications
You must be signed in to change notification settings - Fork 32
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
base: njiyeon
Are you sure you want to change the base?
Changes from 17 commits
5f646b6
c11466c
4668c04
aa5836c
af98667
331daeb
5558289
ef9d540
c7707ac
780930e
bb11170
981ac9b
c432f98
0206173
ee465f3
d2fbc7c
a06df9f
12cb523
2c0fa80
694c187
bbdcde3
3b17d38
ea80f63
8eb54f6
2b38b12
76fc3d1
7ce9a82
874ee98
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,7 +1,5 @@ | ||
# android-map-refactoring | ||
## 기능 요구 사항 | ||
- 데이터베이스를 Room으로 변경한다. | ||
- 가능한 모든 부분에 대해서 의존성 주입을 적용한다. | ||
## 프로그래밍 요구 사항 | ||
- 의존성 주입을 위해서 Hilt를 사용한다. | ||
- 코드 컨벤션을 준수하며 프로그래밍한다. | ||
- MVVM 아키텍처 패턴을 적용한다. | ||
- DataBinding, LiveData를 사용한다. | ||
- 비동기 처리를 Coroutine으로 변경한다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,4 @@ data class Meta( | |
|
||
@SerializedName("is_end") | ||
val isEnd: Boolean | ||
) | ||
) |
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) | ||
} | ||
} |
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 | ||
) |
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 | ||
) |
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
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. DatabaseModule에서도 KeywordRepsoitroy 의존성을 생성하고 여기서도 생성하시는 이유가 있을까요? 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.
|
||
|
||
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 |
---|---|---|
|
@@ -24,6 +24,10 @@ import javax.inject.Inject | |
@AndroidEntryPoint | ||
class SearchActivity : AppCompatActivity(), OnSearchItemClickListener, OnKeywordItemClickListener { | ||
private lateinit var binding: ActivitySearchBinding | ||
|
||
@Inject | ||
lateinit var api: KakaoLocalApi | ||
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. KakaoLocalApi를 주입받는 이유가 있을까요? 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.
|
||
|
||
private lateinit var searchViewModel: SearchViewModel | ||
private lateinit var keywordViewModel: KeywordViewModel | ||
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. ViewModel를 by viewModels 를 통해 ViewModel을 LAZY 하게 providing 받을 수 있는 기능을 제공합니다. 한 번 적용해보세요! |
||
private lateinit var searchAdapter: SearchAdapter | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ import campus.tech.kakao.map.model.Item | |
|
||
interface OnSearchItemClickListener { | ||
fun onSearchItemClick(item: Item) | ||
} | ||
} | ||
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. ViewModel에 리스너가 있는건 어색하네요. View로 옮겨주세요 |
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.
Item -> LocationEntity로 변경하셨으니 ItemDao도 그에 맞게 변경하면 좋겠네요