Skip to content

Commit 4bc1b5a

Browse files
committed
feat: github id 이전 기록 검색 기능 구현
1 parent 0019e8a commit 4bc1b5a

6 files changed

Lines changed: 108 additions & 42 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.seok.gfd.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.recyclerview.widget.RecyclerView
7+
import com.seok.gfd.R
8+
import com.seok.gfd.room.entity.SearchGithubId
9+
import kotlinx.android.synthetic.main.item_search_github_id.view.*
10+
11+
class SearchGithubIdAdapter(list: ArrayList<SearchGithubId>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
12+
private val githubIds: ArrayList<SearchGithubId> = list
13+
14+
class SearchGithubIdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
15+
16+
fun bind(searchGithubId: SearchGithubId){
17+
itemView.item_search_txt_github_id.text = searchGithubId.gidName
18+
}
19+
}
20+
21+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
22+
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_github_id, parent, false)
23+
return SearchGithubIdViewHolder(view)
24+
}
25+
26+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
27+
val viewHolder = holder as SearchGithubIdViewHolder
28+
viewHolder.bind(githubIds[position])
29+
}
30+
31+
override fun getItemCount(): Int {
32+
return githubIds.size
33+
}
34+
}

app/src/main/java/com/seok/gfd/room/dao/SearchGithubIdDao.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface SearchGithubIdDao {
1313
@Delete
1414
suspend fun delete(searchGithubId: SearchGithubId)
1515

16-
@Query("SELECT * FROM $TABLE_NAME WHERE gid_name = :gidName LIKE '%' || :gidName || '%' ORDER BY created DESC")
16+
@Query("SELECT * FROM $TABLE_NAME WHERE gid_name LIKE '%' || :gidName || '%' ORDER BY created DESC")
1717
suspend fun selectAll(gidName: String): List<SearchGithubId>
1818

1919
@Query("SELECT * FROM $TABLE_NAME ORDER BY created DESC")

app/src/main/java/com/seok/gfd/viewmodel/GithubIdViewModel.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,30 @@ import kotlinx.coroutines.runBlocking
1010

1111
class GithubIdViewModel(val context: Application) : AndroidViewModel(context){
1212
private val TAG = this.javaClass.toString()
13+
private val database = AppDatabase.getInstance(context)
1314

1415
private val _githubIds = MutableLiveData<List<SearchGithubId>>()
1516

1617
val githubIds : LiveData<List<SearchGithubId>>
1718
get() = _githubIds
1819

1920
fun insertGithubId(githubId: SearchGithubId){
20-
val database = AppDatabase.getInstance(context)
2121
runBlocking {
2222
database.searchGithubIdDao().insert(githubId)
2323
}
2424
}
2525

2626
fun getGithubId(name : String){
27-
val database = AppDatabase.getInstance(context)
2827
runBlocking {
2928
if(name == "" || name.isEmpty()) {
30-
_githubIds.value = database.searchGithubIdDao().selectAll(name)
31-
}else{
3229
_githubIds.value = database.searchGithubIdDao().selectAll()
30+
}else{
31+
_githubIds.value = database.searchGithubIdDao().selectAll(name)
3332
}
34-
database.close()
3533
}
3634
}
35+
36+
fun closeDatabase(){
37+
database.close()
38+
}
3739
}

app/src/main/java/com/seok/gfd/views/LauncherActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ class LauncherActivity : AppCompatActivity() {
2020
val intent = Intent(this, SearchActivity::class.java)
2121
startActivity(intent)
2222
finish()
23-
}, 1000)
23+
}, 50)
2424
}
2525
}
Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,92 @@
11
package com.seok.gfd.views
22

33
import android.os.Bundle
4-
import android.util.Log
4+
import android.text.Editable
5+
import android.text.TextWatcher
6+
import android.view.View.OnFocusChangeListener
57
import android.view.WindowManager
68
import android.view.animation.AnimationUtils
79
import androidx.appcompat.app.AppCompatActivity
810
import androidx.lifecycle.Observer
911
import androidx.lifecycle.ViewModelProviders
10-
import androidx.room.Room
12+
import androidx.recyclerview.widget.DividerItemDecoration
13+
import androidx.recyclerview.widget.GridLayoutManager
14+
import androidx.recyclerview.widget.LinearLayoutManager
1115
import com.seok.gfd.R
12-
import com.seok.gfd.room.AppDatabase
16+
import com.seok.gfd.adapter.SearchGithubIdAdapter
1317
import com.seok.gfd.room.entity.SearchGithubId
1418
import com.seok.gfd.viewmodel.GithubIdViewModel
19+
import kotlinx.android.synthetic.main.activity_guest_main.*
1520
import kotlinx.android.synthetic.main.activity_search.*
16-
import kotlinx.coroutines.runBlocking
17-
import java.util.*
21+
1822

1923
class SearchActivity : AppCompatActivity() {
2024
private lateinit var githubIdsViewModel: GithubIdViewModel
25+
private lateinit var gridLayoutManager: GridLayoutManager
26+
private lateinit var searchGithubIdAdapter: SearchGithubIdAdapter
27+
private lateinit var githubIds: ArrayList<SearchGithubId>
2128

2229
override fun onCreate(savedInstanceState: Bundle?) {
2330
super.onCreate(savedInstanceState)
2431
setContentView(R.layout.activity_search)
2532
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
2633

2734
init()
35+
initViewModel()
2836
setAnimation()
37+
setListener()
38+
}
39+
40+
private fun setListener() {
41+
// EditText 포커싱 되었을 때
42+
// search_edt_id.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
43+
// if (hasFocus) {
44+
// githubIdsViewModel.getGithubId("")
45+
// }
46+
// }
47+
search_edt_id.addTextChangedListener(object : TextWatcher {
48+
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
49+
githubIdsViewModel.getGithubId(search_edt_id.text.toString())
50+
}
51+
52+
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
53+
}
54+
55+
override fun afterTextChanged(p0: Editable?) {
56+
}
57+
})
58+
59+
search_btn_ok.setOnClickListener {
60+
githubIdsViewModel.insertGithubId(SearchGithubId(search_edt_id.text.toString()))
61+
}
62+
63+
// githubIdsViewModel.closeDatabase() db 컨넥션 끊기
2964
}
3065

3166
private fun init() {
67+
githubIds = ArrayList()
68+
searchGithubIdAdapter = SearchGithubIdAdapter(githubIds)
69+
gridLayoutManager = GridLayoutManager(this, 1)
70+
search_recycler_view.layoutManager = gridLayoutManager
71+
search_recycler_view.adapter = searchGithubIdAdapter
72+
search_recycler_view.addItemDecoration(
73+
DividerItemDecoration(
74+
this,
75+
LinearLayoutManager.VERTICAL
76+
)
77+
)
78+
}
79+
80+
private fun initViewModel() {
3281
githubIdsViewModel = ViewModelProviders.of(this).get(GithubIdViewModel::class.java)
3382
githubIdsViewModel.githubIds.observe(this, Observer {
34-
for(a in it){
35-
println("data${a.gid},${a.gidName},${a.created}")
36-
}
83+
githubIds.clear()
84+
githubIds.addAll(it)
85+
searchGithubIdAdapter.notifyDataSetChanged()
86+
println(githubIds)
3787
})
38-
githubIdsViewModel.getGithubId("t")
88+
// 전체 검색해놓은 것 가져오기
89+
githubIdsViewModel.getGithubId("")
3990
}
4091

4192
private fun setAnimation() {
@@ -47,29 +98,8 @@ class SearchActivity : AppCompatActivity() {
4798
val leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right)
4899
leftToRight.startOffset = 800
49100
search_layout_id.startAnimation(leftToRight)
101+
val rightToLeft = AnimationUtils.loadAnimation(this, R.anim.right_to_left)
102+
rightToLeft.startOffset = 1000
103+
search_recycler_view.startAnimation(rightToLeft)
50104
}
51-
52-
// private fun iWantToKnowTheDatabaseIsFind() {
53-
// // 테스트 용으로 메모리상 생성
54-
// val database = Room.inMemoryDatabaseBuilder(
55-
// this,
56-
// AppDatabase::class.java
57-
// ).build()
58-
//
59-
// // id를 0 으로 설정해주어서 id가 autoGeneration 되게 한다.
60-
// val test = SearchGithubId(gidName = "test")
61-
// runBlocking {
62-
// // 습관을 씁니다.
63-
// database.searchGithubIdDao().insert(test)
64-
//
65-
// // 실제 DB에 써진 것을 확인합니다.
66-
// var dbHabitSchema = database.searchGithubIdDao().selectAll("t")[0]
67-
// Log.d(this.javaClass.name, "방금 넣은 것 $dbHabitSchema")
68-
//
69-
// // 지우는 것도 잘 동작하는지 확인해 봅니다.
70-
// database.searchGithubIdDao().delete(dbHabitSchema)
71-
//
72-
// Log.d(this.javaClass.name, "방금 지워서 아무것도 없음. ${database.searchGithubIdDao().selectAll("t")}")
73-
// }
74-
// }
75105
}

app/src/main/res/layout/item_search_github_id.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="100dp">
6+
android:layout_height="50dp">
77

88
<com.google.android.material.card.MaterialCardView
99
android:id="@+id/item_search_card_view"
@@ -27,7 +27,7 @@
2727
android:layout_height="wrap_content"
2828
android:layout_marginStart="20dp"
2929
android:layout_marginEnd="20dp"
30-
android:textSize="20sp"
30+
android:textSize="16sp"
3131
app:layout_constraintBottom_toBottomOf="parent"
3232
app:layout_constraintEnd_toStartOf="@+id/imageView2"
3333
app:layout_constraintStart_toStartOf="parent"

0 commit comments

Comments
 (0)