Skip to content

Commit a6fb285

Browse files
committed
feat: github id 이전 기록 검색 입력 및 삭제 구현
1 parent 4bc1b5a commit a6fb285

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

app/src/main/java/com/seok/gfd/adapter/SearchGithubIdAdapter.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@ package com.seok.gfd.adapter
33
import android.view.LayoutInflater
44
import android.view.View
55
import android.view.ViewGroup
6+
import androidx.fragment.app.FragmentActivity
7+
import androidx.lifecycle.ViewModelProviders
68
import androidx.recyclerview.widget.RecyclerView
79
import com.seok.gfd.R
810
import com.seok.gfd.room.entity.SearchGithubId
11+
import com.seok.gfd.viewmodel.GithubIdViewModel
912
import kotlinx.android.synthetic.main.item_search_github_id.view.*
1013

1114
class SearchGithubIdAdapter(list: ArrayList<SearchGithubId>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
1215
private val githubIds: ArrayList<SearchGithubId> = list
16+
private lateinit var githubIdsViewModel: GithubIdViewModel
1317

14-
class SearchGithubIdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
15-
18+
inner class SearchGithubIdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
1619
fun bind(searchGithubId: SearchGithubId){
1720
itemView.item_search_txt_github_id.text = searchGithubId.gidName
21+
itemView.item_search_img_close.setOnClickListener {
22+
githubIdsViewModel.deleteGithubId(searchGithubId)
23+
githubIds.removeAt(this.adapterPosition)
24+
notifyItemRemoved(this.adapterPosition)
25+
notifyDataSetChanged()
26+
}
1827
}
1928
}
2029

2130
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
2231
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_github_id, parent, false)
32+
githubIdsViewModel = ViewModelProviders.of(parent.context as FragmentActivity).get(GithubIdViewModel::class.java)
2333
return SearchGithubIdViewHolder(view)
2434
}
2535

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class GithubIdViewModel(val context: Application) : AndroidViewModel(context){
3333
}
3434
}
3535

36+
fun deleteGithubId(githubId: SearchGithubId){
37+
runBlocking {
38+
database.searchGithubIdDao().delete(githubId)
39+
}
40+
}
41+
3642
fun closeDatabase(){
3743
database.close()
3844
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
android:layout_marginEnd="20dp"
3030
android:textSize="16sp"
3131
app:layout_constraintBottom_toBottomOf="parent"
32-
app:layout_constraintEnd_toStartOf="@+id/imageView2"
32+
app:layout_constraintEnd_toStartOf="@+id/item_search_img_close"
3333
app:layout_constraintStart_toStartOf="parent"
3434
app:layout_constraintTop_toTopOf="parent" />
3535

3636
<ImageView
37-
android:id="@+id/imageView2"
37+
android:id="@+id/item_search_img_close"
3838
android:layout_width="wrap_content"
3939
android:layout_height="wrap_content"
4040
android:layout_marginEnd="20dp"

0 commit comments

Comments
 (0)