Skip to content

Commit

Permalink
reworked vector options builder alexzhirkevich#21
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed Mar 10, 2023
1 parent 1cfb4de commit 2a9fd23
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION", "UNUSED")

package com.github.alexzhirkevich.customqrgenerator

import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator

import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@file:Suppress("UNUSED")

package com.github.alexzhirkevich.customqrgenerator

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
Expand Down Expand Up @@ -306,6 +309,7 @@ fun interface QrData {
override fun encode(): String = "{{{market://details?id=%$appPackage}}}"
}

@OptIn(ExperimentalSerializationApi::class)
companion object : SerializationProvider {
override val defaultSerializersModule by lazy(LazyThreadSafetyMode.NONE) {
SerializersModule {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED")

package com.github.alexzhirkevich.customqrgenerator

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ data class QrOptions(
class Builder(
@IntRange(from = 0) val width: Int,
@IntRange(from = 0) val height: Int = width
) : QrLogoBuilder, QrOffsetBuilder {

var padding = .125f
override var offset = QrOffset.Zero
var colors = QrColors()
override var logo = QrLogo()
var background = QrBackground()
var elementsShapes = QrElementsShapes()
var codeShape: QrShape = QrShape.Default
var errorCorrectionLevel: QrErrorCorrectionLevel = QrErrorCorrectionLevel.Auto
) {

internal var padding = .125f
internal var offset = QrOffset.Zero
internal var colors = QrColors()
internal var logo = QrLogo()
internal var background = QrBackground()
internal var elementsShapes = QrElementsShapes()
internal var codeShape: QrShape = QrShape.Default
internal var errorCorrectionLevel: QrErrorCorrectionLevel = QrErrorCorrectionLevel.Auto

fun build(): QrOptions = QrOptions(
width,height, padding, offset, colors, logo, background,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED")

package com.github.alexzhirkevich.customqrgenerator

import com.github.alexzhirkevich.customqrgenerator.vector.QrVectorOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import android.graphics.Canvas
Expand Down Expand Up @@ -73,7 +75,7 @@ inline fun <reified T : QrShapeModifier> QrElementsShapesBuilderScope.pathShape(
/**
* @see [pathShape]
* */
@Suppress("unchecked_cast", "deprecation")
@Suppress("unchecked_cast")
fun <T : QrShapeModifier> QrElementsShapesBuilderScope.pathShape(
clazz: KClass<T>,
efficiency: Efficiency = Efficiency.Time,
Expand All @@ -100,7 +102,6 @@ inline fun <reified T : QrShapeModifier> QrLogoBuilderScope.pathShape(
noinline builder: Path.(size: Int) -> Unit
): T = pathShape(T::class, efficiency, builder)

@Suppress("deprecation")
fun <T : QrShapeModifier> QrLogoBuilderScope.pathShape(
clazz: KClass<T>,
efficiency: Efficiency = Efficiency.Time,
Expand All @@ -124,7 +125,6 @@ fun <T : QrShapeModifier> QrLogoBuilderScope.pathShape(
ReplaceWith("pathShape"),
level = DeprecationLevel.WARNING
)
@Suppress("deprecation")
inline fun <reified T : QrShapeModifier> QrElementsShapesBuilderScope.drawShape(
noinline draw : Canvas.(drawPaint : Paint, erasePaint : Paint) -> Unit
): T = drawShape(T::class, draw)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrOptions
Expand All @@ -18,7 +20,7 @@ sealed interface QrLogoBuilderScope : IQRLogo {
}

internal class InternalQrLogoBuilderScope(
val builder: QrLogoBuilder,
val builder: QrOptions.Builder,
val width : Int,
val height : Int,
val codePadding : Float = -1f
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrOptions
import com.github.alexzhirkevich.customqrgenerator.style.IQrOffset
import com.github.alexzhirkevich.customqrgenerator.style.QrOffset
import com.github.alexzhirkevich.customqrgenerator.style.QrOffsetBuilder

/**
* @see QrOffset
Expand All @@ -13,7 +15,7 @@ sealed interface QrOffsetBuilderScope : IQrOffset {
}

internal class InternalQrOffsetBuilderScope(
private val builder: QrOffsetBuilder
private val builder: QrOptions.Builder
) : QrOffsetBuilderScope {

override var x: Float
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.github.alexzhirkevich.customqrgenerator.dsl

import com.github.alexzhirkevich.customqrgenerator.QrErrorCorrectionLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,3 @@ data class QrOffset(
val Zero = QrOffset(0f,0f)
}
}

interface QrOffsetBuilder {
var offset : QrOffset
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.alexzhirkevich.customqrgenerator.QrErrorCorrectionLevel
import com.github.alexzhirkevich.customqrgenerator.SerializationProvider
import com.github.alexzhirkevich.customqrgenerator.SerializersModuleFromProviders
import com.github.alexzhirkevich.customqrgenerator.style.QrOffset
import com.github.alexzhirkevich.customqrgenerator.style.QrOffsetBuilder
import com.github.alexzhirkevich.customqrgenerator.style.QrShape
import com.github.alexzhirkevich.customqrgenerator.vector.dsl.InternalQrVectorOptionsBuilderScope
import com.github.alexzhirkevich.customqrgenerator.vector.dsl.QrVectorOptionsBuilderScope
Expand All @@ -27,48 +26,56 @@ data class QrVectorOptions(
val errorCorrectionLevel: QrErrorCorrectionLevel,
val fourthEyeEnabled : Boolean,
) {
class Builder : QrOffsetBuilder, QrVectorLogoBuilder {
class Builder {

@FloatRange(from = .0, to = .5)
var padding : Float = 0f
override var offset: QrOffset = QrOffset(0f,0f)
private set
var offset: QrOffset = QrOffset(0f,0f)
private set
var shapes: QrVectorShapes = QrVectorShapes()
private set
var shape : QrShape = QrShape.Default
private set
var colors : QrVectorColors = QrVectorColors()
override var logo : QrVectorLogo = QrVectorLogo()
private set
var logo : QrVectorLogo = QrVectorLogo()
private set
var background: QrVectorBackground = QrVectorBackground()
private set
var errorCorrectionLevel: QrErrorCorrectionLevel = QrErrorCorrectionLevel.Auto
private set
var fourthEyeEnabled : Boolean = false

fun padding(@FloatRange(from = .0, to = .5) padding: Float) = apply {
private set
fun setPaddding(@FloatRange(from = .0, to = .5) padding: Float) = apply {
this.padding = padding
}

fun offset(offset: QrOffset) = apply {
fun setOffset(offset: QrOffset) = apply {
this.offset = offset
}

fun shapes(shapes: QrVectorShapes) = apply {
fun setShapes(shapes: QrVectorShapes) = apply {
this.shapes = shapes
}

fun colors(colors: QrVectorColors) = apply {
fun setColors(colors: QrVectorColors) = apply {
this.colors = colors
}

fun codeShape(shape: QrShape) = apply{
fun setCodeShape(shape: QrShape) = apply{
this.shape = shape
}

fun logo(logo: QrVectorLogo) = apply {
fun setLogo(logo: QrVectorLogo) = apply {
this.logo = logo
}

fun background(background: QrVectorBackground) = apply {
fun setBackground(background: QrVectorBackground) = apply {
this.background = background
}

fun errorCorrectionLevel(errorCorrectionLevel: QrErrorCorrectionLevel) = apply {
fun setErrorCorrectionLevel(errorCorrectionLevel: QrErrorCorrectionLevel) = apply {
this.errorCorrectionLevel = errorCorrectionLevel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ internal class InternalQrVectorBackgroundBuilderScope(
override var drawable: DrawableSource
get() = builder.background.drawable
set(value) = with(builder){
background(background.copy(drawable = value))
setBackground(background.copy(drawable = value))
}
override var scale: BitmapScale
get() = builder.background.scale
set(value) = with(builder){
background(background.copy(scale = value))
setBackground(background.copy(scale = value))
}
override var color: QrVectorColor
get() = builder.background.color
set(value) = with(builder) {
background(background.copy(color = value))
setBackground(background.copy(color = value))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ internal class InternalQrVectorColorsBuilderScope(
override var dark: QrVectorColor
get() = builder.colors.dark
set(value) = with(builder){
colors(colors.copy(
setColors(colors.copy(
dark = value
))
}

override var light: QrVectorColor
get() = builder.colors.light
set(value) = with(builder){
colors(colors.copy(
setColors(colors.copy(
light = value
))
}

override var ball: QrVectorColor
get() = builder.colors.ball
set(value) = with(builder){
colors(colors.copy(
setColors(colors.copy(
ball = value
))
}
override var frame: QrVectorColor
get() = builder.colors.frame
set(value) = with(builder){
colors(colors.copy(
setColors(colors.copy(
frame = value
))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package com.github.alexzhirkevich.customqrgenerator.vector.dsl

import com.github.alexzhirkevich.customqrgenerator.style.*
import com.github.alexzhirkevich.customqrgenerator.vector.QrVectorOptions
import com.github.alexzhirkevich.customqrgenerator.vector.style.*

internal class InternalQrVectorLogoBuilderScope(
val builder: QrVectorLogoBuilder,
val builder: QrVectorOptions.Builder,
) : QrVectorLogoBuilderScope {

override var drawable: DrawableSource
get() = builder.logo.drawable
set(value) = with(builder) {
logo = logo.copy(drawable = value)
setLogo(logo.copy(drawable = value))
}
override var size: Float
get() = builder.logo.size
set(value) = with(builder) {
logo = logo.copy(size = value)
setLogo(logo.copy(size = value))
}

override var padding: QrVectorLogoPadding
get() = builder.logo.padding
set(value) = with(builder) {
logo =logo.copy(padding = value)
setLogo(logo.copy(padding = value))
}
override var shape: QrVectorLogoShape
get() = builder.logo.shape
set(value) = with(builder) {
logo = logo.copy(shape = value)
setLogo(logo.copy(shape = value))
}

override var scale: BitmapScale
get() = builder.logo.scale
set(value) = with(builder) {
logo = logo.copy(scale = value)
setLogo(logo.copy(scale = value))
}
override var backgroundColor: QrVectorColor
get() = builder.logo.backgroundColor
set(value) = with(builder) {
logo = logo.copy(backgroundColor = value)
setLogo(logo.copy(backgroundColor = value))
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.alexzhirkevich.customqrgenerator.vector.dsl

import com.github.alexzhirkevich.customqrgenerator.QrErrorCorrectionLevel
import com.github.alexzhirkevich.customqrgenerator.dsl.InternalQrOffsetBuilderScope
import com.github.alexzhirkevich.customqrgenerator.dsl.QrOffsetBuilderScope
import com.github.alexzhirkevich.customqrgenerator.style.QrOffset
import com.github.alexzhirkevich.customqrgenerator.style.QrShape
import com.github.alexzhirkevich.customqrgenerator.vector.QrVectorOptions

Expand All @@ -14,21 +13,29 @@ internal class InternalQrVectorOptionsBuilderScope(
override var padding: Float
get() = builder.padding
set(value) {
builder.padding(value)
builder.setPaddding(value)
}

override var errorCorrectionLevel: QrErrorCorrectionLevel
get() = builder.errorCorrectionLevel
set(value) {
builder.errorCorrectionLevel(value)
builder.setErrorCorrectionLevel(value)
}

override var codeShape: QrShape by builder::shape
override var codeShape: QrShape
get() = builder.shape
set(value){
builder.setCodeShape(value)
}

override var fourthEyeEnabled: Boolean by builder::fourthEyeEnabled
override var fourthEyeEnabled: Boolean
get() = builder.fourthEyeEnabled
set(value) {
builder.setFourthEyeEnabled(value)
}

override fun offset(block: QrOffsetBuilderScope.() -> Unit) {
InternalQrOffsetBuilderScope(builder).apply(block)
override fun offset(x : Float, y : Float) {
builder.setOffset(QrOffset(x,y))
}

override fun shapes(
Expand Down
Loading

0 comments on commit 2a9fd23

Please sign in to comment.