SearchActivity.kt
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.example.vip
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
//import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.view.View
import android.content.Intent
import android.content.SharedPreferences
import android.util.Log
import android.view.inputmethod.InputMethodManager
import android.widget.*
import androidx.core.content.ContextCompat
import com.google.firebase.database.*
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.search.*
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.ValueEventListener
import androidx.fragment.app.FragmentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
data class MemoItem(
val Target : String = "",
val Policy : String = "",
val Content : String = "",
val Link : String = "",
val Value : String = ""
)
var database = FirebaseDatabase.getInstance().reference
class SearchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.search)
val prof: SharedPreferences = getSharedPreferences("policy_key", Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor=prof.edit()
val searchList = ArrayList<SearchItem>()
val no_search : ImageView = findViewById(R.id.no_matching) // 노서치이미지
val searchView: SearchView
searchView = findViewById(R.id.searchForm)
//searchView.onActionViewExpanded(); //new Added line
searchView.setIconifiedByDefault(false)
///
database.orderByChild("Policy").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val size = dataSnapshot.childrenCount
//Toast.makeText(this@SearchActivity, size.toString(), Toast.LENGTH_SHORT).show()
if (prof.getString("len", "-1") != size.toString()) {
//Toast.makeText(this@SearchActivity, "비 효율", Toast.LENGTH_SHORT).show()
editor.putString("len", size.toString())
for (memoSnapshot in dataSnapshot.children) {
val memo = memoSnapshot.getValue(MemoItem::class.java)
editor.putString(memo!!.Value, memo!!.Policy)
}
editor.commit()
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
fun CloseKeyboard()
{
var view = this.currentFocus
if(view != null)
{
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
searchList.clear()
// 검색 버튼이 눌러졌을 때 이벤트 처리
val layout : LinearLayout = findViewById(R.id.hashtag_linear)
layout.setVisibility(View.GONE)
CloseKeyboard()
// DB 내 목록에서 맞는거 찾는 부분
val key = arrayListOf<String>()
for (i in 0.. (prof.getString("len", "-1")!!.toInt() -1)) {
val temp = prof.getString("$i", "fail")
val regex = "$query".toRegex()
val matchResult : MatchResult? = regex.find(temp.toString())
if (matchResult != null) {
key.add(temp.toString())
}
}
no_search.setVisibility(View.GONE)
policy_scroll_view.setVisibility(View.VISIBLE)
for (i in key) {
val key_query = database.orderByChild("Policy").equalTo("$i")
key_query.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
//Toast.makeText(getBaseContext(), dataSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show()
for (memoSnapshot in dataSnapshot.children){
val memo = memoSnapshot.getValue(MemoItem::class.java)
searchList.add(
SearchItem(
ContextCompat.getDrawable(this@SearchActivity, R.drawable.image01)!!, memo!!.Policy
)
)
}
val adapter=SearchAdapter(searchList)
searchRecyclerView.adapter=adapter
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
if (key.size == 0) {
policy_scroll_view.setVisibility(View.GONE)
no_search.setVisibility(View.VISIBLE)
}
//////////
/*
database.orderByChild("Policy").startAt("$query").endAt("$query"+"\uf8ff").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
Toast.makeText(this@SearchActivity, "실패 : $query", Toast.LENGTH_SHORT).show()
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists() == false){ // 값 없을 때
policy_scroll_view.setVisibility(View.GONE)
no_search.setVisibility(View.VISIBLE)
}
else {
no_search.setVisibility(View.GONE)
policy_scroll_view.setVisibility(View.VISIBLE)
}
for (memoSnapshot in dataSnapshot.children){
val memo = memoSnapshot.getValue(MemoItem::class.java)
searchList.add(
SearchItem(
ContextCompat.getDrawable(this@SearchActivity, R.drawable.image01)!!, memo!!.Policy
)
)
}
val adapter=SearchAdapter(searchList)
searchRecyclerView.adapter=adapter
}
})*/
return true
}
override fun onQueryTextChange(newText: String): Boolean {
// 검색어가 변경되었을 때 이벤트 처리
return false
}
})
}
}