-
Notifications
You must be signed in to change notification settings - Fork 33
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주차 과제 Step2 #34
Open
cleonno3o
wants to merge
17
commits into
kakao-tech-campus-2nd-step2:cleonno3o
Choose a base branch
from
cleonno3o:step2
base: cleonno3o
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 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21b34d9
docs: README.md 업데이트
cleonno3o 67847f8
feat: 기본 레이아웃 구현
cleonno3o 99fe361
feat: 초기 로컬 데이터베이스 구현
cleonno3o de54a46
docs: README.md 업데이트
cleonno3o b6d5fbe
feat: 검색 결과를 표시할 RecyclerView 추가
cleonno3o 0183943
feat: 결과에 표시될 아이템 레이아웃 구현
cleonno3o 844f359
docs: README.md 업데이트
cleonno3o 5b90825
feat: 검색어를 입력했을 때 검색 결과 표시 기능 구현
cleonno3o ce89e3e
feat: 검색어를 지우는 기능 구현
cleonno3o 3794349
feat: 검색 기록을 DB에 저장하는 기능 구현
cleonno3o e1af1c6
feat: 검색 기록을 검색 창 하단에 표시하는 기능 구현
cleonno3o b3f2ba3
feat: 검색 기록이 중복되지 않고 새롭게 추가되도록 변경
cleonno3o ac7775f
feat: X 버튼 누를 시 검색기록 삭제 기능 추가
cleonno3o 48c92b3
refactor: 코드 리팩터링
cleonno3o 15e4d83
style: 검색 기록 간 구분을 위한 디자인 추가
cleonno3o 8ad627a
style: code style reformat
cleonno3o 6dbd82d
refactor: 불필요한 초기화 제거
cleonno3o 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
# android-map-keyword | ||
|
||
## 1단계 | ||
- [x] 기본 레이아웃 구현 | ||
- [x] 로컬 데이터베이스 구현 | ||
- [x] 검색 결과 표시 레이아웃 구현 | ||
- [x] 결과 목록 레이아웃 | ||
- [x] 결과 아이템 레이아웃 | ||
|
||
## 2단계 | ||
- [x] 검색어를 입력하면 검색 결과 목록 표시 | ||
- [x] 검색 결과 목록 스크롤 가능 | ||
- [x] 입력한 검색어 X 버튼을 통해 삭제 가능 | ||
- [x] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 | ||
- [x] 저장된 검색어 가로 스크롤 가능 | ||
- [x] 저장된 검색어 X 버튼을 통해 삭제 가능 | ||
- [x] 저장된 검색어 DB에 저장 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package campus.tech.kakao.map | ||
|
||
interface DatabaseListener { | ||
fun deleteHistory(historyName: String) | ||
fun insertHistory(historyName: String) | ||
fun updateSearchResult() | ||
fun updateSearchHistory() | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.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,49 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageButton | ||
import android.widget.TextView | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.RecyclerView.ViewHolder | ||
|
||
class HistoryRecyclerAdapter( | ||
var history: List<String>, | ||
val layoutInflater: LayoutInflater, | ||
val databaseListener: DatabaseListener | ||
) : RecyclerView.Adapter<HistoryRecyclerAdapter.HistoryViewHolder>() { | ||
inner class HistoryViewHolder(itemView: View) : ViewHolder(itemView) { | ||
val name: TextView = itemView.findViewById(R.id.history_name) | ||
val clear: ImageButton = itemView.findViewById(R.id.history_clear) | ||
|
||
init { | ||
clear.setOnClickListener { | ||
if (bindingAdapterPosition != RecyclerView.NO_POSITION) { | ||
databaseListener.deleteHistory(name.text.toString()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { | ||
val view = layoutInflater.inflate(R.layout.item_search_history, parent, false) | ||
return HistoryViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { | ||
holder.name.text = history[position] | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return history.size | ||
} | ||
|
||
fun refreshList() { | ||
notifyDataSetChanged() | ||
} | ||
} |
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,14 @@ | ||
package campus.tech.kakao.map | ||
|
||
data class Location( | ||
val name: String, | ||
val category: String, | ||
val address: String | ||
) { | ||
companion object { | ||
const val CAFE: String = "카페" | ||
const val PHARMACY: String = "약국" | ||
const val RESTAURANT: String = "식당" | ||
const val NORMAL: String = "일반" | ||
} | ||
} |
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,11 +1,104 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.os.Bundle | ||
import android.widget.EditText | ||
import android.widget.ImageButton | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.view.isVisible | ||
import androidx.core.widget.doAfterTextChanged | ||
import androidx.lifecycle.Observer | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class MainActivity : AppCompatActivity(), DatabaseListener { | ||
private lateinit var viewModel: MapViewModel | ||
private lateinit var searchBox: EditText | ||
private lateinit var searchHistoryView: RecyclerView | ||
private lateinit var searchResultView: RecyclerView | ||
private lateinit var message: TextView | ||
private lateinit var clear: ImageButton | ||
|
||
private lateinit var searchResultAdapter: ResultRecyclerAdapter | ||
private lateinit var searchHistoryAdapter: HistoryRecyclerAdapter | ||
|
||
class MainActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
viewModel = MapViewModel(this, this) | ||
searchBox = findViewById(R.id.search_box) | ||
searchHistoryView = findViewById(R.id.search_history) | ||
searchResultView = findViewById(R.id.search_result) | ||
message = findViewById(R.id.message) | ||
clear = findViewById(R.id.clear) | ||
|
||
searchBox.doAfterTextChanged { | ||
it?.let { | ||
search(it.toString(), false) | ||
} | ||
} | ||
|
||
clear.setOnClickListener { | ||
searchBox.text.clear() | ||
} | ||
|
||
initSearchResultView() | ||
initSearchHistoryView() | ||
observeData() | ||
} | ||
|
||
override fun deleteHistory(historyName: String) { | ||
viewModel.deleteHistory(historyName) | ||
} | ||
|
||
override fun insertHistory(historyName: String) { | ||
viewModel.insertHistory(historyName) | ||
} | ||
|
||
override fun updateSearchResult() { | ||
val searchResult = viewModel.searchResult.value!! | ||
searchResultAdapter.refreshList() | ||
|
||
if (searchResult.isNotEmpty() && searchBox.text.isNotEmpty()) { | ||
searchResultView.isVisible = true | ||
message.isVisible = false | ||
} else { | ||
searchResultView.isVisible = false | ||
message.isVisible = true | ||
} | ||
} | ||
|
||
override fun updateSearchHistory() { | ||
searchHistoryAdapter.refreshList() | ||
} | ||
|
||
private fun search(locName: String, isExactMatch: Boolean) { | ||
viewModel.searchLocation(locName, isExactMatch) | ||
} | ||
|
||
private fun initSearchResultView() { | ||
searchResultAdapter = | ||
ResultRecyclerAdapter(viewModel.searchResult.value!!, layoutInflater, this) | ||
searchResultView.adapter = searchResultAdapter | ||
searchResultView.layoutManager = | ||
LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) | ||
} | ||
|
||
private fun initSearchHistoryView() { | ||
searchHistoryAdapter = | ||
HistoryRecyclerAdapter(viewModel.getAllHistory(), layoutInflater, this) | ||
searchHistoryView.adapter = searchHistoryAdapter | ||
searchHistoryView.layoutManager = | ||
LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) | ||
} | ||
|
||
private fun observeData() { | ||
viewModel.searchHistory.observe(this, Observer { | ||
searchHistoryAdapter.history = it | ||
}) | ||
viewModel.searchResult.observe(this, Observer { | ||
searchResultAdapter.searchResult = it | ||
}) | ||
} | ||
} |
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,13 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.provider.BaseColumns | ||
|
||
object MapContract { | ||
object MapEntry : BaseColumns { | ||
const val TABLE_NAME = "map" | ||
const val TABLE_NAME_HISTORY = "history" | ||
const val COLUMN_NAME_NAME = "name" | ||
const val COLUMN_NAME_CATEGORY = "category" | ||
const val COLUMN_NAME_ADDRESS = "address" | ||
} | ||
} |
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,61 @@ | ||
package campus.tech.kakao.map | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
import android.provider.BaseColumns | ||
|
||
class MapDbHelper(mContext: Context) : | ||
SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { | ||
override fun onCreate(db: SQLiteDatabase?) { | ||
db?.execSQL(SQL_CREATE_ENTRIES) | ||
db?.execSQL(SQL_CREATE_ENTRIES_HISTORY) | ||
initializeDb(db) | ||
} | ||
|
||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
db?.execSQL(SQL_DELETE_ENTRIES) | ||
db?.execSQL(SQL_DELETE_ENTRIES_HISTORY) | ||
onCreate(db) | ||
} | ||
|
||
private fun initializeDb(db: SQLiteDatabase?) { | ||
for (idx in 1..10) { | ||
val exampleCafeValue = ContentValues() | ||
exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_NAME, "카페$idx") | ||
exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_CATEGORY, Location.CAFE) | ||
exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_ADDRESS, "서울 성동구 성수동 $idx") | ||
db?.insert(MapContract.MapEntry.TABLE_NAME, null, exampleCafeValue) | ||
|
||
val examplePharValue = ContentValues() | ||
examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_NAME, "약국$idx") | ||
examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_CATEGORY, Location.PHARMACY) | ||
examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_ADDRESS, "서울 성동구 성수동 $idx") | ||
db?.insert(MapContract.MapEntry.TABLE_NAME, null, examplePharValue) | ||
} | ||
} | ||
|
||
companion object { | ||
const val DATABASE_NAME = "map.db" | ||
const val DATABASE_VERSION = 1 | ||
|
||
private const val SQL_CREATE_ENTRIES = | ||
"CREATE TABLE ${MapContract.MapEntry.TABLE_NAME} (" + | ||
"${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
"${MapContract.MapEntry.COLUMN_NAME_NAME} TEXT," + | ||
"${MapContract.MapEntry.COLUMN_NAME_CATEGORY} TEXT," + | ||
"${MapContract.MapEntry.COLUMN_NAME_ADDRESS} TEXT" + | ||
");" | ||
private const val SQL_DELETE_ENTRIES = | ||
"DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME}" | ||
|
||
private const val SQL_CREATE_ENTRIES_HISTORY = | ||
"CREATE TABLE ${MapContract.MapEntry.TABLE_NAME_HISTORY} (" + | ||
"${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
"${MapContract.MapEntry.COLUMN_NAME_NAME} TEXT" + | ||
");" | ||
private const val SQL_DELETE_ENTRIES_HISTORY = | ||
"DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME_HISTORY}" | ||
} | ||
} |
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.
db 버전이 업그레이드될때 실행되는 마이그레이션 작업인데
지금은 모든 데이터를 지우고 새로 설정 해주시고 계시네요 ㅎㅎ
지금은 실 서비스가 아니니까 상관 없는데
실제 서비스를 할땐 이런 작업을 유의해야합니다.
앱 업데이트를 했는데 내가 저장해둔 데이터가 날아가면... 무수히 많은 문의를 받을수도 있거든요
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.
이번 과제에서 실제로 upgrade하는 상황이 발생하지 않아 다 지워버리고 새로 생성한다고 구현했던 것 같습니다! 하지만 실제 서비스에서는 발생하면 정말 아찔할 것 같습니다.. 앞으로 실제 서비스여도 괜찮을지 고민해보며 구현하겠습니다!