Skip to content

Commit

Permalink
🐛 Fix CurToList and other models
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jul 4, 2020
1 parent 2006d62 commit 7ed2c06
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.72"

}
56 changes: 38 additions & 18 deletions app/src/main/java/com/kyhsgeekcode/dereinfo/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import androidx.core.database.getBlobOrNull
import androidx.core.database.getFloatOrNull
import androidx.core.database.getIntOrNull
import androidx.core.database.getStringOrNull
import androidx.core.text.isDigitsOnly
import java.io.*
import kotlin.math.abs
import kotlin.math.roundToInt
import kotlin.reflect.KParameter
import kotlin.reflect.full.declaredMembers


fun launchActivity(context: Context, target: Class<out Activity>) {
Expand Down Expand Up @@ -85,40 +88,57 @@ fun Float.equalsDelta(other: Float) = abs(this - other) < 0.000001
inline fun <reified T> cur2List(cursor: Cursor): List<T> {
val converters = arrayOf(
Cursor::getStringOrNull,
Cursor::getIntOrNull, Cursor::getStringOrNull, Cursor::getFloatOrNull,
Cursor::getIntOrNull, Cursor::getFloatOrNull, Cursor::getStringOrNull,
Cursor::getBlobOrNull
)
val resultList = ArrayList<T>()
cursor.moveToFirst()
val nameToParmaeter =
T::class.constructors.toTypedArray()[0].parameters.associateBy({ it.name }, { it })
val totalColumn = cursor.columnCount
val iToParameter = HashMap<Int, KParameter>()
for (i in 0 until totalColumn) {
iToParameter[i] = nameToParmaeter[cursor.getColumnName(i) ?: ""] ?: return emptyList()
}
while (!cursor.isAfterLast) {
val totalColumn: Int = cursor.columnCount
val arr = ArrayList<Any?>()
val paramToVal = HashMap<KParameter, Any?>()
for (i in 0 until totalColumn) {
if (cursor.getColumnName(i) != null) {
try {
arr.add(converters[cursor.getType(i)](cursor, i))
} catch (e: Exception) {
Log.d("CursorToJson", e.message)
}
}
try {
val theObject: T =
T::class.java.constructors[0].newInstance(*arr.toTypedArray()) as T
resultList.add(theObject)
paramToVal[iToParameter[i]!!] = converters[cursor.getType(i)](cursor, i)
} catch (e: Exception) {
Log.e("Cur", "Error $i", e)
Log.d("CursorToJson", e.message)
}
}
Log.w(
"CurToList",
"paramToVal is " + paramToVal.entries.joinToString("/") { "${it.component1().name} = ${it.component2()}" })
try {
val theObject: T =
T::class.constructors.toTypedArray()[0].callBy(paramToVal)
resultList.add(theObject)
} catch (e: Exception) {
Log.e("Cur", "Error ", e)
}
cursor.moveToNext()
}
cursor.close()
return resultList
}

inline fun <reified T> queryToList(database : SQLiteDatabase, table: String, selection: String? = null, params: Array<String>?=null) : List<T> {
val fields : Array<String> = T::class.java.fields.map { it.name }.toTypedArray()
inline fun <reified T> queryToList(
database: SQLiteDatabase,
table: String,
selection: String? = null,
params: Array<String>? = null
): List<T> {
Log.d("QueryToList", "QueryToList Test 1")
val fields /*: Array<String>*/ = T::class.members.map { it.name }.filter { it ->
!(it.startsWith("component") && it.replace("component", "")
.isDigitsOnly() || arrayOf("copy", "equals", "hashCode", "toString").contains(it))
}.toTypedArray()
Log.d("QueryToList", "${fields.joinToString(",")}")
val cursor = database.query(table, fields, selection, params, null, null, null)
cursor.use { cursor ->
return cur2List(cursor)
cursor.use {
return cur2List(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data class CardModel(
val chara_id: Int,
val rarity: Int,
val attribute: Int,
val titleFlag: Int,
val title_flag: Int,
val evolution_id: Int,
val series_id: Int,
val pose: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ data class LeaderSkillModel(
val name: String,
val explain: String,
val type: Int, // 20: appeal 30: present, 40: fan 50: cross 60: unizon 70: resonance 80: yell
val need_cute: Boolean,
val need_cool: Boolean,
val need_passion: Boolean,
val need_cute: Int,
val need_cool: Int,
val need_passion: Int,
val target_attribute: Int, // cu co pa
val target_param: Int, // vo da vi
val up_type: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.kyhsgeekcode.dereinfo.cardunit

data class SkillModel(
val id: Int,
val name: String,
val skill_name: String,
val explain: String,
val skill_type: Int,
val judge_type: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import com.kyhsgeekcode.dereinfo.*
import com.kyhsgeekcode.dereinfo.cardunit.CardModel
import com.kyhsgeekcode.dereinfo.cardunit.LeaderSkillModel
import com.kyhsgeekcode.dereinfo.cardunit.SkillBoostModel
import com.kyhsgeekcode.dereinfo.cardunit.SkillModel
import com.kyhsgeekcode.dereinfo.model.CircleType.Companion.getColor
import java.io.File
Expand Down Expand Up @@ -43,7 +44,7 @@ class DereDatabaseHelper(context: Context) {
} catch (e: Exception) {
searchMainDB()
}
// initSkillAndLeaderSkillData()
initSkillAndLeaderSkillData()
}

private fun searchMainDB() {
Expand Down Expand Up @@ -746,17 +747,19 @@ class DereDatabaseHelper(context: Context) {

//
private fun initSkillAndLeaderSkillData() {
Log.d(TAG, "QueryToList Test start")
val fumensDB =
SQLiteDatabase.openDatabase(fumensDBFile.path, null, SQLiteDatabase.OPEN_READONLY)
cardModels = queryToList(fumensDB, "card_data")
skillModels = queryToList(fumensDB, "skill_data")
leaderSkillModels = queryToList(fumensDB, "leader_skill_data")
skillBoostModels = queryToList(fumensDB, "skill_boost_type")
Log.d(TAG, "QueryToList Test end")
}

lateinit var cardModels: List<CardModel>
lateinit var skillModels: List<SkillModel>
lateinit var leaderSkillModels: List<LeaderSkillModel>
lateinit var skillBoostModels: List<LeaderSkillModel>
lateinit var skillBoostModels: List<SkillBoostModel>
}

0 comments on commit 7ed2c06

Please sign in to comment.