android 如何在此扩展函数中返回正确的类型

gopyfrb3  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(130)

我知道我可能做错了,但是我在这个函数中返回正确的类型时有点卡住了。

val dbFields = dbInfo.equipmentList

        fun DbEquipmentWithMediaList.toApiUpdateDeviceList(): ApiUpdateDeviceList {
            val newUpdate: ApiUpdateDeviceList
           newUpdate = dbFields.forEach { ApiUpdateDeviceList(
                    deviceEquipmentId = it.sSEquipmentDbInfo.deviceEquipmentId,
                    secondaryDeviceId = it.sSEquipmentDbInfo.secondaryDeviceId,
                    deviceInstanceId = it.sSEquipmentDbInfo.deviceInstanceId.toString(),
                    deviceSerialNumber = it.sSEquipmentDbInfo.deviceSerialNumber,
                    devicePartNumber = it.sSEquipmentDbInfo.devicePartNumber,
                    deviceManufacturerName = it.sSEquipmentDbInfo.deviceManufacturer,
                    deviceInstallationDate = DateUtil.dateToStringUTCSiteSurvey(it.sSEquipmentDbInfo.deviceInstallationDate),
                    deviceLocation = it.sSEquipmentDbInfo.locationInfo,
                    deviceTechnicianNotes = it.sSEquipmentDbInfo.deviceTechnicianNotes
            )
            }
        }

我需要在forEach循环后将函数作为ApiUpdateDeviceList返回。感谢您的帮助。
下面是有关DbEquipmentWithMediaList类和ApiUpdateDeviceList这两个类的详细信息
对于具有媒体列表的数据库设备

data class DbEquipmentWithMediaList(
    @Embedded val sSEquipmentDbInfo: SSEquipmentDbInfo,
    @Relation(
        parentColumn = "id",
        entityColumn = "media_id",
        entity = SurveySiteDbMedia::class
    )
    val mediaList: List<SurveySiteDbMedia>
)

data class SSEquipmentDbInfo(
        @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id") val id: Long,
        @ColumnInfo(name = "equipment_id", index = true) val equipmentId: Long,
        @ColumnInfo(name = "actual_device_equipmentId") val deviceEquipmentId: Int?,

        @ColumnInfo(name = "secondary_device_id") val secondaryDeviceId: Int,
        @ColumnInfo(name = "secondary_device_checked_status") val secondaryDeviceCheckedStatus: Int?,
        @ColumnInfo(name = "device_instance_id") val deviceInstanceId: Int?,
        @ColumnInfo(name = "media_names_list") val mediaNames: List<String>? = null,
        @ColumnInfo(name = "device_serial_number") var deviceSerialNumber: String? = null,
        @ColumnInfo(name = "device_part_number") var devicePartNumber: String? = null,
        @ColumnInfo(name = "device_manufacturer") var deviceManufacturer: String? = null,
        @ColumnInfo(name = "device_installation_date") var deviceInstallationDate: Date? = null,
        @ColumnInfo(name = "location") var locationInfo: String? = null,
        @ColumnInfo(name = "device_technician_notes") var deviceTechnicianNotes: String? = null,
        @ColumnInfo(name = "image_start_count") val imageStartCount: Int?,
        @ColumnInfo(name = "video_start_count") val videoStartCount: Int?,
        @ColumnInfo(name = "device_equipment_id") val deviceEquipmentMediaId: Int? = 0,
        @ColumnInfo(name = "device_update_mode") val isDeviceUpdateMode: Boolean
)

对于ApiUpdate设备列表

data class ApiUpdateDeviceList(
        @SerializedName("DeviceEquipmentId") val deviceEquipmentId: Int?,
        @SerializedName("DeviceId") val secondaryDeviceId: Int?,
        @SerializedName("DeviceInstance") val deviceInstanceId: String?,
        @SerializedName("SerialNumber") val deviceSerialNumber: String?,
        @SerializedName("PartNumber") val devicePartNumber: String?,
        @SerializedName("ManufacturerName") val deviceManufacturerName: String?,
        @SerializedName("InstallationDate") val deviceInstallationDate: String?,
        @SerializedName("Location") val deviceLocation: String?,
        @SerializedName("TechnicianNotes") val deviceTechnicianNotes: String?
)
ntjbwcob

ntjbwcob1#

因此,我将重命名该类,使其名称中不包含List,因为它只表示一个项:

ApiUpdateDeviceList -> ApiUpdateDevice

由于这是一对多的关系,所以这个扩展函数的返回类型应该是List<ApiUpdateDevice>
实际上,我会首先将一对一的关系分解成它自己的扩展函数,使用with scope函数来简化代码:

fun SurveySiteDbMedia.toApiUpdateDevice(): ApiUpdateDevice = with(sSEquipmentDbInfo) {
    ApiUpdateDevice(
        deviceEquipmentId = deviceEquipmentId,
        secondaryDeviceId = secondaryDeviceId,
        deviceInstanceId = deviceInstanceId.toString(),
        deviceSerialNumber = deviceSerialNumber,
        devicePartNumber = devicePartNumber,
        deviceManufacturerName = deviceManufacturer,
        deviceInstallationDate = DateUtil.dateToStringUTCSiteSurvey(deviceInstallationDate),
        deviceLocation = locationInfo,
        deviceTechnicianNotes = deviceTechnicianNotes
    )
}

你可以使用map函数代替forEach来从一个List生成另一个List,所以我们将它与上面的函数结合使用来简洁地创建列表转换:

fun DbEquipmentWithMediaList.toApiUpdateDeviceList(): List<ApiUpdateDevice> =
    dbFields.map { it.toApiUpdateDevice() }

相关问题