This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DemoActivity.kt
79 lines (63 loc) · 2.13 KB
/
DemoActivity.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
package io.kolumbus.demo
import android.app.Activity
import android.os.Bundle
import io.kolumbus.Kolumbus
import io.kolumbus.demo.model.Category
import io.kolumbus.demo.model.Product
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmList
import java.util.Random
class DemoActivity : Activity() {
private val CATEGORIES_COUNT = 100
private val MAX_LINKED_CATEGORIES = 50
private val PRODUCTS_COUNT = 1000
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(realmConfiguration)
this.fillDatabase()
with(Kolumbus) {
explore(realmConfiguration)
navigate(this@DemoActivity)
}
}
private fun fillDatabase() {
with(Realm.getDefaultInstance()) {
executeTransaction {
val random = Random()
val categoriesCount = this.where(Category::class.java).count()
val productsCount = this.where(Product::class.java).count()
for (i in 1..CATEGORIES_COUNT) {
with(createObject(Category::class.java, (categoriesCount + i).toInt())) {
color = random.nextColor()
name = "Category $id"
}
}
for (i in 1..PRODUCTS_COUNT) {
with(createObject(Product::class.java, (productsCount + i).toInt())) {
categories = RealmList<Category>()
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)) {
(categories as RealmList<Category>).add(category)
}
}
description = "<b>Lorem ipsum</b> dolor sit amet, <i>consectetur adipiscing</i> elit. <b><u>Maecenas mollis</u></b> eget nibh et condimentum."
name = "Product $id"
}
}
}
close()
}
}
}
fun Random.nextColor(): String {
fun Random.nextColorHex(): String {
return Integer.toHexString(this.nextInt(256)).padStart(2, '0')
}
return "#${this.nextColorHex()}${this.nextColorHex()}${this.nextColorHex()}"
}