-
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 과제 #80
Open
leeghy
wants to merge
14
commits into
kakao-tech-campus-2nd-step2:leeghy
Choose a base branch
from
leeghy:step2
base: leeghy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5294b55
fix: MainViewModel 오류 해결
leeghy 6f176c1
feat: 불필요한 클래스 삭제
leeghy 8aa01ab
feat: Room 데이터베이스로 변경
leeghy 4aa9ca4
feat: DAO 생성
leeghy 1d06981
feat: Dao 내용추가
leeghy 787a00e
feat: Room 데이터베이스 클래스 생성
leeghy 53ae7d4
feat: DatabaseModule 추가
leeghy d7031ed
feat: 의존성 추가
leeghy 54aefd4
fix: hilt, room 오류 수정
leeghy f082ba1
feat: 의존성 주입
leeghy fb11712
fix: 필요없는 부분 삭제
leeghy 5f3f0e7
docs: 리드미 수정
leeghy ed47f46
refactor: KakaoLocalAPI 인터페이스 수정
leeghy 08795c6
refactor: MVVM 아키텍처 패턴 적용
leeghy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 30 additions & 22 deletions
52
app/src/main/java/campus/tech/kakao/map/Adapter/LocationAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
package campus.tech.kakao.map.Adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import campus.tech.kakao.map.Model.LocationData | ||
import campus.tech.kakao.map.R | ||
import campus.tech.kakao.map.databinding.ItemViewBinding | ||
|
||
class LocationAdapter( | ||
private val locationList: List<LocationData>, | ||
private val onItemViewClick: (LocationData) -> Unit | ||
) : RecyclerView.Adapter<LocationAdapter.ViewHolder>() { | ||
private val onItemClick: (LocationData) -> Unit | ||
) : ListAdapter<LocationData, LocationAdapter.ViewHolder>(LocationDiffCallback()) { | ||
|
||
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
val nameTextView: TextView = itemView.findViewById(R.id.name) | ||
val locationTextView: TextView = itemView.findViewById(R.id.location) | ||
val categoryTextView: TextView = itemView.findViewById(R.id.category) | ||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
return ViewHolder( | ||
ItemViewBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false | ||
) | ||
) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
val view = inflater.inflate(R.layout.item_view, parent, false) | ||
return ViewHolder(view) | ||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val location = getItem(position) | ||
holder.bind(location) | ||
holder.itemView.setOnClickListener { onItemClick(location) } | ||
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. onBindViewHolder 시점에 setOnClickListener 는 성능이 좋지 않습니다. 안티패턴이에요. ViewHolder 생성 시 onItemClick lambda 도 같이 추가하면 좋을 것 같네요 :) |
||
} | ||
|
||
override fun getItemCount(): Int = locationList.size | ||
class ViewHolder(private val binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind(location: LocationData) { | ||
binding.name.text = location.name | ||
binding.location.text = location.location | ||
binding.category.text = location.category | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val location = locationList[position] | ||
holder.nameTextView.text = location.name | ||
holder.locationTextView.text = location.location | ||
holder.categoryTextView.text = location.category | ||
private class LocationDiffCallback : DiffUtil.ItemCallback<LocationData>() { | ||
override fun areItemsTheSame(oldItem: LocationData, newItem: LocationData): Boolean { | ||
return oldItem.name == newItem.name | ||
} | ||
|
||
holder.itemView.setOnClickListener { | ||
onItemViewClick(location) | ||
override fun areContentsTheSame(oldItem: LocationData, newItem: LocationData): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/campus/tech/kakao/map/Model/AppDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package campus.tech.kakao.map.Model | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
|
||
@Database(entities = [LocationData::class], version = 1, exportSchema = false) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun locationDao(): LocationDao | ||
|
||
companion object { | ||
private var instance: AppDatabase? = null | ||
|
||
fun getInstance(context: Context): AppDatabase { | ||
return instance ?: synchronized(this) { | ||
instance ?: buildDatabase(context).also { instance = it } | ||
} | ||
} | ||
|
||
private fun buildDatabase(context: Context): AppDatabase { | ||
return Room.databaseBuilder( | ||
context.applicationContext, | ||
AppDatabase::class.java, | ||
"location_database" | ||
).build() | ||
} | ||
} | ||
} |
7 changes: 2 additions & 5 deletions
7
app/src/main/java/campus/tech/kakao/map/Model/KakaoLocalApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
package campus.tech.kakao.map.Model | ||
|
||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Header | ||
import retrofit2.http.Query | ||
|
||
interface KakaoLocalApi { | ||
@GET("v2/local/search/keyword.json") | ||
fun searchPlaces( | ||
@Header("Authorization") apiKey: String, | ||
suspend fun searchPlaces( | ||
@Query("query") query: String, | ||
@Query("size") size: Int = 15 | ||
): Call<SearchResult> | ||
): SearchResult | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/campus/tech/kakao/map/Model/LocationDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface LocationDao { | ||
@Query("SELECT * FROM locations") | ||
fun getAllLocations(): List<LocationData> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insertLocation(location: LocationData) | ||
|
||
@Delete | ||
fun deleteLocation(location: LocationData) | ||
|
||
@Query("DELETE FROM locations") | ||
fun deleteAllLocations() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
app/src/main/java/campus/tech/kakao/map/Model/RetrofitClient.kt
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
app/src/main/java/campus/tech/kakao/map/Model/SearchCallback.kt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
app/src/main/java/campus/tech/kakao/map/Module/DatabaseModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package campus.tech.kakao.map.Module | ||
|
||
import android.content.Context | ||
import campus.tech.kakao.map.Model.AppDatabase | ||
import campus.tech.kakao.map.Model.LocationDao | ||
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 provideAppDatabase(@ApplicationContext context: Context): AppDatabase { | ||
return AppDatabase.getInstance(context) | ||
} | ||
|
||
@Provides | ||
fun provideLocationDao(database: AppDatabase): LocationDao { | ||
return database.locationDao() | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/campus/tech/kakao/map/Module/NetworkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package campus.tech.kakao.map.Module | ||
|
||
import campus.tech.kakao.map.Model.KakaoLocalApi | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
private const val BASE_URL = "https://dapi.kakao.com/" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofit(): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideKakaoLocalApi(retrofit: Retrofit): KakaoLocalApi { | ||
return retrofit.create(KakaoLocalApi::class.java) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/campus/tech/kakao/map/Repository/LocationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package campus.tech.kakao.map.Repository | ||
|
||
import campus.tech.kakao.map.Model.KakaoLocalApi | ||
import campus.tech.kakao.map.Model.LocationData | ||
import campus.tech.kakao.map.Model.LocationDao | ||
import javax.inject.Inject | ||
|
||
class LocationRepository @Inject constructor( | ||
private val kakaoLocalApi: KakaoLocalApi, | ||
private val locationDao: LocationDao | ||
) { | ||
suspend fun searchLocations(query: String): List<LocationData> { | ||
return try { | ||
val response = kakaoLocalApi.searchPlaces(query) | ||
response.documents.map { place -> | ||
LocationData( | ||
name = place.place_name, | ||
location = place.address_name, | ||
category = place.category_group_name, | ||
latitude = place.y.toDouble(), | ||
longitude = place.x.toDouble() | ||
) | ||
} | ||
} catch (e: Exception) { | ||
// Log the error and return an empty list | ||
e.printStackTrace() | ||
emptyList() | ||
} | ||
} | ||
|
||
suspend fun getAllLocations(): List<LocationData> { | ||
return locationDao.getAllLocations() | ||
} | ||
|
||
suspend fun insertLocation(location: LocationData) { | ||
locationDao.insertLocation(location) | ||
} | ||
|
||
suspend fun deleteLocation(location: LocationData) { | ||
locationDao.deleteLocation(location) | ||
} | ||
|
||
suspend fun deleteAllLocations() { | ||
locationDao.deleteAllLocations() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍