CFragment.kt 7.94 KB
package com.wello.vip


import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import kotlinx.android.synthetic.main.fragment_a.*

class CFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_a, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val policyList = ArrayList<PolicyItem>()
        var recomdatabase = FirebaseDatabase.getInstance().reference
        val prof: SharedPreferences = view!!.context.getSharedPreferences("profdata", Context.MODE_PRIVATE)

        recomdatabase.orderByChild("View").addListenerForSingleValueEvent(object :
            ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {
                //Toast.makeText(this@RecommendActivity, "실패부분", Toast.LENGTH_SHORT).show()
            }
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                //if (policyList0.size == 0) {

                for (memoSnapshot in dataSnapshot.children) {
                    val memo = memoSnapshot.getValue(MemoItemRecom::class.java)


                    var all_flag = arrayListOf<String>()
                    var check_flag = arrayListOf<String>()

                    var user_category = arrayListOf<String>()
                    var user_topic = prof.getString("existing_topic","none")
                    //Toast.makeText(this@RecommendActivity, user_topic, Toast.LENGTH_SHORT).show()

                    val regex = "c[0-9]+[_][0-9]+".toRegex()
                    val matchResult  : Sequence<MatchResult> = regex.findAll(user_topic.toString())

                    for(i in matchResult) {
                        if (i.value.split("_")[1] != "1") {
                            user_category.add(i.value)
                        }
                    }

                    for(i in user_category) {
                        if (i.split("_")[0] == "c1") { all_flag.add("c1"); if ((i.split("_")[1] in memo!!.Category1.split(" ")) || (memo!!.Category1.split(" ")[0] == "1")) { check_flag.add("c1") } }
                        if (i.split("_")[0] == "c2") { all_flag.add("c2"); if ((i.split("_")[1] in memo!!.Category2.split(" ")) || (memo!!.Category2.split(" ")[0] == "1")) { check_flag.add("c2") } }
                        if (i.split("_")[0] == "c3") { all_flag.add("c3"); if ((i.split("_")[1] in memo!!.Category3.split(" ")) || (memo!!.Category3.split(" ")[0] == "1")) { check_flag.add("c3") } }
                        if (i.split("_")[0] == "c4") { all_flag.add("c4"); if ((i.split("_")[1] in memo!!.Category4.split(" ")) || (memo!!.Category4.split(" ")[0] == "1")) { check_flag.add("c4") } }
                        if (i.split("_")[0] == "c5") { all_flag.add("c5"); if ((i.split("_")[1] in memo!!.Category5.split(" ")) || (memo!!.Category5.split(" ")[0] == "1")) { check_flag.add("c5") } }
                        if (i.split("_")[0] == "c6") { all_flag.add("c6"); if ((i.split("_")[1] in memo!!.Category6.split(" ")) || (memo!!.Category6.split(" ")[0] == "1")) { check_flag.add("c6") } }
                        if (i.split("_")[0] == "c7") { all_flag.add("c7"); if ((i.split("_")[1] in memo!!.Category7.split(" ")) || (memo!!.Category7.split(" ")[0] == "1")) { check_flag.add("c7") } }
                        if (i.split("_")[0] == "c8") { all_flag.add("c8"); if ((i.split("_")[1] in memo!!.Category8.split(" ")) || (memo!!.Category8.split(" ")[0] == "1")) { check_flag.add("c8") } }
                        if (i.split("_")[0] == "c9") { all_flag.add("c9"); if ((i.split("_")[1] in memo!!.Category9.split(" ")) || (memo!!.Category9.split(" ")[0] == "1")) { check_flag.add("c9") } }
                        if (i.split("_")[0] == "c10") { all_flag.add("c10"); if ((i.split("_")[1] in memo!!.Category10.split(" ")) || (memo!!.Category10.split(" ")[0] == "1")) { check_flag.add("c10") } }
                        if (i.split("_")[0] == "c11") { all_flag.add("c11"); if ((i.split("_")[1] in memo!!.Category11.split(" ")) || (memo!!.Category11.split(" ")[0] == "1")) { check_flag.add("c11") } }
                        if (i.split("_")[0] == "c12") { all_flag.add("c12"); if ((i.split("_")[1] in memo!!.Category12.split(" ")) || (memo!!.Category12.split(" ")[0] == "1")) { check_flag.add("c12") } }
                        if (i.split("_")[0] == "c13") { all_flag.add("c13"); if ((i.split("_")[1] in memo!!.Category13.split(" ")) || (memo!!.Category13.split(" ")[0] == "1")) { check_flag.add("c13") } }
                        if (i.split("_")[0] == "c14") { all_flag.add("c14"); if ((i.split("_")[1] in memo!!.Category14.split(" ")) || (memo!!.Category14.split(" ")[0] == "1")) { check_flag.add("c14") } }
                        if (i.split("_")[0] == "c15") { all_flag.add("c15"); if ((i.split("_")[1] in memo!!.Category15.split(" ")) || (memo!!.Category15.split(" ")[0] == "1")) { check_flag.add("c15") } }
                        if (i.split("_")[0] == "c16") { all_flag.add("c16"); if ((i.split("_")[1] in memo!!.Category16.split(" ")) || (memo!!.Category16.split(" ")[0] == "1")) { check_flag.add("c16") } }
                    }


                    var set_all = all_flag.distinct()
                    var set_check = check_flag.distinct()



                    if (set_all.size == set_check.size) {


                        if ("3" in memo!!.Category2.split(" ") ||
                            "4" in memo!!.Category2.split(" ") ||
                            "5" in memo!!.Category2.split(" ") ||
                            "6" in memo!!.Category2.split(" ") ||
                            "7" in memo!!.Category2.split(" ") ||
                            "3" in memo!!.Category5.split(" ") ||
                            "4" in memo!!.Category5.split(" ") ||
                            "5" in memo!!.Category5.split(" ") ||
                            "6" in memo!!.Category5.split(" ") ||
                            "7" in memo!!.Category5.split(" ") ||
                            "8" in memo!!.Category5.split(" ") ||
                            "9" in memo!!.Category5.split(" ") ||
                            "4" in memo!!.Category6.split(" ") ||
                            "5" in memo!!.Category6.split(" ") ||
                            "6" in memo!!.Category6.split(" ") ) {

                            policyList.add(
                                PolicyItem(
                                    ContextCompat.getDrawable(context!!.applicationContext, R.drawable.recommend_default_blue)!!, memo!!.Policy, memo!!.Content, memo!!.Link, memo!!.View, memo!!.Keyword, memo!!.Date, memo!!.Review, memo!!.Score, memo!!.Category1, memo!!.Category2, memo!!.Category3, memo!!.Category4, memo!!.Category5, memo!!.Category6, memo!!.Category7, memo!!.Category8, memo!!.Category9, memo!!.Category10, memo!!.Category11, memo!!.Category12, memo!!.Category13, memo!!.Category14, memo!!.Category15, memo!!.Category16, memo!!.Value, memo!!.D_day
                                )
                            )
                            val adapter = PolicyAdapter(policyList)
                            policyRecyclerView.adapter = adapter
                            policyRecyclerView.setItemViewCacheSize((200000000))
                        }
                    }
                }
                    //Toast.makeText(context!!.applicationContext, "총 "+policyList.size.toString()+"개의 추천 정책을 찾았습니다.", Toast.LENGTH_SHORT).show()
            }
        })
    }
}