Skip to content

Commit

Permalink
feat: 아이디 검색목록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
msnodeve authored Jan 3, 2021
2 parents 578e21a + 5234526 commit 3623157
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 42 deletions.
49 changes: 49 additions & 0 deletions app/src/main/java/com/seok/gfd/adapter/SearchGithubIdAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.seok.gfd.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.seok.gfd.R
import com.seok.gfd.room.entity.SearchGithubId
import com.seok.gfd.viewmodel.GithubIdViewModel
import kotlinx.android.synthetic.main.item_search_github_id.view.*

class SearchGithubIdAdapter(list: ArrayList<SearchGithubId>, edtText : EditText) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val githubIds: ArrayList<SearchGithubId> = list
private val searchEditText = edtText
private lateinit var githubIdsViewModel: GithubIdViewModel

inner class SearchGithubIdViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
fun bind(searchGithubId: SearchGithubId){
itemView.item_search_txt_github_id.text = searchGithubId.gidName
itemView.item_search_img_close.setOnClickListener {
githubIdsViewModel.deleteGithubId(searchGithubId)
githubIds.removeAt(this.adapterPosition)
notifyItemRemoved(this.adapterPosition)
notifyDataSetChanged()
}
itemView.item_search_card_view.setOnClickListener {
searchEditText.setText(searchGithubId.gidName)
}
}
}

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

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val viewHolder = holder as SearchGithubIdViewHolder
viewHolder.bind(githubIds[position])
}

override fun getItemCount(): Int {
return githubIds.size
}
}
10 changes: 7 additions & 3 deletions app/src/main/java/com/seok/gfd/room/dao/SearchGithubIdDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ private const val TABLE_NAME = "search_github_ids"
@Dao
interface SearchGithubIdDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(searchGithubId: SearchGithubId) : Long
suspend fun insert(searchGithubId: SearchGithubId): Long

@Delete
suspend fun delete(searchGithubId: SearchGithubId)

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

@Query("SELECT * FROM $TABLE_NAME ORDER BY created DESC")
suspend fun selectAll(): List<SearchGithubId>

@Query("delete from $TABLE_NAME")
suspend fun deleteAll()
}
21 changes: 16 additions & 5 deletions app/src/main/java/com/seok/gfd/viewmodel/GithubIdViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@ import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.room.Room
import com.seok.gfd.room.AppDatabase
import com.seok.gfd.room.entity.SearchGithubId
import kotlinx.coroutines.runBlocking

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

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

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

fun insertGithubId(githubId: SearchGithubId){
val database = AppDatabase.getInstance(context)
runBlocking {
database.searchGithubIdDao().insert(githubId)
}
}

fun getGithubId(name : String){
val database = AppDatabase.getInstance(context)
runBlocking {
_githubIds.value = database.searchGithubIdDao().selectAll(name)
database.close()
if(name == "" || name.isEmpty()) {
_githubIds.value = database.searchGithubIdDao().selectAll()
}else{
_githubIds.value = database.searchGithubIdDao().selectAll(name)
}
}
}

fun deleteGithubId(githubId: SearchGithubId){
runBlocking {
database.searchGithubIdDao().delete(githubId)
}
}

fun closeDatabase(){
database.close()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/seok/gfd/views/LauncherActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class LauncherActivity : AppCompatActivity() {
val intent = Intent(this, SearchActivity::class.java)
startActivity(intent)
finish()
}, 1000)
}, 50)
}
}
94 changes: 61 additions & 33 deletions app/src/main/java/com/seok/gfd/views/SearchActivity.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,90 @@
package com.seok.gfd.views

import android.os.Bundle
import android.util.Log
import android.text.Editable
import android.text.TextWatcher
import android.view.WindowManager
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.room.Room
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.seok.gfd.R
import com.seok.gfd.room.AppDatabase
import com.seok.gfd.adapter.SearchGithubIdAdapter
import com.seok.gfd.room.entity.SearchGithubId
import com.seok.gfd.viewmodel.GithubIdViewModel
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.coroutines.runBlocking
import java.util.*


class SearchActivity : AppCompatActivity() {
private lateinit var githubIdsViewModel: GithubIdViewModel
private lateinit var gridLayoutManager: GridLayoutManager
private lateinit var searchGithubIdAdapter: SearchGithubIdAdapter
private lateinit var githubIds: ArrayList<SearchGithubId>

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

init()
initViewModel()
setAnimation()
setListener()
}

private fun setListener() {
// EditText 포커싱 되었을 때
// search_edt_id.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
// if (hasFocus) {
// githubIdsViewModel.getGithubId("")
// }
// }
search_edt_id.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
githubIdsViewModel.getGithubId(search_edt_id.text.toString())
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun afterTextChanged(p0: Editable?) {
}
})

search_btn_ok.setOnClickListener {
githubIdsViewModel.insertGithubId(SearchGithubId(search_edt_id.text.toString()))
}

// githubIdsViewModel.closeDatabase() db 컨넥션 끊기
}

private fun init() {
githubIds = ArrayList()
searchGithubIdAdapter = SearchGithubIdAdapter(githubIds, search_edt_id)
gridLayoutManager = GridLayoutManager(this, 1)
search_recycler_view.layoutManager = gridLayoutManager
search_recycler_view.adapter = searchGithubIdAdapter
search_recycler_view.addItemDecoration(
DividerItemDecoration(
this,
LinearLayoutManager.VERTICAL
)
)
}

private fun initViewModel() {
githubIdsViewModel = ViewModelProviders.of(this).get(GithubIdViewModel::class.java)
githubIdsViewModel.githubIds.observe(this, Observer {
for(a in it){
println("data${a.gid},${a.gidName},${a.created}")
}
githubIds.clear()
githubIds.addAll(it)
searchGithubIdAdapter.notifyDataSetChanged()
println(githubIds)
})
githubIdsViewModel.getGithubId("t")
// 전체 검색해놓은 것 가져오기
githubIdsViewModel.getGithubId("")
}

private fun setAnimation() {
Expand All @@ -47,29 +96,8 @@ class SearchActivity : AppCompatActivity() {
val leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right)
leftToRight.startOffset = 800
search_layout_id.startAnimation(leftToRight)
val rightToLeft = AnimationUtils.loadAnimation(this, R.anim.right_to_left)
rightToLeft.startOffset = 1000
search_recycler_view.startAnimation(rightToLeft)
}

// private fun iWantToKnowTheDatabaseIsFind() {
// // 테스트 용으로 메모리상 생성
// val database = Room.inMemoryDatabaseBuilder(
// this,
// AppDatabase::class.java
// ).build()
//
// // id를 0 으로 설정해주어서 id가 autoGeneration 되게 한다.
// val test = SearchGithubId(gidName = "test")
// runBlocking {
// // 습관을 씁니다.
// database.searchGithubIdDao().insert(test)
//
// // 실제 DB에 써진 것을 확인합니다.
// var dbHabitSchema = database.searchGithubIdDao().selectAll("t")[0]
// Log.d(this.javaClass.name, "방금 넣은 것 $dbHabitSchema")
//
// // 지우는 것도 잘 동작하는지 확인해 봅니다.
// database.searchGithubIdDao().delete(dbHabitSchema)
//
// Log.d(this.javaClass.name, "방금 지워서 아무것도 없음. ${database.searchGithubIdDao().selectAll("t")}")
// }
// }
}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@
app:layout_constraintCircleRadius="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/search_btn_ok"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_layout_id" />
</androidx.constraintlayout.widget.ConstraintLayout>
47 changes: 47 additions & 0 deletions app/src/main/res/layout/item_search_github_id.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp">

<com.google.android.material.card.MaterialCardView
android:id="@+id/item_search_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
app:cardCornerRadius="0dp"
app:cardElevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/item_search_txt_github_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/item_search_img_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/item_search_img_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_close_gray" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 3623157

Please sign in to comment.