-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
247 lines (202 loc) · 9.82 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.example.materialregistration
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var request: Request
enum class StorageArea(val displayName: String) {
FREEZER("냉동실"),
FRIDGE("냉장실"),
ROOM_TEMP("실온")
}
data class Ingredients(
val name: String,
val category: String,
val storageArea: StorageArea,
val count: Int,
val expirationDate: String
)
class MyApiClient {
private val client = OkHttpClient()
fun sendRequest(url: String, callback: Callback) {
val request = Request.Builder()
.addHeader("jwt", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIzIiwibmlja25hbWUiOiJ0ZXN0IiwiaWF0IjoxNjg2MjIyODM1LCJleHAiOjE2ODg4MTQ4MzV9.C3gcVa22eGBigQkGQZidRFL_isRlp8IhZKKmynH67XU")
.url(url)
.build()
client.newCall(request).enqueue(callback)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setBackgroundDrawable(getDrawable(R.color.carrot))
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val underlineView = findViewById<View>(R.id.underline_view)
val categoryVegetable = findViewById<TextView>(R.id.category_vegetable)
val subcategoryLayoutVegetableGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_vegetable_group)
val categoryFruit = findViewById<TextView>(R.id.category_fruit)
val subcategoryLayoutFruitGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_fruit_group)
val categoryMeat = findViewById<TextView>(R.id.category_meat)
val subcategoryLayoutMeatGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_meat_group)
val categorySeafood = findViewById<TextView>(R.id.category_seafood)
val subcategoryLayoutSeaFoodGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_seafood_group)
val categoryBeverage = findViewById<TextView>(R.id.category_Beverage)
val subcategoryLayoutBeverageGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_beverage_group)
val categoryAlcohol = findViewById<TextView>(R.id.category_Alcohol)
val subcategoryLayoutAlcoholGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_alcohol_group)
val categorySeasoning = findViewById<TextView>(R.id.category_Seasoning)
val subcategoryLayoutSeasoningGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_seasoning_group)
val categorySpice = findViewById<TextView>(R.id.category_Spice)
val subcategoryLayoutSpiceGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_spice_group)
val categoryDish = findViewById<TextView>(R.id.category_Dish)
val subcategoryLayoutDishGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_dish_group)
val categoryMilkProduct = findViewById<TextView>(R.id.category_MilkProduct)
val subcategoryLayoutMilkProductGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_milkproduct_group)
val categoryBakery = findViewById<TextView>(R.id.category_Bakery)
val subcategoryLayoutBakeryGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_bakery_group)
val categoryDessert = findViewById<TextView>(R.id.category_Dessert)
val subcategoryLayoutDessertGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_dessert_group)
val categoryNuts = findViewById<TextView>(R.id.category_Nuts)
val subcategoryLayoutNutsGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_nuts_group)
val categoryGrains = findViewById<TextView>(R.id.category_Grains)
val subcategoryLayoutGrainsGroup =
findViewById<LinearLayout>(R.id.subcategory_layout_grains_group)
val categoryOther2 = findViewById<TextView>(R.id.category_Other2)
val subcategoryLayoutOther2Group =
findViewById<LinearLayout>(R.id.subcategory_layout_other2_group)
underlineView.visibility = View.INVISIBLE
subcategoryLayoutVegetableGroup.visibility = View.INVISIBLE
subcategoryLayoutFruitGroup.visibility = View.INVISIBLE
subcategoryLayoutMeatGroup.visibility = View.INVISIBLE
subcategoryLayoutSeaFoodGroup.visibility = View.INVISIBLE
subcategoryLayoutBeverageGroup.visibility = View.INVISIBLE
subcategoryLayoutAlcoholGroup.visibility = View.INVISIBLE
subcategoryLayoutSeasoningGroup.visibility = View.INVISIBLE
subcategoryLayoutSpiceGroup.visibility = View.INVISIBLE
subcategoryLayoutDishGroup.visibility = View.INVISIBLE
subcategoryLayoutMilkProductGroup.visibility = View.INVISIBLE
subcategoryLayoutBakeryGroup.visibility = View.INVISIBLE
subcategoryLayoutDessertGroup.visibility = View.INVISIBLE
subcategoryLayoutNutsGroup.visibility = View.INVISIBLE
subcategoryLayoutGrainsGroup.visibility = View.INVISIBLE
subcategoryLayoutOther2Group.visibility = View.INVISIBLE
val categories = listOf(
categoryVegetable, categoryFruit, categoryMeat, categorySeafood,
categoryBeverage, categoryAlcohol, categorySeasoning, categorySpice,
categoryDish, categoryMilkProduct, categoryBakery, categoryDessert,
categoryNuts, categoryGrains, categoryOther2
)
val subcategoryLayouts = listOf(
subcategoryLayoutVegetableGroup,
subcategoryLayoutFruitGroup,
subcategoryLayoutMeatGroup,
subcategoryLayoutSeaFoodGroup,
subcategoryLayoutBeverageGroup,
subcategoryLayoutAlcoholGroup,
subcategoryLayoutSeasoningGroup,
subcategoryLayoutSpiceGroup,
subcategoryLayoutDishGroup,
subcategoryLayoutMilkProductGroup,
subcategoryLayoutBakeryGroup,
subcategoryLayoutDessertGroup,
subcategoryLayoutNutsGroup,
subcategoryLayoutGrainsGroup,
subcategoryLayoutOther2Group
)
categories.forEachIndexed { index, category ->
category.setOnClickListener {
underlineView.visibility = View.VISIBLE
subcategoryLayouts.forEachIndexed { layoutIndex, layout ->
layout.visibility = if (layoutIndex == index) View.VISIBLE else View.GONE
}
}
}
subcategoryLayouts.forEachIndexed { index, layout ->
layout.setOnClickListener {
val selectedIngredientTextView = categories[index]
val selectedIngredient = selectedIngredientTextView.text?.toString() ?: ""
val category = selectedIngredientTextView.text?.toString() ?: ""
selectedIngredientTextView.contentDescription = category // 카테고리 정보를 contentDescription에 설정
val intent = Intent(this, NextActivity::class.java)
intent.putExtra("ingredient", selectedIngredient)
intent.putExtra("category", category)
intent.putExtra("index", index) // Add index as an extra parameter
startActivity(intent)
}
}
val myApiClient = MyApiClient()
val url = "https://mocum-project-gmck.vercel.app/api/recipe"
val jsonString = readJsonFromFile("addMaterial.json")
val mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody: RequestBody = jsonString?.toRequestBody(mediaType) ?: "".toRequestBody(mediaType)
request = Request.Builder()
.url(url)
.post(requestBody)
.build()
myApiClient.sendRequest(url, object : Callback {
override fun onResponse(call: Call, response: Response) {
// 상태 코드 확인
val statusCode = response.code
Log.d("API Response", "Status code: $statusCode")
// 응답 처리
val responseBody = response.body?.string()
if (responseBody != null) {
Log.d("API Response", responseBody)
}
}
override fun onFailure(call: Call, e: IOException) {
// 요청 실패 처리
e.printStackTrace()
}
})
}
override fun onSupportNavigateUp(): Boolean {
if (isTaskRoot) {
// 현재 Activity가 앱의 첫 화면이 아닌 경우에만 이전 화면으로 돌아감
onBackPressed()
} else {
// 앱의 첫 화면인 경우에는 현재 Activity를 종료하여 이전 화면으로 돌아감
finish()
}
return true
}
private fun readJsonFromFile(fileName: String): String? {
return try {
val inputStream = assets.open(fileName)
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
inputStream.close()
String(buffer)
} catch (e: IOException) {
e.printStackTrace()
null
}
}
}