Skip to content

Commit 3623157

Browse files
authored
feat: 아이디 검색목록 구현
2 parents 578e21a + 5234526 commit 3623157

7 files changed

Lines changed: 190 additions & 42 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.seok.gfd.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.EditText
7+
import androidx.fragment.app.FragmentActivity
8+
import androidx.lifecycle.ViewModelProviders
9+
import androidx.recyclerview.widget.RecyclerView
10+
import com.seok.gfd.R
11+
import com.seok.gfd.room.entity.SearchGithubId
12+
import com.seok.gfd.viewmodel.GithubIdViewModel
13+
import kotlinx.android.synthetic.main.item_search_github_id.view.*
14+
15+
class SearchGithubIdAdapter(list: ArrayList<SearchGithubId>, edtText : EditText) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
16+
private val githubIds: ArrayList<SearchGithubId> = list
17+
private val searchEditText = edtText
18+
private lateinit var githubIdsViewModel: GithubIdViewModel
19+
20+
inner class SearchGithubIdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
21+
fun bind(searchGithubId: SearchGithubId){
22+
itemView.item_search_txt_github_id.text = searchGithubId.gidName
23+
itemView.item_search_img_close.setOnClickListener {
24+
githubIdsViewModel.deleteGithubId(searchGithubId)
25+
githubIds.removeAt(this.adapterPosition)
26+
notifyItemRemoved(this.adapterPosition)
27+
notifyDataSetChanged()
28+
}
29+
itemView.item_search_card_view.setOnClickListener {
30+
searchEditText.setText(searchGithubId.gidName)
31+
}
32+
}
33+
}
34+
35+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
36+
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_github_id, parent, false)
37+
githubIdsViewModel = ViewModelProviders.of(parent.context as FragmentActivity).get(GithubIdViewModel::class.java)
38+
return SearchGithubIdViewHolder(view)
39+
}
40+
41+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
42+
val viewHolder = holder as SearchGithubIdViewHolder
43+
viewHolder.bind(githubIds[position])
44+
}
45+
46+
override fun getItemCount(): Int {
47+
return githubIds.size
48+
}
49+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ private const val TABLE_NAME = "search_github_ids"
88
@Dao
99
interface SearchGithubIdDao {
1010
@Insert(onConflict = OnConflictStrategy.REPLACE)
11-
suspend fun insert(searchGithubId: SearchGithubId) : Long
11+
suspend fun insert(searchGithubId: SearchGithubId): Long
1212

1313
@Delete
1414
suspend fun delete(searchGithubId: SearchGithubId)
1515

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

1922
@Query("delete from $TABLE_NAME")
2023
suspend fun deleteAll()
24+
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,42 @@ import android.app.Application
44
import androidx.lifecycle.AndroidViewModel
55
import androidx.lifecycle.LiveData
66
import androidx.lifecycle.MutableLiveData
7-
import androidx.room.Room
87
import com.seok.gfd.room.AppDatabase
98
import com.seok.gfd.room.entity.SearchGithubId
109
import kotlinx.coroutines.runBlocking
1110

1211
class GithubIdViewModel(val context: Application) : AndroidViewModel(context){
1312
private val TAG = this.javaClass.toString()
13+
private val database = AppDatabase.getInstance(context)
1414

1515
private val _githubIds = MutableLiveData<List<SearchGithubId>>()
1616

1717
val githubIds : LiveData<List<SearchGithubId>>
1818
get() = _githubIds
1919

2020
fun insertGithubId(githubId: SearchGithubId){
21-
val database = AppDatabase.getInstance(context)
2221
runBlocking {
2322
database.searchGithubIdDao().insert(githubId)
2423
}
2524
}
2625

2726
fun getGithubId(name : String){
28-
val database = AppDatabase.getInstance(context)
2927
runBlocking {
30-
_githubIds.value = database.searchGithubIdDao().selectAll(name)
31-
database.close()
28+
if(name == "" || name.isEmpty()) {
29+
_githubIds.value = database.searchGithubIdDao().selectAll()
30+
}else{
31+
_githubIds.value = database.searchGithubIdDao().selectAll(name)
32+
}
3233
}
3334
}
35+
36+
fun deleteGithubId(githubId: SearchGithubId){
37+
runBlocking {
38+
database.searchGithubIdDao().delete(githubId)
39+
}
40+
}
41+
42+
fun closeDatabase(){
43+
database.close()
44+
}
3445
}

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: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,90 @@
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
56
import android.view.WindowManager
67
import android.view.animation.AnimationUtils
78
import androidx.appcompat.app.AppCompatActivity
89
import androidx.lifecycle.Observer
910
import androidx.lifecycle.ViewModelProviders
10-
import androidx.room.Room
11+
import androidx.recyclerview.widget.DividerItemDecoration
12+
import androidx.recyclerview.widget.GridLayoutManager
13+
import androidx.recyclerview.widget.LinearLayoutManager
1114
import com.seok.gfd.R
12-
import com.seok.gfd.room.AppDatabase
15+
import com.seok.gfd.adapter.SearchGithubIdAdapter
1316
import com.seok.gfd.room.entity.SearchGithubId
1417
import com.seok.gfd.viewmodel.GithubIdViewModel
1518
import kotlinx.android.synthetic.main.activity_search.*
16-
import kotlinx.coroutines.runBlocking
17-
import java.util.*
19+
1820

1921
class SearchActivity : AppCompatActivity() {
2022
private lateinit var githubIdsViewModel: GithubIdViewModel
23+
private lateinit var gridLayoutManager: GridLayoutManager
24+
private lateinit var searchGithubIdAdapter: SearchGithubIdAdapter
25+
private lateinit var githubIds: ArrayList<SearchGithubId>
2126

2227
override fun onCreate(savedInstanceState: Bundle?) {
2328
super.onCreate(savedInstanceState)
2429
setContentView(R.layout.activity_search)
2530
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
2631

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

3164
private fun init() {
65+
githubIds = ArrayList()
66+
searchGithubIdAdapter = SearchGithubIdAdapter(githubIds, search_edt_id)
67+
gridLayoutManager = GridLayoutManager(this, 1)
68+
search_recycler_view.layoutManager = gridLayoutManager
69+
search_recycler_view.adapter = searchGithubIdAdapter
70+
search_recycler_view.addItemDecoration(
71+
DividerItemDecoration(
72+
this,
73+
LinearLayoutManager.VERTICAL
74+
)
75+
)
76+
}
77+
78+
private fun initViewModel() {
3279
githubIdsViewModel = ViewModelProviders.of(this).get(GithubIdViewModel::class.java)
3380
githubIdsViewModel.githubIds.observe(this, Observer {
34-
for(a in it){
35-
println("data${a.gid},${a.gidName},${a.created}")
36-
}
81+
githubIds.clear()
82+
githubIds.addAll(it)
83+
searchGithubIdAdapter.notifyDataSetChanged()
84+
println(githubIds)
3785
})
38-
githubIdsViewModel.getGithubId("t")
86+
// 전체 검색해놓은 것 가져오기
87+
githubIdsViewModel.getGithubId("")
3988
}
4089

4190
private fun setAnimation() {
@@ -47,29 +96,8 @@ class SearchActivity : AppCompatActivity() {
4796
val leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right)
4897
leftToRight.startOffset = 800
4998
search_layout_id.startAnimation(leftToRight)
99+
val rightToLeft = AnimationUtils.loadAnimation(this, R.anim.right_to_left)
100+
rightToLeft.startOffset = 1000
101+
search_recycler_view.startAnimation(rightToLeft)
50102
}
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-
// }
75103
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,13 @@
7878
app:layout_constraintCircleRadius="30dp"
7979
app:layout_constraintEnd_toEndOf="parent"
8080
app:layout_constraintStart_toStartOf="parent"/>
81+
82+
<androidx.recyclerview.widget.RecyclerView
83+
android:id="@+id/search_recycler_view"
84+
android:layout_width="0dp"
85+
android:layout_height="0dp"
86+
app:layout_constraintBottom_toTopOf="@+id/search_btn_ok"
87+
app:layout_constraintEnd_toEndOf="parent"
88+
app:layout_constraintStart_toStartOf="parent"
89+
app:layout_constraintTop_toBottomOf="@+id/search_layout_id" />
8190
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="50dp">
7+
8+
<com.google.android.material.card.MaterialCardView
9+
android:id="@+id/item_search_card_view"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:clickable="true"
13+
app:cardCornerRadius="0dp"
14+
app:cardElevation="0dp"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent">
19+
20+
<androidx.constraintlayout.widget.ConstraintLayout
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent">
23+
24+
<TextView
25+
android:id="@+id/item_search_txt_github_id"
26+
android:layout_width="0dp"
27+
android:layout_height="wrap_content"
28+
android:layout_marginStart="20dp"
29+
android:layout_marginEnd="20dp"
30+
android:textSize="16sp"
31+
app:layout_constraintBottom_toBottomOf="parent"
32+
app:layout_constraintEnd_toStartOf="@+id/item_search_img_close"
33+
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintTop_toTopOf="parent" />
35+
36+
<ImageView
37+
android:id="@+id/item_search_img_close"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_marginEnd="20dp"
41+
app:layout_constraintBottom_toBottomOf="parent"
42+
app:layout_constraintEnd_toEndOf="parent"
43+
app:layout_constraintTop_toTopOf="parent"
44+
app:srcCompat="@drawable/ic_close_gray" />
45+
</androidx.constraintlayout.widget.ConstraintLayout>
46+
</com.google.android.material.card.MaterialCardView>
47+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)