RecommendActivity.kt 7.2 KB
package com.example.vip

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Half.toFloat
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.snackbar.Snackbar
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.activity_recommend.*
import kotlinx.android.synthetic.main.activity_signin.*
import kotlinx.android.synthetic.main.activity_signin.toolbar
import kotlinx.android.synthetic.main.item_policy.*
import kotlinx.android.synthetic.main.item_policy.view.*
import kotlinx.android.synthetic.main.search.*

data class MemoItemRecom(
    val Target : String = "",
    val Policy : String = "",
    val Content : String = "",
    val Link : String = ""
)

class RecommendActivity : AppCompatActivity() {

    ////bottom navigation view operation start 1
    lateinit var bottomBar: ActionBar
    ////bottom navigation view operation end 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recommend)

        val policyList = ArrayList<PolicyItem>()
        var recomdatabase = FirebaseDatabase.getInstance().reference

        if (intent.hasExtra("key")) {
            //textView.text = intent.getStringExtra("key") 텍뷰추가하면 인텐트로 값넘기는거 보임

            recomdatabase.orderByChild("Policy").startAt(intent.getStringExtra("key")).endAt(intent.getStringExtra("key")+"\uf8ff").addListenerForSingleValueEvent(object :
                ValueEventListener {
                override fun onCancelled(p0: DatabaseError) {
                    Toast.makeText(this@RecommendActivity, "실패부분", Toast.LENGTH_SHORT).show()
                }
                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    for (memoSnapshot in dataSnapshot.children){
                        val memo = memoSnapshot.getValue(MemoItemRecom::class.java)

                        policyList.add(
                            PolicyItem(
                                ContextCompat.getDrawable(this@RecommendActivity, R.drawable.image01)!!,
                                "상시",
                                memo!!.Policy,
                                memo!!.Policy,
                                4.toFloat(),
                                ""
                            )
                        )
                    }
                    val adapter = PolicyAdapter(policyList)
                    policyRecyclerView.adapter = adapter
                }
            })

        } else {
            Toast.makeText(this, "putExtra value not found", Toast.LENGTH_SHORT).show()

            recomdatabase.orderByChild("Policy").startAt(intent.getStringExtra("\uf8ff")).addListenerForSingleValueEvent(object :
                ValueEventListener {
                override fun onCancelled(p0: DatabaseError) {
                    Toast.makeText(this@RecommendActivity, "실패부분", Toast.LENGTH_SHORT).show()
                }
                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    for (memoSnapshot in dataSnapshot.children){
                        val memo = memoSnapshot.getValue(MemoItemRecom::class.java)

                        policyList.add(
                            PolicyItem(
                                ContextCompat.getDrawable(this@RecommendActivity, R.drawable.image01)!!,
                                "상시",
                                memo!!.Policy,
                                memo!!.Policy,
                                4.toFloat(),
                                ""

                            )
                        )
                    }

                    val adapter = PolicyAdapter(policyList)
                    policyRecyclerView.adapter = adapter
                }
            })
        }
        // 1. 툴바 사용 설정
        setSupportActionBar(toolbar)
        // 2. 툴바 왼쪽 버튼 설정
        supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 왼쪽 버튼 사용 여부 true
        supportActionBar!!.setHomeAsUpIndicator(R.drawable.return_page) // 왼쪽 버튼 아이콘 설정
        supportActionBar!!.setDisplayShowTitleEnabled(false) // 타이틀 안보이게 하기
        ////bottom navigation view operation start 2
        bottomBar=supportActionBar!!
        val bottomNavigation: BottomNavigationView =findViewById(R.id.bottomNavigation)
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        ////bottom navigation view operation end 2
    }
    // 3.툴바 메뉴 버튼을 설정
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main_menu, menu) // main_menu 메뉴를 toolbar 메뉴 버튼으로 설정
        return true
    }
    // 4.툴바 메뉴 버튼이 클릭 됐을 때 콜백
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        // 클릭된 메뉴 아이템의 아이디 마다 when 구절로 클릭시 동작을 설정한다.
        when(item!!.itemId){
            android.R.id.home->{ // 메뉴 버튼
                Snackbar.make(toolbar,"Menu pressed",Snackbar.LENGTH_SHORT).show()
            }
            R.id.menu_search->{ // 검색 버튼
                val search_Intent = Intent(this, SearchActivity::class.java)
                startActivity(search_Intent)
                //Snackbar.make(toolbar,"Search menu pressed",Snackbar.LENGTH_SHORT).show()
            }
        }
        return super.onOptionsItemSelected(item)
    }

    //bottom navigation view operation start 3
    private val mOnNavigationItemSelectedListener=BottomNavigationView.OnNavigationItemSelectedListener{item->
        when (item.itemId){

            R.id.bottomHome ->{
                val intent = Intent(this, SignInActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener true
            }
            R.id.bottomRecommend ->{
                val intent = Intent(this, RecommendActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener true
            }
            R.id.bottomFavorites ->{
                val intent = Intent(this, FavoritesActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener true
            }
            R.id.bottomInfo ->{
                val intent = Intent(this, InfoActivity::class.java)
                startActivity(intent)
                return@OnNavigationItemSelectedListener true
            }


        }
        false
    }
//bottom navigation view operation end 3

}