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
56import android.view.WindowManager
67import android.view.animation.AnimationUtils
78import androidx.appcompat.app.AppCompatActivity
89import androidx.lifecycle.Observer
910import 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
1114import com.seok.gfd.R
12- import com.seok.gfd.room.AppDatabase
15+ import com.seok.gfd.adapter.SearchGithubIdAdapter
1316import com.seok.gfd.room.entity.SearchGithubId
1417import com.seok.gfd.viewmodel.GithubIdViewModel
1518import kotlinx.android.synthetic.main.activity_search.*
16- import kotlinx.coroutines.runBlocking
17- import java.util.*
19+
1820
1921class 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}
0 commit comments