Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Minor chantes
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jun 21, 2017
1 parent 2b98313 commit 09abea8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/io/kolumbus/demo/DemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DemoActivity : Activity() {
with(createObject(Product::class.java, (productsCount + i).toInt())) {
categories = RealmList<Category>()

for (j in 0..(random.nextInt(MAX_LINKED_CATEGORIES) - 1)) {
for (j in 0 until random.nextInt(MAX_LINKED_CATEGORIES)) {
val category = where(Category::class.java).equalTo("id", random.nextInt(CATEGORIES_COUNT - 1) + 1).findFirst()

if (!(categories as RealmList<Category>).contains(category)) {
Expand Down
42 changes: 18 additions & 24 deletions kolumbus/src/main/kotlin/io/kolumbus/adapter/TableAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,19 @@ class TableAdapter(val entries: List<RealmModel>, val fields: List<Field>, val m
this.processField(holder) { fieldView, field ->
val result = this.methods[field.name]?.invoke(entry)

if (result is Boolean) {
Kolumbus.architect.displayBoolean(fieldView, result)
} else if (result is Float) {
Kolumbus.architect.displayFloat(fieldView, result)
} else if (result is Int) {
Kolumbus.architect.displayInt(fieldView, result)
} else if (result is RealmList<*>) {
val resultCasted = result as RealmList<RealmModel>
val returnType = field.genericType as ParameterizedType
val type = returnType.actualTypeArguments[0] as Class<RealmModel>

Kolumbus.architect.displayRealmList(fieldView, resultCasted, type)
} else if (result is RealmModel) {
Kolumbus.architect.displayRealmModel(fieldView, result)
} else if (result is String) {
if (result.isEmpty()) {
when {
result is Boolean -> Kolumbus.architect.displayBoolean(fieldView, result)
result is Float -> Kolumbus.architect.displayFloat(fieldView, result)
result is Int -> Kolumbus.architect.displayInt(fieldView, result)
result is RealmList<*> -> {
val resultCasted = result as RealmList<RealmModel>
val returnType = field.genericType as ParameterizedType
val type = returnType.actualTypeArguments[0] as Class<RealmModel>

Kolumbus.architect.displayRealmList(fieldView, resultCasted, type)
}
result is RealmModel -> Kolumbus.architect.displayRealmModel(fieldView, result)
result is String -> if (result.isEmpty()) {
Kolumbus.architect.displayEmpty(fieldView)
} else {
if (Patterns.WEB_URL.matcher(result).matches()) {
Expand All @@ -96,20 +93,17 @@ class TableAdapter(val entries: List<RealmModel>, val fields: List<Field>, val m
}
}
}
} else if (result != null) {
Kolumbus.architect.displayAny(fieldView, result)
} else {
Kolumbus.architect.displayNull(fieldView)
result != null -> Kolumbus.architect.displayAny(fieldView, result)
else -> Kolumbus.architect.displayNull(fieldView)
}
}
}

private fun bindHeaderRow(holder: ViewHolder) {
this.processField(holder) { fieldView, field ->
fieldView.text = if (field.isAnnotationPresent(PrimaryKey::class.java)) {
"#${field.name.prettify()}"
} else {
field.name.prettify()
fieldView.text = when {
field.isAnnotationPresent(PrimaryKey::class.java) -> "#${field.name.prettify()}"
else -> field.name.prettify()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class TableInfoAdapter(val fields: List<Field>, val instance: RealmModel) : Recy
val genericType = field.genericType
val value = field.get(this.instance)

holder.name.text = if (field.isAnnotationPresent(PrimaryKey::class.java)) {
"#${field.name}"
} else {
field.name
}
holder.name.text = if (field.isAnnotationPresent(PrimaryKey::class.java)) "#${field.name}" else field.name

holder.type.text = if (genericType is ParameterizedType) {
val subType = genericType.actualTypeArguments[0] as Class<Any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ internal fun String.removeAccessorPrefixes(): String = ACCESSOR_PREFIXES.replace
internal fun String.toCamelCase(): String {
val camelCased = CAMEL_CASE.replace(this, "$1 $2")

return if (camelCased.isEmpty()) {
camelCased
} else {
camelCased.first().toUpperCase() + camelCased.drop(1)
return when {
camelCased.isEmpty() -> camelCased
else -> camelCased.first().toUpperCase() + camelCased.drop(1)
}
}
36 changes: 17 additions & 19 deletions kolumbus/src/main/kotlin/io/kolumbus/layout/TableLayoutManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,30 @@ class TableLayoutManager(context: Context) : LinearLayoutManager(context) {
val size = Point(0, 0)

// Compute the biggest dimension from all the visible items
this.processChild(0, this.itemCount) { index, child ->
child.measure(0, 0)
this.processChild(0, this.itemCount) {
it.measure(0, 0)

size.x = Math.max(size.x, child.measuredWidth)
size.y = Math.max(size.y, child.measuredHeight)
size.x = Math.max(size.x, it.measuredWidth)
size.y = Math.max(size.y, it.measuredHeight)
}

// Set the proper size on all the visible items
this.processChild(0, state?.itemCount ?: 0) { index, child ->
child.layoutParams.height = size.y
child.layoutParams.width = size.x
child.post {
child.requestLayout()
this.processChild(0, state?.itemCount ?: 0) {
it.layoutParams.height = size.y
it.layoutParams.width = size.x
it.post {
it.requestLayout()
}
}
}

private fun processChild(from: Int, to: Int, callback: (index: Int, child: View) -> Unit) {
for (rowIndex in from..to) {
val row = this.getChildAt(rowIndex) as ViewGroup? ?: continue

for (childIndex in 0..(row.childCount - 1)) {
val child = row.getChildAt(childIndex)

callback(childIndex, child)
}
}
private fun processChild(from: Int, to: Int, callback: (child: View) -> Unit) {
(from..to)
.mapNotNull { this.getChildAt(it) as ViewGroup? }
.forEach {
(0 until it.childCount)
.map(it::getChildAt)
.forEach(callback)
}
}
}

0 comments on commit 09abea8

Please sign in to comment.