239일차 - 커뮤니티 앱개발 (7) : 코틀린 게시글 업로드

2021. 8. 25. 23:42Diary/201~300

1. Write Fragment

class WriteFragment : Fragment(), AdapterView.OnItemSelectedListener {

    // 값 전달 변수
    var share_access_token = ""
    var share_username = ""
    var share_nickname = ""
    var share_profile = ""
    var share_user_type = ""
    var share_message = ""

    var temp_board_type:String = "daily"

    companion object{
        fun newInstance() : WriteFragment {
            return WriteFragment()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("TEST","WriteFragment - onCreate")
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d("TEST","WriteFragment - onAttach")
    }

    @SuppressLint("ResourceType")
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d("TEST","WriteFragment - onCreateView")
        val view=inflater.inflate(R.layout.fragment_write, container, false)

        // 값 전달
        val bundle = Bundle()
        val bundle_arguments = arguments
        share_access_token = bundle_arguments?.getString("access_token").toString()
        share_username = bundle_arguments?.getString("username").toString()
        share_nickname = bundle_arguments?.getString("nickname").toString()
        share_profile = bundle_arguments?.getString("profile").toString()
        share_user_type = bundle_arguments?.getString("user_type").toString()
        share_message = bundle_arguments?.getString("share_message").toString()

        Log.d("TEST", "share_access_token : $share_access_token")
        Log.d("TEST", "share_username : $share_username")
        Log.d("TEST", "share_nickname : $share_nickname")
        Log.d("TEST", "share_profile : $share_profile")
        Log.d("TEST", "share_user_type : $share_user_type")
        Log.d("TEST", "share_message : $share_message")

        // API 셋팅
        val access_token = share_access_token
        val conn = Connect().connect(access_token)
        val board_api: BoardInterface = conn.create(BoardInterface::class.java)

        val write_submit_btn = view.findViewById<Button>(R.id.write_submit_btn)

        // 작성 완료 버튼 클릭
        write_submit_btn.setOnClickListener {
            val write_title_input = view.findViewById<TextInputEditText>(R.id.write_title_input).text.toString()
            val write_content_input = view.findViewById<TextInputEditText>(R.id.write_content_input).text.toString()
            view.findViewById<TextInputEditText>(R.id.write_title_input).text!!.clear()
            view.findViewById<TextInputEditText>(R.id.write_content_input).text!!.clear()

            // 키보드 내리기
            val mInputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            mInputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)

            Log.d("TEST", "write_title_input : $write_title_input")
            Log.d("TEST", "write_content_input : $write_content_input")

            if(write_title_input.replace(" ", "") == "" || write_content_input.replace(" ", "") == "") {
                return@setOnClickListener
            }

            // 게시글 생성 API
            val parameter:HashMap<String, String> = HashMap()
            parameter["board_title"] = write_title_input
            parameter["board_content"] = write_content_input
            parameter["board_type"] = temp_board_type
            parameter["board_username"] = share_username
            parameter["board_like_count"] = "0"
            parameter["board_nickname"] = share_nickname
            parameter["board_user_type"] = share_user_type

            board_api.createBoard(parameter).enqueue(object: Callback<CreateBoardData> {
                override fun onResponse(call: Call<CreateBoardData>, response: Response<CreateBoardData>) {
                    val body = response.body()

                    if(body != null) {
                        Snackbar
                            .make(view.findViewById<FrameLayout>(R.id.write_frame_layout), "게시글이 업로드 되었습니다.", 1000)
                            .setBackgroundTint(Color.parseColor("#666666"))
                            .setTextColor(Color.parseColor("#aaffffff"))
                            .show()
                    }

                    Log.d("TEST", "createBoard 통신성공 바디 -> $body")
                }

                override fun onFailure(call: Call<CreateBoardData>, t: Throwable) {
                    Log.d("TEST", "createBoard 통신실패 에러 -> " + t.message)
                }
            })
        }

        val write_select_daily = view.findViewById<TextView>(R.id.write_select_daily)
        val write_select_free = view.findViewById<TextView>(R.id.write_select_free)
        val write_select_question = view.findViewById<TextView>(R.id.write_select_question)
        val write_select_hobby = view.findViewById<TextView>(R.id.write_select_hobby)
        val write_select_job = view.findViewById<TextView>(R.id.write_select_job)
        val write_select_support = view.findViewById<TextView>(R.id.write_select_support)

        write_select_daily.setOnClickListener {
            temp_board_type = "daily"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
        }
        write_select_free.setOnClickListener {
            temp_board_type = "free"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
        }
        write_select_question.setOnClickListener {
            temp_board_type = "question"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
        }
        write_select_hobby.setOnClickListener {
            temp_board_type = "hobby"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
        }
        write_select_job.setOnClickListener {
            temp_board_type = "job"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
        }
        write_select_support.setOnClickListener {
            temp_board_type = "support"
            write_select_daily.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_free.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_question.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_hobby.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_job.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#000000"))
            write_select_support.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#C73279"))
        }

        return view
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    }

    override fun onNothingSelected(parent: AdapterView<*>) {
        // Another interface callback
    }

    override fun onPause() {
        super.onPause()
        Log.d("TEST", "WriteFragment - onPause")
    }

    override fun onResume() {
        super.onResume()
        Log.d("TEST", "WriteFragment - onResume")
    }

    override fun onStop() {
        super.onStop()
        Log.d("TEST", "WriteFragment - onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("TEST", "WriteFragment - onDestroy")
    }
}