Skip to content

Commit

Permalink
Search Article containing Specific Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kkjsw17 committed Jun 20, 2021
1 parent 78bf81f commit b38c9a9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import kr.ac.konkuk.koogle.DBKeys.Companion.DB_GROUPS
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_MAIN_TAGS
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_USERS
import kr.ac.konkuk.koogle.DBKeys.Companion.GROUP_ID
import kr.ac.konkuk.koogle.DBKeys.Companion.TAG_INDEX
import kr.ac.konkuk.koogle.DBKeys.Companion.USER_EMAIL
import kr.ac.konkuk.koogle.DBKeys.Companion.USER_ID
import kr.ac.konkuk.koogle.DBKeys.Companion.USER_NAME
Expand Down Expand Up @@ -426,7 +427,7 @@ class ArticleActivity : AppCompatActivity() {
private fun initRecyclerView(){
// DB 에서 게시글 태그 데이터 받아옴
val tagData: ArrayList<TagModel> = arrayListOf()
currentArticleRef.child(DB_MAIN_TAGS).orderByChild(DBKeys.TAG_INDEX)
currentArticleRef.child(DB_MAIN_TAGS).orderByChild(TAG_INDEX)
.limitToFirst(MAXSHOWTAG).addListenerForSingleValueEvent(object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for(s in snapshot.children){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kr.ac.konkuk.koogle.Fragment
import android.content.Intent
import android.os.Bundle
import android.provider.ContactsContract
import android.text.TextUtils
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
Expand All @@ -13,20 +14,21 @@ import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.database.ChildEventListener
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.*
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import kr.ac.konkuk.koogle.Activity.AddArticleActivity
import kr.ac.konkuk.koogle.Activity.ArticleActivity
import kr.ac.konkuk.koogle.Activity.LogInActivity
import kr.ac.konkuk.koogle.Adapter.CommunityAdapter
import kr.ac.konkuk.koogle.DBKeys
import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_ID
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_ARTICLES
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_MAIN_TAGS
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_USERS
import kr.ac.konkuk.koogle.DBKeys.Companion.SUB_TAGS
import kr.ac.konkuk.koogle.Model.ArticleModel
import kr.ac.konkuk.koogle.Model.TagModel
import kr.ac.konkuk.koogle.R
import kr.ac.konkuk.koogle.databinding.FragmentCommunityBinding

Expand All @@ -38,6 +40,7 @@ class CommunityFragment : Fragment(R.layout.fragment_community) {
private lateinit var communityAdapter: CommunityAdapter

private val articleList = mutableListOf<ArticleModel>()
private var searchedArticleList = mutableListOf<ArticleModel>()

private val auth: FirebaseAuth by lazy {
Firebase.auth
Expand Down Expand Up @@ -156,6 +159,67 @@ class CommunityFragment : Fragment(R.layout.fragment_community) {
//startActivity(Intent(requireContext(),ArticleAddActivity::class.java))
}
}

binding!!.searchImageView.setOnClickListener {
val searchText = binding!!.searchEditText.text.toString()
val articleRef = Firebase.database.reference.child(DB_ARTICLES)

if (searchText.isEmpty()) {
articleRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
articleList.clear()
for (article in snapshot.children) {
articleList.add(0, article.getValue(ArticleModel::class.java)!!)
}
communityAdapter.submitList(articleList)
communityAdapter.notifyDataSetChanged()
}

override fun onCancelled(error: DatabaseError) {

}

})
} else {
searchedArticleList.clear()
articleRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (article in snapshot.children) {
for (tag in article.child(DB_MAIN_TAGS).children) {
var isContain = false
if (searchText in tag.key.toString()) {
searchedArticleList.add(
0,
article.getValue(ArticleModel::class.java)!!
)
break
}
for (subtag in tag.child(SUB_TAGS).children) {
if (searchText in subtag.key.toString()) {
searchedArticleList.add(
0,
article.getValue(ArticleModel::class.java)!!
)
isContain = true
break
}
}
if (isContain) {
break
}
}
}
communityAdapter.submitList(searchedArticleList)
communityAdapter.notifyDataSetChanged()
}

override fun onCancelled(error: DatabaseError) {

}

})
}
}
}

override fun onResume() {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_community.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
android:id="@+id/searchEditText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="6"
android:layout_gravity="center_vertical"/>
android:hint="제목, 글 내용, 태그 등"