|
@@ -1,5 +1,7 @@
|
|
|
package com.develop.main.ui
|
|
|
|
|
|
+import android.content.Context
|
|
|
+import android.content.res.AssetManager
|
|
|
import android.os.Bundle
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.ViewGroup
|
|
@@ -7,6 +9,7 @@ import android.widget.ImageView
|
|
|
import androidx.appcompat.widget.AppCompatTextView
|
|
|
import androidx.recyclerview.widget.LinearSnapHelper
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
+import com.develop.airfryer.viewmodel.WorkMode
|
|
|
import com.develop.base.ext.load
|
|
|
import com.develop.base.ext.navigateTo
|
|
|
import com.develop.base.ext.resId2Dimension
|
|
@@ -24,8 +27,12 @@ import com.develop.main.databinding.FragmentModesBinding
|
|
|
import com.develop.main.viewmodel.HomeViewModel
|
|
|
import com.drake.brv.utils.linear
|
|
|
import com.drake.brv.utils.setup
|
|
|
+import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevModes
|
|
|
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
|
|
|
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
|
|
|
+import org.json.JSONObject
|
|
|
+import java.io.IOException
|
|
|
+import java.nio.charset.Charset
|
|
|
import kotlin.math.abs
|
|
|
|
|
|
/**
|
|
@@ -53,18 +60,56 @@ class ModesFragment : CommonBVMFragment<FragmentModesBinding, HomeViewModel>() {
|
|
|
initView()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ fun loadJSONFromAsset(context: Context, fileName: String): JSONObject? {
|
|
|
+ return try {
|
|
|
+ val inputStream = context.assets.open(fileName)
|
|
|
+ val size = inputStream.available()
|
|
|
+ val buffer = ByteArray(size)
|
|
|
+ inputStream.read(buffer)
|
|
|
+ inputStream.close()
|
|
|
+
|
|
|
+ val jsonString = String(buffer, Charset.defaultCharset())
|
|
|
+ JSONObject(jsonString)
|
|
|
+ } catch (ex: IOException) {
|
|
|
+ ex.printStackTrace()
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private fun initView() {
|
|
|
+
|
|
|
+ val configJson = this.context?.let { loadJSONFromAsset(it, "config.json") }
|
|
|
+ val workModes = configJson?.getJSONArray( if (CofarSDK.devInfo().devMode == DevModes.SOUP) "soupWorkModes" else "airFryerWorkModes");
|
|
|
+ var modes = ArrayList<WorkMode>();
|
|
|
+ val len = workModes?.length();
|
|
|
+ for (i in 0 until len!!) {
|
|
|
+ val element = workModes.getJSONObject(i)
|
|
|
+ // 处理数组元素,例如打印它们
|
|
|
+ var model = WorkMode(name = element.getString("name"), icon = element.getString("icon"), bg = element.getString("bg"), type = element.getString("type"))
|
|
|
+ modes.add(model)
|
|
|
+ }
|
|
|
+
|
|
|
modeTypeList = DataFactory.modesTypeList
|
|
|
binding.galleryRecycle.apply {
|
|
|
linear(RecyclerView.HORIZONTAL)
|
|
|
setup {
|
|
|
- addType<ModelsModel>(R.layout.item_mode_card_view)
|
|
|
+ addType<WorkMode>(R.layout.item_mode_card_view)
|
|
|
onBind {
|
|
|
- val model = getModel<ModelsModel>()
|
|
|
- findView<ImageView>(R.id.iv_icon).load(model.resId)
|
|
|
- findView<AppCompatTextView>(R.id.tv_mode_name).updateText(
|
|
|
- model.modeName
|
|
|
- )
|
|
|
+ val model = getModel<WorkMode>()
|
|
|
+ findView<ImageView>(R.id.iv_icon).load(resources.getIdentifier(model.icon,"drawable","com.develop.foodcooking"))
|
|
|
+ model.name?.let { it1 ->
|
|
|
+ findView<AppCompatTextView>(R.id.tv_mode_name).updateText(
|
|
|
+ resources.getString(resources.getIdentifier(it1,"string","com.develop.foodcooking"))
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
R.id.card_view.onClick {
|
|
|
|
|
@@ -85,7 +130,7 @@ class ModesFragment : CommonBVMFragment<FragmentModesBinding, HomeViewModel>() {
|
|
|
with(bundle)
|
|
|
}
|
|
|
}
|
|
|
- }.models = DataFactory.genGalleryModesList(resources)
|
|
|
+ }.models = modes
|
|
|
// 让item居中显示
|
|
|
val snapHelper = LinearSnapHelper()
|
|
|
// 绑定到 mRecyclerView
|