Skip to content

Commit

Permalink
Feat: Article 작성시 태그 작성 DB 적용 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
linenive committed Jun 20, 2021
1 parent 201540d commit 80991bd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_TITLE
import kr.ac.konkuk.koogle.DBKeys.Companion.CURRENT_NUMBER
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_ARTICLES
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.DESIRED_LOCATION
import kr.ac.konkuk.koogle.DBKeys.Companion.GROUP_ID
Expand Down Expand Up @@ -155,6 +156,7 @@ class AddArticleActivity : AppCompatActivity() {
}
}

// 글 작성 완료 버튼 클릭시
binding.submitButton.setOnClickListener {
articleId = articleRef.push().key.toString()
val articleTitle = binding.titleEditText.text.toString()
Expand Down Expand Up @@ -204,7 +206,8 @@ class AddArticleActivity : AppCompatActivity() {
articleTitle,
articleContent,
recruitmentNumber,
downloadedUrlList
downloadedUrlList,
tagRecyclerAdapter.data
)
createChatRoom(
writerId,
Expand Down Expand Up @@ -301,7 +304,6 @@ class AddArticleActivity : AppCompatActivity() {
}
}
}

private fun uploadArticle(
writerId: String,
writerName: String,
Expand All @@ -310,7 +312,8 @@ class AddArticleActivity : AppCompatActivity() {
articleTitle: String,
articleContent: String,
recruitmentNumber: Int,
uriList: ArrayList<String>
uriList: ArrayList<String>,
tagList: MutableList<TagModel>
) {
val currentArticleRef = articleRef.child(articleId)
val article = mutableMapOf<String, Any>()
Expand All @@ -329,6 +332,28 @@ class AddArticleActivity : AppCompatActivity() {
if (::searchResult.isInitialized)
article[DESIRED_LOCATION] = searchResult

// 태그 정보 추가
if(!tagList.isNullOrEmpty()){
val tags = mutableMapOf<String, Any>()
for((j, value) in tagList.withIndex()){
val newTag = mutableMapOf<String, Any>()
val newSubTag = mutableMapOf<String, Any>()
for ((i, s) in value.sub_tag_list.withIndex()) {
val content = s.split(" ")
// 만약 아무 내용 없는 서브 태그가 있으면 무시한다.
if(content[0]==null || content[0]==""|| content[0]==" ")
continue
newSubTag[content[0]] = i
}
newTag[DBKeys.TAG_INDEX] = j
newTag[DBKeys.SUB_TAGS] = newSubTag
newTag[DBKeys.TAG_TYPE] = value.tag_type
newTag[DBKeys.TAG_VALUE] = value.value
tags[value.main_tag_name] = newTag
}
article[DB_MAIN_TAGS] = tags
}

// currentArticleRef.updateChildren(article)
currentArticleRef.setValue(article)

Expand Down Expand Up @@ -490,41 +515,11 @@ class AddArticleActivity : AppCompatActivity() {
}

// 리사이클러뷰 초기화
protected fun initRecyclerView(){
// DB 에서
/*
// DB 에서 유저 태그 데이터 받아옴
val tagData: ArrayList<TagModel> = arrayListOf()
userTagRef = Firebase.database.reference
.child(DBKeys.DB_USER_TAG).child(firebaseUser.uid)
userTagRef.orderByChild(DBKeys.TAG_INDEX)
.limitToFirst(maxShowTag).addListenerForSingleValueEvent(object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for(s in snapshot.children){
val newSubTag = arrayListOf<String>()
for(st in s.child(DBKeys.SUB_TAGS).children){
newSubTag.add(st.key.toString())
}
tagData.add(
TagModel(
s.key.toString(), newSubTag,
s.child(DBKeys.TAG_VALUE).value.toString().toInt(),
s.child(DBKeys.TAG_TYPE).value.toString().toInt()
))
}
// 로딩 작업이 끝난 이후 RecyclerView 를 초기화하는 순서를 맞추기 위해 이곳에 넣음
initTagRecyclerView(tagData)
}
override fun onCancelled(error: DatabaseError) {
return
}
})
*/


private fun initRecyclerView(){
initTagRecyclerView(arrayListOf())
}

// 태그 관련 리사이클러뷰 추가
private fun initTagRecyclerView(data: ArrayList<TagModel>){
binding.tagRecyclerView.layoutManager = LinearLayoutManager(this)

Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/kr/ac/konkuk/koogle/Activity/ArticleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.SimpleTarget
Expand All @@ -32,6 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kr.ac.konkuk.koogle.Adapter.ArticleImageAdapter
import kr.ac.konkuk.koogle.Adapter.TagAdapter
import kr.ac.konkuk.koogle.DBKeys
import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_ID
import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_IMAGE_FILE_NAME
Expand All @@ -40,6 +44,7 @@ import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_TITLE
import kr.ac.konkuk.koogle.DBKeys.Companion.CURRENT_NUMBER
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_ARTICLES
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.USER_EMAIL
Expand All @@ -48,6 +53,7 @@ import kr.ac.konkuk.koogle.DBKeys.Companion.USER_NAME
import kr.ac.konkuk.koogle.DBKeys.Companion.USER_PROFILE_IMAGE_URL
import kr.ac.konkuk.koogle.Model.ArticleModel
import kr.ac.konkuk.koogle.Model.Entity.SearchResultEntity
import kr.ac.konkuk.koogle.Model.TagModel
import kr.ac.konkuk.koogle.Model.UserModel
import kr.ac.konkuk.koogle.databinding.ActivityArticleBinding
import java.text.SimpleDateFormat
Expand All @@ -60,6 +66,9 @@ class ArticleActivity : AppCompatActivity() {

val scope = CoroutineScope(Dispatchers.Main)

// 게시물에서 최대로 표시할 태그
val maxShowTag = 10

private lateinit var writerId:String
private lateinit var articleId:String
private lateinit var articleTitle:String
Expand All @@ -75,6 +84,7 @@ class ArticleActivity : AppCompatActivity() {
private var fileNameList: ArrayList<String> = arrayListOf()

private lateinit var imageAdapter: ArticleImageAdapter
private lateinit var tagRecyclerAdapter : TagAdapter

private lateinit var mapInfo:SearchResultEntity

Expand Down Expand Up @@ -119,6 +129,7 @@ class ArticleActivity : AppCompatActivity() {
initDB()
initViews()
initImageRecyclerView()
initRecyclerView()
initButton()
}

Expand Down Expand Up @@ -412,6 +423,53 @@ class ArticleActivity : AppCompatActivity() {
finish()
}

// 리사이클러뷰 초기화
private fun initRecyclerView(){
// DB 에서 게시글 태그 데이터 받아옴
val tagData: ArrayList<TagModel> = arrayListOf()
currentArticleRef.child(DB_MAIN_TAGS).orderByChild(DBKeys.TAG_INDEX)
.limitToFirst(maxShowTag).addListenerForSingleValueEvent(object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for(s in snapshot.children){
val newSubTag = arrayListOf<String>()
for(st in s.child(DBKeys.SUB_TAGS).children){
newSubTag.add(st.key.toString())
}
tagData.add(
TagModel(
s.key.toString(), newSubTag,
s.child(DBKeys.TAG_VALUE).value.toString().toInt(),
s.child(DBKeys.TAG_TYPE).value.toString().toInt()
))
}
// 로딩 작업이 끝난 이후 RecyclerView 를 초기화하는 순서를 맞추기 위해 이곳에 넣음
initTagRecyclerView(tagData)
}
override fun onCancelled(error: DatabaseError) {
return
}
})
initTagRecyclerView(arrayListOf())
}

// 태그 관련 리사이클러뷰 추가
private fun initTagRecyclerView(data: ArrayList<TagModel>){
binding.tagRecyclerView.layoutManager = LinearLayoutManager(this)

tagRecyclerAdapter = TagAdapter(this, data, false)
// 서브태그들 클릭했을 때 이벤트 구현
tagRecyclerAdapter.subTagClickListener = object : TagAdapter.OnItemClickListener {
override fun onItemClick(
holder: TagAdapter.DefaultViewHolder,
view: EditText,
data: TagModel,
position: Int
) {
}
}
binding.tagRecyclerView.adapter = tagRecyclerAdapter
}

private fun showProgress() {
binding.progressBar.isVisible = true
}
Expand Down

0 comments on commit 80991bd

Please sign in to comment.