|
| 1 | +package com.devkor.kodaero |
| 2 | + |
| 3 | +import android.app.Dialog |
| 4 | +import android.content.Context |
| 5 | +import android.graphics.Color |
| 6 | +import android.graphics.drawable.ColorDrawable |
| 7 | +import android.os.Bundle |
| 8 | +import android.text.Editable |
| 9 | +import android.text.TextWatcher |
| 10 | +import android.view.Gravity |
| 11 | +import android.widget.* |
| 12 | +import retrofit2.Call |
| 13 | +import retrofit2.Callback |
| 14 | +import retrofit2.Response |
| 15 | + |
| 16 | +class AddCategoryDialog(context: Context) : Dialog(context) { |
| 17 | + |
| 18 | + private var onCategoryAddListener: ((String, String) -> Unit)? = null |
| 19 | + private var selectedColor: String = "red" |
| 20 | + private var selectedButton: ImageButton? = null |
| 21 | + |
| 22 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 23 | + super.onCreate(savedInstanceState) |
| 24 | + setContentView(R.layout.dialog_add_category) |
| 25 | + |
| 26 | + |
| 27 | + // Dialog의 배경을 투명하게 설정 |
| 28 | + window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) |
| 29 | + |
| 30 | + val addButton = findViewById<Button>(R.id.add_button) |
| 31 | + val categoryInput = findViewById<EditText>(R.id.category_input) |
| 32 | + |
| 33 | + categoryInput.gravity = Gravity.CENTER |
| 34 | + |
| 35 | + categoryInput.setOnFocusChangeListener { _, hasFocus -> |
| 36 | + if (hasFocus) { |
| 37 | + categoryInput.gravity = Gravity.CENTER |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + categoryInput.addTextChangedListener(object : TextWatcher { |
| 42 | + override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} |
| 43 | + |
| 44 | + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} |
| 45 | + |
| 46 | + override fun afterTextChanged(s: Editable?) { |
| 47 | + if (s?.length == 0) { |
| 48 | + categoryInput.gravity = Gravity.CENTER |
| 49 | + } else { |
| 50 | + categoryInput.gravity = Gravity.START or Gravity.CENTER_VERTICAL |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + |
| 56 | + // Initialize color buttons |
| 57 | + val colorButtons = mapOf( |
| 58 | + "red" to findViewById<ImageButton>(R.id.color_red), |
| 59 | + "orange" to findViewById<ImageButton>(R.id.color_orange), |
| 60 | + "yellow" to findViewById<ImageButton>(R.id.color_yellow), |
| 61 | + "green" to findViewById<ImageButton>(R.id.color_green), |
| 62 | + "blue" to findViewById<ImageButton>(R.id.color_blue), |
| 63 | + "purple" to findViewById<ImageButton>(R.id.color_purple), |
| 64 | + "pink" to findViewById<ImageButton>(R.id.color_pink) |
| 65 | + ) |
| 66 | + |
| 67 | + // Set default selected button |
| 68 | + selectedButton = colorButtons[selectedColor] |
| 69 | + selectedButton?.setBackgroundResource(R.drawable.icon_circle_red_selected) // 기본 선택된 상태로 설정 |
| 70 | + |
| 71 | + // Handle color button clicks |
| 72 | + for ((color, button) in colorButtons) { |
| 73 | + button.setOnClickListener { |
| 74 | + selectedButton?.setBackgroundResource(getUnselectedDrawable(selectedColor)) // 이전 선택된 버튼 초기화 |
| 75 | + selectedColor = color |
| 76 | + selectedButton = button |
| 77 | + button.setBackgroundResource(getSelectedDrawable(color)) // 선택된 버튼으로 변경 |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + addButton.setOnClickListener { |
| 82 | + val category = categoryInput.text.toString() |
| 83 | + |
| 84 | + if (category.isNotEmpty()) { |
| 85 | + onCategoryAddListener?.invoke(category, selectedColor) |
| 86 | + sendCategoryToServer(category, selectedColor) // API 요청 |
| 87 | + dismiss() |
| 88 | + } else { |
| 89 | + Toast.makeText(context, "모든 필드를 입력하세요.", Toast.LENGTH_SHORT).show() |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fun setOnCategoryAddListener(listener: (String, String) -> Unit) { |
| 95 | + onCategoryAddListener = listener |
| 96 | + } |
| 97 | + |
| 98 | + // API 요청을 보내는 메서드 |
| 99 | + private fun sendCategoryToServer(category: String, color: String) { |
| 100 | + val apiService = RetrofitClient.instance |
| 101 | + val accessToken = TokenManager.getAccessToken() |
| 102 | + |
| 103 | + |
| 104 | + val requestData = CategoryItemRequest(category, color, "") // 데이터 객체 생성 |
| 105 | + |
| 106 | + if (accessToken != null) { |
| 107 | + apiService.addCategory("$accessToken", requestData).enqueue(object : Callback<ApiResponse<Any>> { |
| 108 | + override fun onResponse(call: Call<ApiResponse<Any>>, response: Response<ApiResponse<Any>>) { |
| 109 | + if (response.isSuccessful) { |
| 110 | + Toast.makeText(context, "카테고리가 추가되었습니다.", Toast.LENGTH_SHORT).show() |
| 111 | + } else { |
| 112 | + Toast.makeText(context, "카테고리 추가에 실패했습니다: ${response.code()}", Toast.LENGTH_SHORT).show() |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + override fun onFailure(call: Call<ApiResponse<Any>>, t: Throwable) { |
| 117 | + Toast.makeText(context, "네트워크 오류: ${t.message}", Toast.LENGTH_SHORT).show() |
| 118 | + } |
| 119 | + }) |
| 120 | + } else { |
| 121 | + Toast.makeText(context, "액세스 토큰이 없습니다.", Toast.LENGTH_SHORT).show() |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + // 선택된 버튼의 드로어블을 반환하는 메서드 |
| 127 | + private fun getSelectedDrawable(color: String): Int { |
| 128 | + return when (color) { |
| 129 | + "red" -> R.drawable.icon_circle_red_selected |
| 130 | + "orange" -> R.drawable.icon_circle_orange_selected |
| 131 | + "yellow" -> R.drawable.icon_circle_yellow_selected |
| 132 | + "green" -> R.drawable.icon_circle_green_selected |
| 133 | + "blue" -> R.drawable.icon_circle_blue_selected |
| 134 | + "purple" -> R.drawable.icon_circle_purple_selected |
| 135 | + "pink" -> R.drawable.icon_circle_pink_selected |
| 136 | + else -> R.drawable.icon_circle_red_selected |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // 선택되지 않은 버튼의 드로어블을 반환하는 메서드 |
| 141 | + private fun getUnselectedDrawable(color: String): Int { |
| 142 | + return when (color) { |
| 143 | + "red" -> R.drawable.icon_circle_red |
| 144 | + "orange" -> R.drawable.icon_circle_orange |
| 145 | + "yellow" -> R.drawable.icon_circle_yellow |
| 146 | + "green" -> R.drawable.icon_circle_green |
| 147 | + "blue" -> R.drawable.icon_circle_blue |
| 148 | + "purple" -> R.drawable.icon_circle_purple |
| 149 | + "pink" -> R.drawable.icon_circle_pink |
| 150 | + else -> R.drawable.icon_circle_red |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments