Skip to content

Commit

Permalink
refactor: support dynamic colors for Blockies library
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljainlive committed Nov 18, 2023
1 parent beb211b commit 29c42be
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class BlockiesGeneratorViewHolder(
override fun bind(blockiesViewsData: BlockiesViewsData) {
itemBinding.apply {
val iconGenerator = BlockiesIconGenerator(
seed = blockiesViewsData.address, size = 10, scale = 10
seed = blockiesViewsData.address,
size = 10,
scale = 10,
color = itemView.context.getColor(blockiesViewsData.color),
bgColor = itemView.context.getColor(blockiesViewsData.bgColor),
spotColor = itemView.context.getColor(blockiesViewsData.spotColor)
)
val context = itemView.context
contactIcon.setImageBitmap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class BlockiesGlideViewHolder(
override fun bind(blockiesViewsData: BlockiesViewsData) {
itemBinding.apply {
val blockiesData = BlockiesIconData(
seed = blockiesViewsData.address, size = 10, scale = 10
seed = blockiesViewsData.address,
size = 10,
scale = 10,
color = itemView.context.getColor(blockiesViewsData.color),
bgColor = itemView.context.getColor(blockiesViewsData.bgColor),
spotColor = itemView.context.getColor(blockiesViewsData.spotColor)
)
Glide.with(contactIcon)
.load(blockiesData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.nikhiljain.blockiesgenerator.sample.blockiesview

import com.nikhiljain.blockiesgenerator.databinding.ListItemBlockiesViewBinding
import com.nikhiljain.blockiesgenerator.sample.common.BlockiesViewsData
import com.nikhiljain.blockiesgenerator.sample.common.BaseBlockiesViewHolder
import com.nikhiljain.blockiesgenerator.sample.common.BlockiesViewsData
import com.nikhiljain.blockiesgenerator.sample.common.StringUtil.shortenAddressString
import com.nikhiljain.blockiesview.BlockiesIconData

class BlockiesViewHolder(
private val itemBinding: ListItemBlockiesViewBinding
) : BaseBlockiesViewHolder(itemBinding.root) {
override fun bind(blockiesViewsData: BlockiesViewsData) {
itemBinding.apply {
contactIcon.setSeed(blockiesViewsData.address, 10)
contactIcon.setBlockiesIconData(
BlockiesIconData(
seed = blockiesViewsData.address,
size = 10,
color = itemView.context.getColor(blockiesViewsData.color),
bgColor = itemView.context.getColor(blockiesViewsData.bgColor),
spotColor = itemView.context.getColor(blockiesViewsData.spotColor)
)
)
contactName.text = blockiesViewsData.name
contactAddress.text = shortenAddressString(blockiesViewsData.address)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.nikhiljain.blockiesgenerator.sample.common

import androidx.annotation.ColorRes

data class BlockiesViewsData(
val name : String,
val address : String
val name: String,
val address: String,
@ColorRes val color: Int = 0,
@ColorRes val bgColor: Int = 0,
@ColorRes val spotColor: Int = 0
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ object DataUtil {
fun getBlockiesData(): List<BlockiesViewsData> {
val addresses =
"1DYEFJDdrsbk4mC4cckyVpYXiYHMsMF5yb,12FiFMbjMedbkovw65WuaSybxVaExHE4FC,15mqYdCW9ddrWMfK9SyXbBUTEBeBRyBcjn,1M7i8Hgopn6757TniGLRGKf5gGx2x9aXEg,1Km3u8rAjUJFABzxndAUD9H7dimjNkKyVw,1CST4CNtKvModKX3RAQVYCXwHkEoWcqst1,17skjGY4TXSUUHvFwq7Ew5fo5HmsjCBuqS,1JndWtfXvo6YMumjZaDA2oqxncbsxSWUP,1DZCVnA6zwCicNsmcHfhvQBp6NJMGk7egg,1BNiAnQgfETQJ324SkPszvpyox8iUaJkTD,159He4o581J45bANJbwmUWfVzGMqS9Ym9v,1PUy4bhjLNs1CAihaeTr4cZjTSjZyqBGr1,193yuyNdbEdq2dH9y8nDZdsegtz7jEWjnm,1ANtoV5Z6rjmxhF8HXHh1oMcYEv5TSeLc4,12A8PhQ8KzWQxTCpFm6iK54KMyFMdBsGFE,1Gk3t3v3d73d9hfr6EKZXVWd6ZoUEMqXf7,1871WvQhEaCeTNjJTJj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XQQ3GrsGz,1FNNsvX7nTCbMWsZCTqXfbwgmeNzwS6EXk,151DH2hBHJMWRzYZPWPAtnruyAQqz61XRR,1GxUWbskETYav6N1t5BgEFUe2RGo3BhXvz,16mV9enyTj8kcTRxpY9u1L1xd81TXkaiGV,1FE39hzHy8eRrhVgr9nCGUUWkcAQgNyB5C,1EcmxY7UfPqiMKUZ3ckMWg2NQBVQfTDJFH,14474zmXzWJNhdHf5zhVU8VnUaPQJmvy54,14KZk12AMNQrTHmjSNDJcoR5HYMAzhovqP,1HUwm6WL8nrrobLQzpYQpdtQjxqTu3R8xw,1LuXJBiA2R9Yr6QAwSPSVriB8JjQkt39kK,192LCobf3z34NuqczE3pHz7cDoT9sGPAkW,1Aqu16TFoHisJziidf89M73jYfLJVMTRwb,13oXgK9KNxkZB75qamuxKtRNM66VerUtEf,1F1Za94p6z1yHgujiMJX6RujwhArbVFKz,1PsBN9MKjUP1EBzgwn1x5po1R3MYZ9Czjp,1F2VamX1TfimkyaFRx5Teoq1E5MTjwmoEW,1K8BUYDTPaTqot7xrqYPsXovvb6FTb3pPj,1NrXcsar3WyiuA8bPtPUKphdMEUj2JWGXP,14hJCrSzHPuY486vPaC3na5aWNt2riaqqS,1PArLcKQvyY9PgM18L45rTbcwhDB96xj1t,18HeE6SY7PKAGE9CZqbBB6SC4m79YS6fGR,1B4s74tRWA2g9s667gm6xgb19x3Xd2swEV,1KovKg8bwhHRecTdSszPpwMQg3FuX4CqVA,1NjwHs5f3PoKertMgHKdJWtYjiH7W2tgWd,1Dpw113c6XfvzKnnQu6HBj7cKKNRcV5qH5,16xKy4zW4mk3RWSBcsnqBGvEqo6NKv5Rc,1M1YwrRDJ2VhpzchKrkY3ywnvDmxSw6BRP,1bZc6Q5a6YZkjYtHuZRBK6SqDPsu3nQ8B"
return addresses.split(",").map { BlockiesViewsData("Ajay", it) }
return addresses.split(",").map {
BlockiesViewsData(
name = "Ajay",
address = it,
color = android.R.color.holo_green_light,
bgColor = android.R.color.holo_blue_bright,
spotColor = android.R.color.holo_orange_dark
)
}
}
}

This file was deleted.

5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<com.nikhiljain.blockiesview.BlockiesIconView
android:layout_width="200dp"
android:layout_height="200dp"
app:bgColor="@android:color/holo_blue_light"
app:color="@android:color/holo_green_light"
app:seed="0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359"
app:size="10" />
app:size="10"
app:spotColor="@android:color/holo_orange_dark" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ class BlockiesIconGenerator(
private var imageData: IntArray = intArrayOf()

init {
seedRand(seed.lowercase(Locale.getDefault()))
seedRand(seed)
initialize(size)
}

private fun initialize(size: Int) {
// create colors after seedRandom function
if (color == 0) {
if (color.isColorInvalid) {
color = createColor()
}
if (bgColor == 0) {
if (bgColor.isColorInvalid) {
bgColor = createColor()
}
if (spotColor == 0) {
if (spotColor.isColorInvalid) {
spotColor = createColor()
}
colorPaint.color = color
Expand Down Expand Up @@ -172,4 +172,7 @@ class BlockiesIconGenerator(
)
canvas.drawRect(backgroundRect, backgroundPaint)
}

private val @receiver:ColorInt Int.isColorInvalid: Boolean
get() = this == 0
}
Loading

0 comments on commit 29c42be

Please sign in to comment.