11package com.seok.gfd.views
22
33import android.os.Bundle
4- import android.util.Log
4+ import android.text.Editable
5+ import android.text.TextWatcher
6+ import android.view.View.OnFocusChangeListener
57import android.view.WindowManager
68import android.view.animation.AnimationUtils
79import androidx.appcompat.app.AppCompatActivity
810import androidx.lifecycle.Observer
911import 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
1115import com.seok.gfd.R
12- import com.seok.gfd.room.AppDatabase
16+ import com.seok.gfd.adapter.SearchGithubIdAdapter
1317import com.seok.gfd.room.entity.SearchGithubId
1418import com.seok.gfd.viewmodel.GithubIdViewModel
19+ import kotlinx.android.synthetic.main.activity_guest_main.*
1520import kotlinx.android.synthetic.main.activity_search.*
16- import kotlinx.coroutines.runBlocking
17- import java.util.*
21+
1822
1923class 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}
0 commit comments