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_오진우_2주차 과제_Step1 #25

Open
wants to merge 7 commits into
base: fivejinw
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# android-map-keyword
# 2주차 - android-map-keyword
## step1 구현할 기능 목록

**UI 관련 기능**
- [x] 검색어를 입력할 수 있는 칸 구현
- [x] 검색 결과를 표시할 수 있는 칸 구현
- [ ] 저장된 검색 결과를 표시할 수 있는 칸 구현

**DB 관련 기능**
- [ ] 장소의 이름, 장소의 위치. 장소 카테고리를 저장할 수 있는 데이터베이스 구
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:theme="@style/Theme.Map"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".view.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/campus/tech/kakao/map/MainActivity.kt

This file was deleted.

12 changes: 12 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/db/PlaceContract.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package campus.tech.kakao.map.db

import android.provider.BaseColumns

object PlaceContract{
object PlaceEntry : BaseColumns {
const val TABLE_NAME = "PLACE"
const val COLUMN_NAME = "NAME"
const val COLUMN_LOCATION = "LOCATION"
const val COLUMN_CATEGORY = "CATEGORY"
}
Comment on lines +5 to +11
Copy link

Choose a reason for hiding this comment

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

👍

}
50 changes: 50 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/db/PlaceDBHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package campus.tech.kakao.map.db

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper


class PlaceDBHelper(context:Context) : SQLiteOpenHelper(context, "place.db", null, 1){
Copy link

Choose a reason for hiding this comment

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

place.db 와 같은 것들도 const 로 정의하면 조금 더 좋겠네요!

override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL("CREATE TABLE " +
"${PlaceContract.PlaceEntry.TABLE_NAME}( " +
" ${PlaceContract.PlaceEntry.COLUMN_NAME} varchar(60) not null ," +
" ${PlaceContract.PlaceEntry.COLUMN_LOCATION} varchar(255) not null, " +
" ${PlaceContract.PlaceEntry.COLUMN_CATEGORY} varchar(30) );"
)
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS ${PlaceContract.PlaceEntry.TABLE_NAME};")
onCreate(db)
Copy link

Choose a reason for hiding this comment

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

onCreate 를 직접 호출하기 보다 onCreate 에서 수행하는 로직을 따로 메소드로 분리하는 것은 어떨까요?

override fun onCreate(db: SQLiteDatabase) {
    createLocationTable(db)
}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion) {
    db?.execSQL("DROP TABLE IF EXISTS ${PlaceContract.PlaceEntry.TABLE_NAME};")
    createLocationTable(db)
}

private fun createLocationTable(db: SQLiteDatabase) {
    db?.execSQL("CREATE TABLE " +
            "${PlaceContract.PlaceEntry.TABLE_NAME}( " +
            " ${PlaceContract.PlaceEntry.COLUMN_NAME} varchar(60) not null ," +
            " ${PlaceContract.PlaceEntry.COLUMN_LOCATION} varchar(255) not null, " +
            " ${PlaceContract.PlaceEntry.COLUMN_CATEGORY} varchar(30) );"
        )
}

}

fun insertDummyData(){
val name = "카페"
val category = "카페"
val location = "서울 성동구 성수동"
for(i in 1..10){
insertData(name + i, location + i, category)
}
readData()
}
Comment on lines +25 to +33
Copy link

Choose a reason for hiding this comment

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

사용하고 있지 않은 코드네요!


fun insertData(name: String, location: String, category: String) {
val db = this.writableDatabase
val values = ContentValues().apply {
put(PlaceContract.PlaceEntry.COLUMN_NAME, name)
put(PlaceContract.PlaceEntry.COLUMN_LOCATION, location)
put(PlaceContract.PlaceEntry.COLUMN_CATEGORY, category)
}
db.insert(PlaceContract.PlaceEntry.TABLE_NAME, null, values)
}

fun readData(): Cursor {
val rDb = this.readableDatabase

return rDb.rawQuery("SELECT * FROM ${PlaceContract.PlaceEntry.TABLE_NAME};", null)
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/model/Place.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package campus.tech.kakao.map.model

data class Place(
val name : String,
val location : String,
val category : String
)
36 changes: 36 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/repository/Repository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package campus.tech.kakao.map.repository

import android.util.Log
import campus.tech.kakao.map.db.PlaceContract
import campus.tech.kakao.map.db.PlaceDBHelper
import campus.tech.kakao.map.model.Place

class Repository (val dbHelper: PlaceDBHelper){
Copy link

Choose a reason for hiding this comment

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

Repository 의 네이밍이 조금 더 명확했으면 좋겠습니다!

Copy link

Choose a reason for hiding this comment

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

또한 Repository 가 PlaceDBHelper 를 가지고 있는데, PlaceDBHelper 의 close 를 호출하는 부분이 없네요! 앱 사용에 따라 반드시 close 되어야 합니다. 앱 라이프사이클에 따라 close 시점을 고민해보시면 좋을 것 같습니다!

fun getAllPlace() : MutableList<Place>{
Copy link

Choose a reason for hiding this comment

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

getAllPlace 는 수정될 여지가 없기 때문에 MutableList 를 반환하기 보다는 Immutable 한 List 를 반환하는 것이 어떨까요?

Suggested change
fun getAllPlace() : MutableList<Place>{
fun getAllPlace() : List<Place>{

val cursor = dbHelper.readData()
val result = mutableListOf<Place>()

while (cursor.moveToNext()) {
val place = Place(
cursor.getString(
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_NAME)
),
cursor.getString(
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_LOCATION)
),
cursor.getString(
cursor.getColumnIndexOrThrow(PlaceContract.PlaceEntry.COLUMN_CATEGORY)
)
)
Log.d("readData", "이름 = ${place.name}, 위치 = ${place.location}, 분류 = ${place.category}")
result.add(place)
}

cursor.close()
return result
}

fun writePlace(place: Place){
dbHelper.insertData(place.name, place.location, place.category)
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/view/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package campus.tech.kakao.map.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import campus.tech.kakao.map.db.PlaceDBHelper
import campus.tech.kakao.map.R
import campus.tech.kakao.map.repository.Repository
import campus.tech.kakao.map.viewmodel.MainActivityViewModel
import campus.tech.kakao.map.viewmodel.ViewModelFactory

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dbHelper = PlaceDBHelper(this)
val repository = Repository(dbHelper)
val viewModel = ViewModelProvider(this, ViewModelFactory(repository))[MainActivityViewModel::class.java]
viewModel.getPlace()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package campus.tech.kakao.map.viewmodel

import androidx.lifecycle.ViewModel
import campus.tech.kakao.map.repository.Repository

class MainActivityViewModel (private val repository: Repository) : ViewModel() {
fun getPlace(){
repository.getAllPlace()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package campus.tech.kakao.map.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import campus.tech.kakao.map.repository.Repository

class ViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if(modelClass.isAssignableFrom(MainActivityViewModel::class.java)){
return MainActivityViewModel(repository) as T
}
throw IllegalArgumentException("unKnown ViewModel class")
}
}
55 changes: 51 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,62 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".view.MainActivity">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/input_search_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:hint="@string/search_hint"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:paddingHorizontal="10dp"
/>
<ImageView
android:id="@+id/button_X"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintBottom_toBottomOf="@+id/input_search_field"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:src="@android:drawable/ic_menu_close_clear_cancel"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/saved_search_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/input_search_field"
app:layout_constraintBottom_toTopOf="@id/search_result_recyclerView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="gone"
/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_result_recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/saved_search_recyclerView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>

<TextView
android:id="@+id/no_search_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/no_search_result"
android:textSize="18sp"
android:textColor="#737373"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Map</string>
<string name="search_hint">검색어를 입력해 주세요.</string>
<string name="no_search_result">검색 결과가 없습니다.</string>
</resources>