From 24692490f1fdb1123bec588c7255ed7798823459 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 11 Sep 2022 11:33:46 +0530 Subject: [PATCH] wheelView fixes --- .idea/misc.xml | 2 + .../java/com/example/spinwill/MainActivity.kt | 17 ++- .../example/spinwill/RemoteDatabaseImpl.kt | 32 ++--- app/src/main/res/layout/activity_main.xml | 9 +- jitpack.yml | 4 + spinwill/build.gradle | 15 +++ .../spinwill/adapter/WillPaintAdapter.kt | 7 ++ .../spinwill/models/WillPaintProperties.kt | 33 ++++- .../com/example/spinwill/ui/SpinWillView1.kt | 46 +++++++ .../java/com/example/spinwill/ui/WillView1.kt | 116 ++++++++++-------- .../ui/defaultImpl/DimenAdapter1Impl.kt | 3 +- .../src/main/res/drawable/bg_spinwill.xml | 6 + .../res/drawable/ic_spinwill_pointer.webp | Bin 0 -> 2036 bytes .../main/res/layout/layout_spinwillview1.xml | 26 ++++ spinwill/src/main/res/values/colors.xml | 2 +- 15 files changed, 230 insertions(+), 88 deletions(-) create mode 100644 jitpack.yml create mode 100644 spinwill/src/main/java/com/example/spinwill/adapter/WillPaintAdapter.kt create mode 100644 spinwill/src/main/java/com/example/spinwill/ui/SpinWillView1.kt create mode 100644 spinwill/src/main/res/drawable/bg_spinwill.xml create mode 100644 spinwill/src/main/res/drawable/ic_spinwill_pointer.webp create mode 100644 spinwill/src/main/res/layout/layout_spinwillview1.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index b50a2dc..72ac3b7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,6 +4,8 @@ diff --git a/app/src/main/java/com/example/spinwill/MainActivity.kt b/app/src/main/java/com/example/spinwill/MainActivity.kt index 327e9a0..e1bb644 100644 --- a/app/src/main/java/com/example/spinwill/MainActivity.kt +++ b/app/src/main/java/com/example/spinwill/MainActivity.kt @@ -2,17 +2,20 @@ package com.example.spinwill import android.annotation.SuppressLint import android.graphics.Bitmap +import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.FrameLayout import android.widget.ImageView +import androidx.core.content.ContextCompat import androidx.work.* import com.example.spinwill.adapter.WillItemAdapter import com.example.spinwill.cron.SpinWillWorker import com.example.spinwill.cron.SpinWillWorkerFactory import com.example.spinwill.database.local.SpinWillLocalDbImpl import com.example.spinwill.di.SpinWillInjector +import com.example.spinwill.ui.SpinWillView1 import com.example.spinwill.ui.WillView1 import com.example.spinwill.ui.adapters.WillItemUiAdapter import com.example.spinwill.usecases.SpinWillBitmapLoadUseCaseImpl @@ -35,18 +38,26 @@ class MainActivity : AppCompatActivity() { setupDependencies() // setupWork() - setupUi() + // setupUi() setupWheelView() } private fun setupWheelView() { val layout = findViewById(R.id.frame) - val willView = WillView1(this, null) + val willView = SpinWillView1(this) layout.addView(willView) scope.launch(Dispatchers.IO) { - val result = injector.getLocalDatabase().getAllWillItems() + injector.getRepository().fetchAndUpdateWheelItem() + val result = injector.getRepository().loadBitmapAndSave() + if (result is Resource.Success && result.data != null) { + val list = result.data!! + Log.d("MainAct", "result size " + list.size) + runOnUiThread { + willView.getWillView().paintProps.textPaint.apply { + color = Color.WHITE + } willView.setItems(result.data!!) willView.setItemAdapter(object : WillItemUiAdapter { override fun getRewardText(item: SpinWillItem): String { diff --git a/app/src/main/java/com/example/spinwill/RemoteDatabaseImpl.kt b/app/src/main/java/com/example/spinwill/RemoteDatabaseImpl.kt index b108805..a479118 100644 --- a/app/src/main/java/com/example/spinwill/RemoteDatabaseImpl.kt +++ b/app/src/main/java/com/example/spinwill/RemoteDatabaseImpl.kt @@ -10,28 +10,20 @@ class RemoteDatabaseImpl : SpinWillRemoteDatabase { Log.d("App", "23") val list = mutableListOf() - list.add( - SpinWillItem( - rewardId = 1L, - rewardAmount = 100L, - rewardImage = "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg", - rewardText = "100 COINS" + /* + when number of items is odd it creates repetition in color of arcs + */ + for (i in 0..5) { + list.add( + SpinWillItem( + rewardId = 1L, + rewardAmount = i * 100L, + rewardImage = "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg", + rewardText = "${i}00" + ) ) - ) - - delay(1000L) - - list.add( - SpinWillItem( - rewardId = 1L, - rewardAmount = 100L, - rewardImage = "https://images.pexels.com/photos/2014422/pexels-photo-2014422.jpeg", - rewardText = "100 COINS" - ) - ) - + } Log.d("App", "19") - return Resource.Success(list) } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f25536a..b64577f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,6 +10,7 @@ android:id="@+id/iv_1" android:layout_width="100dp" android:layout_height="100dp" + android:visibility="gone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -18,18 +19,18 @@ android:id="@+id/iv_2" android:layout_width="200dp" android:layout_height="200dp" + android:visibility="gone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/iv_1" /> + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file diff --git a/jitpack.yml b/jitpack.yml new file mode 100644 index 0000000..1bfc0d7 --- /dev/null +++ b/jitpack.yml @@ -0,0 +1,4 @@ +jdk: + - openjdk11 +before_install: + - ./scripts/prepareJitpackEnvironment.sh \ No newline at end of file diff --git a/spinwill/build.gradle b/spinwill/build.gradle index 7176542..471180e 100644 --- a/spinwill/build.gradle +++ b/spinwill/build.gradle @@ -4,6 +4,7 @@ plugins { id 'kotlin-android' id 'kotlin-android-extensions' id 'kotlin-kapt' + id 'maven-publish' } android { @@ -58,4 +59,18 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} + +afterEvaluate { + publishing { + publications { + release(MavenPublication) { + from components.release + + groupId = 'com.github.sidsharma2002' + artifactId = 'SpinWill' + version = '0.1' + } + } + } } \ No newline at end of file diff --git a/spinwill/src/main/java/com/example/spinwill/adapter/WillPaintAdapter.kt b/spinwill/src/main/java/com/example/spinwill/adapter/WillPaintAdapter.kt new file mode 100644 index 0000000..0024599 --- /dev/null +++ b/spinwill/src/main/java/com/example/spinwill/adapter/WillPaintAdapter.kt @@ -0,0 +1,7 @@ +package com.example.spinwill.adapter + +import android.graphics.Paint + +interface WillPaintAdapter { + fun setPaintProperties(paint: Paint) +} \ No newline at end of file diff --git a/spinwill/src/main/java/com/example/spinwill/models/WillPaintProperties.kt b/spinwill/src/main/java/com/example/spinwill/models/WillPaintProperties.kt index 2910339..e0d8ec3 100644 --- a/spinwill/src/main/java/com/example/spinwill/models/WillPaintProperties.kt +++ b/spinwill/src/main/java/com/example/spinwill/models/WillPaintProperties.kt @@ -1,11 +1,34 @@ package com.example.spinwill.models import android.graphics.Paint +import com.example.spinwill.adapter.WillPaintAdapter data class WillPaintProperties( - val archPaint: Paint = Paint(), - val textPaint: Paint = Paint(), - val overlayTextPaint: Paint = Paint(), - val separationArchPaint: Paint = Paint(), - val bitmapPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG) + val archPaint: Paint = Paint().apply { + isAntiAlias = true + isDither = true + style = Paint.Style.FILL_AND_STROKE + }, + val textPaint: Paint = Paint().apply { + isAntiAlias = true + isDither = true + letterSpacing = 0.1f + }, + val overlayTextPaint: Paint = Paint().apply { + isAntiAlias = true + isDither = true + letterSpacing = 0.1f + }, + val separationArchPaint: Paint = Paint().apply { + style = Paint.Style.STROKE + isAntiAlias = true + isDither = true + }, + val bitmapPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG), + + var archPaintAdapter: WillPaintAdapter? = null, + var textPaintAdapter: WillPaintAdapter? = null, + var overlayTextPaintAdapter: WillPaintAdapter? = null, + var separationArchPaintAdapter: WillPaintAdapter? = null, + var bitmapPaintAdapter: WillPaintAdapter? = null ) \ No newline at end of file diff --git a/spinwill/src/main/java/com/example/spinwill/ui/SpinWillView1.kt b/spinwill/src/main/java/com/example/spinwill/ui/SpinWillView1.kt new file mode 100644 index 0000000..f5f5278 --- /dev/null +++ b/spinwill/src/main/java/com/example/spinwill/ui/SpinWillView1.kt @@ -0,0 +1,46 @@ +package com.example.spinwill.ui + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.widget.FrameLayout +import androidx.annotation.ColorRes +import androidx.annotation.IdRes +import androidx.constraintlayout.widget.ConstraintLayout +import com.example.spinwill.R +import com.example.spinwill.ui.adapters.WillItemUiAdapter + +class SpinWillView1 constructor( + context: Context +) : ConstraintLayout(context) { + + private lateinit var willView1: WillView1 + + init { + val parentView = + LayoutInflater.from(context).inflate(R.layout.layout_spinwillview1, null, false) + willView1 = WillView1(context, null) + val parentLayout: FrameLayout = parentView.findViewById(R.id.container) + parentLayout.addView(willView1) + addView(parentView) + postInvalidate() + } + + fun setItems(data: List) { + willView1.setItems(data) + willView1.invalidate() + } + + fun setItemAdapter(willItemUiAdapter: WillItemUiAdapter) { + willView1.setItemAdapter(willItemUiAdapter) + willView1.invalidate() + } + + fun getWillView(): WillView1 { + return willView1 + } + + fun setTextColor(@IdRes color: Int) { + willView1.paintProps.textPaint.color = color + } +} \ No newline at end of file diff --git a/spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt b/spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt index dfb5209..9433e58 100644 --- a/spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt +++ b/spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt @@ -7,6 +7,7 @@ import android.view.View import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import com.example.spinwill.R +import com.example.spinwill.adapter.WillPaintAdapter import com.example.spinwill.models.WillDimenProperties import com.example.spinwill.models.WillGradient import com.example.spinwill.models.WillPaintProperties @@ -15,6 +16,7 @@ import com.example.spinwill.ui.adapters.WillItemUiAdapter import com.example.spinwill.ui.defaultImpl.OverlayDimenAdapter1Impl import com.example.spinwill.ui.defaultImpl.TextDimenAdapter1Impl import com.example.spinwill.utils.dp +import kotlin.math.PI import kotlin.math.cos import kotlin.math.min import kotlin.math.sin @@ -27,6 +29,10 @@ class WillView1 constructor( attributeSet: AttributeSet? ) : View(context, attributeSet) { + companion object { + const val DEF_BORDER_PERCENT = 3f + } + private var wheelSize: Int = 0 /** @@ -49,8 +55,8 @@ class WillView1 constructor( } private val dimenProps = WillDimenProperties() - private val paintProps = WillPaintProperties() - private val gradientProps = WillGradient() + val paintProps = WillPaintProperties() + val gradientProps = WillGradient() private var willRange = RectF() private var mPath = Path() private var mRect = Rect() @@ -76,7 +82,8 @@ class WillView1 constructor( val sweepAngle: Float = 360f / wheelSize val rotateAngle = sweepAngle / 2 + 90 - for (i in 0..items.size) { + for (i in items.indices) { + // drawPieBackgroundBorder(canvas) drawPieBackground(i, tempAngle, sweepAngle, canvas) val bitmap = itemAdapter.getRewardBitmap(items[i]) @@ -97,7 +104,6 @@ class WillView1 constructor( itemAdapter.getRewardText(items[i]) ) drawSeparationArc(willRange, tempAngle, sweepAngle, canvas) - // prepare for next iteration tempAngle += sweepAngle paintProps.archPaint.reset() @@ -121,7 +127,6 @@ class WillView1 constructor( (dimenProps.center + 5 * (dimenProps.radius) / 9 * sin(angle.toDouble())).toInt() // create arc to draw - // setting the value of Rect mRect.set(x - imgWidth / 2, y - imgWidth / 2, x + imgWidth / 2, y + imgWidth / 2) // rotate main bitmap @@ -148,7 +153,7 @@ class WillView1 constructor( val sweepAngle: Double = 360.0 / wheelSize val angle = Math.toRadians((180 - sweepAngle) / 2) - return (3f * (dimenProps.radius) / 4 * cos(angle)).toInt() + return (2f * (dimenProps.radius) / 4 * cos(angle)).toInt() } @@ -188,7 +193,7 @@ class WillView1 constructor( private fun drawText(canvas: Canvas, tempAngle: Float, sweepAngle: Float, text: String) { mPath.addArc(willRange, tempAngle, sweepAngle) //used global Path - val hOffset: Int = overlayDimenAdapter1.getHOffsetOverlayText( + val hOffset: Int = textDimenAdapter1.getHOffsetOverlayText( tempAngle, sweepAngle, text, @@ -197,7 +202,7 @@ class WillView1 constructor( dimenProps ) - val vOffset: Int = overlayDimenAdapter1.getVOffsetOverLayText( + val vOffset: Int = textDimenAdapter1.getVOffsetOverLayText( tempAngle, sweepAngle, text, @@ -232,18 +237,17 @@ class WillView1 constructor( super.onMeasure(widthMeasureSpec, heightMeasureSpec) val width = min(measuredWidth, measuredHeight) - dimenProps.let { - it.paddingLeft = paddingLeft - it.paddingBottom = paddingBottom - it.paddingTop = paddingTop - it.paddingRight = paddingRight - } + val padding = (width * DEF_BORDER_PERCENT / 100).toInt() - if ((paddingLeft == paddingRight) && (paddingTop == paddingRight) && (paddingBottom == paddingTop)) { - dimenProps.padding = paddingLeft + dimenProps.let { + it.paddingLeft = padding // paddingLeft + it.paddingBottom = padding // paddingBottom + it.paddingTop = padding // paddingTop + it.paddingRight = padding // paddingRight + it.padding = padding } - dimenProps.radius = (width - (dimenProps.paddingLeft + dimenProps.paddingRight * 2)) / 2 + dimenProps.radius = (width - (dimenProps.paddingLeft + dimenProps.paddingRight)) / 2 dimenProps.center = width / 2 setMeasuredDimension( @@ -255,49 +259,53 @@ class WillView1 constructor( } private fun initComponents() { - paintProps.archPaint.apply { - isAntiAlias = true - isDither = true - color = ContextCompat.getColor(context, R.color.coin_txt) - strokeWidth = 0.01f - style = Paint.Style.FILL_AND_STROKE + if (paintProps.archPaintAdapter != null) { + paintProps.archPaintAdapter!!.setPaintProperties(paintProps.archPaint) + } else { + paintProps.archPaint.apply { + color = ContextCompat.getColor(context, R.color.coin_txt) + strokeWidth = 0.01f + } } - paintProps.textPaint.apply { - isAntiAlias = true - isDither = true - typeface = ResourcesCompat.getFont( - context, - R.font.montserrat_extrabolditalic - ) - textSize = 20.dp() - letterSpacing = 0.1f + if (paintProps.textPaintAdapter != null) { + paintProps.textPaintAdapter!!.setPaintProperties(paintProps.textPaint) + } else { + paintProps.textPaint.apply { + typeface = ResourcesCompat.getFont( + context, + R.font.montserrat_extrabolditalic + ) + textSize = (dimenProps.radius * 1.5 / 9).toFloat() + } } - paintProps.overlayTextPaint.apply { - color = ContextCompat.getColor( - context, - R.color.spinwill_overalytext_color - ) - isAntiAlias = true - isDither = true - typeface = ResourcesCompat.getFont( - context, - R.font.montserrat_extrabolditalic - ) - textSize = 45.dp() - letterSpacing = 0.1f + if (paintProps.overlayTextPaintAdapter != null) { + paintProps.overlayTextPaintAdapter!!.setPaintProperties(paintProps.overlayTextPaint) + } else { + paintProps.overlayTextPaint.apply { + color = ContextCompat.getColor( + context, + R.color.spinwill_overalytext_color + ) + typeface = ResourcesCompat.getFont( + context, + R.font.montserrat_extrabolditalic + ) + textSize = (dimenProps.radius * 3 / 9).toFloat() + } } - paintProps.separationArchPaint.apply { - style = Paint.Style.STROKE - color = ContextCompat.getColor( - context, - R.color.spinwill_arc_seperation_line - ) - isAntiAlias = true - isDither = true - strokeWidth = 3f + if (paintProps.separationArchPaintAdapter != null) { + paintProps.separationArchPaintAdapter!!.setPaintProperties(paintProps.separationArchPaint) + } else { + paintProps.separationArchPaint.apply { + color = ContextCompat.getColor( + context, + R.color.spinwill_arc_seperation_line + ) + strokeWidth = dimenProps.radius / 55f + } } val padding = dimenProps.padding.toFloat() diff --git a/spinwill/src/main/java/com/example/spinwill/ui/defaultImpl/DimenAdapter1Impl.kt b/spinwill/src/main/java/com/example/spinwill/ui/defaultImpl/DimenAdapter1Impl.kt index 0f6c10c..8e86eab 100644 --- a/spinwill/src/main/java/com/example/spinwill/ui/defaultImpl/DimenAdapter1Impl.kt +++ b/spinwill/src/main/java/com/example/spinwill/ui/defaultImpl/DimenAdapter1Impl.kt @@ -40,7 +40,8 @@ class TextDimenAdapter1Impl : OffSetDimenAdapter1 { dimenProps: WillDimenProperties ): Int { val textWidth: Float = paintProps.overlayTextPaint.measureText(text) - return (dimenProps.radius * Math.PI / itemsSize - textWidth / 2).toInt() + val h = (2 * dimenProps.radius * Math.PI / itemsSize - textWidth / 2).toInt() + return h / 2 } override fun getVOffsetOverLayText( diff --git a/spinwill/src/main/res/drawable/bg_spinwill.xml b/spinwill/src/main/res/drawable/bg_spinwill.xml new file mode 100644 index 0000000..e44d071 --- /dev/null +++ b/spinwill/src/main/res/drawable/bg_spinwill.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/spinwill/src/main/res/drawable/ic_spinwill_pointer.webp b/spinwill/src/main/res/drawable/ic_spinwill_pointer.webp new file mode 100644 index 0000000000000000000000000000000000000000..505dbfaf5e626bcc5b9e6917f9344d5d7fd5def7 GIT binary patch literal 2036 zcmVJyn3VsIWD=fD9EGf!xla(sRt(qv8&%?BZ3$+i_Zj%GWT8ELT?^ z$L9fRe8^@XY8}kPB|}e!3y(dhc`u8mCS9hc3@qNE_IDQaCL_Vgy9Rr8v;oio~~)s8>m?vL1!! zNd7s2G01SVHt!q`Io`UCAT5P^>`C_GGF0!4YA*7g3afrH0$oQhN4#>KL zB8PZh@pfv~A@j9L;>x*|$EkRT>>nzag96SnE2Kbzf)I0VqlmZ77>+`YFyxynMXsNb zYbfSb1I19U}k z2LNoB=P>0RIgxmm|;hf3Qi8IbhH&SBgY?BQRfzkzm;7kqlQY zjDg2*2|hW1xiNYCGJ?VUcKn)x$`Rapf!fWy)`IGjoKAuIB_=MW6`-?Q36GL)ptn&H zhonZJ+xHc{XTG4{{tUGM0#(c5@l^f{vjoESGcze#oAR+|- z0I(eZodGIT0B-<3Z7z~VBqFM$toEDHPz{M@25@755DJl-ZuURQE`07l?+4@tXcvk8 zxBg4+JGW=scen@XXX{^CuP`2XzJmP#KWF;DJpeqhe{a4IKXQL?F&}@ueE>lL*pz1q zwwc$D8Z3Q%`T!${^r?^boBG|_zPY#_Br82uhi0rwbtRb#`bP)YBu$!eHN%-&i})Rx zIyWvk#K$z#i4Q>kAFNiJddc_^y?lCDobB75B1ric(N{LqTVgl4lJ(J; zKTOgQ*~dq+8C}oJusGew5_2;{hwAHweG_E+)&at9T74sVd@G2khjD(sQCsQ(zKzG% zu=sbL!O zVo0S~CM5C^?7LS}WQrwJXJBc_thU)IKM;wXGyoGk2G9jiTblIz)Abdueg3}ug)98u z-sH~Ye}lICNP!XH#n956StL=1Gk#7%^(+1Zx%HCk`w7|Rd6i)q{$_9biiRlx&>1&S zI9EBT8(yz___aVUWr`8;58pssAV*-A$v>K&NCZud{J-N$n7?GA?uoI4aG`q= zZ#su8E2v)gtpm8G4Y=avXg`3W9s)wR(0Yn=(a=@UhL4g)9gyS9uQG@Rq7xUx;8XSi zyMGR674P-u|JJnj>dK-YyT-3VeSynKQ$w8Ea3N-^^F3O{y!{>(2VJ%P|6EFt>Jk5w zMF?Q8my|z1W1a6TF|(whYbq{Q%~qzN>|XQ=Vbi73x7{wr0=hmDvMurO$BH6~ly*Z& zxAtczCR+AN7@-%h+(|-Y|KC$BYDaNuCC8}WOL-opk8QTjehWbxV7aFSIB5%jc zVvMCg{)_DnEx;yLbi4JLU*J>+)1x$=>~~2v_rcNirL0FZYKftkrd|Au6vSq2ix4cR znjPZXTG$bTUB-uXrHk;!h4Wb?mog8;&~jIY^3JmR{5Slwga7}xfDM1XZU3U~7D11Y znRN40ehT{!WXY8)0q#qQJ0?JJYI%&=wImXHJECZzXV8yC%2EXJ15fYK9ej@r8-u~m3Tic}9 zgedo~(L{C~UYQPK@TGgWL~(9Thm-f81L7swX-s1hus zYX=zqKi~;PeK<-czWsnDqiv;BpZw!QH + + + + + + + \ No newline at end of file diff --git a/spinwill/src/main/res/values/colors.xml b/spinwill/src/main/res/values/colors.xml index 829b48c..e917c79 100644 --- a/spinwill/src/main/res/values/colors.xml +++ b/spinwill/src/main/res/values/colors.xml @@ -9,7 +9,7 @@ #FFFFFFFF #fbb42a #12770E00 - #80E08720 + #FCDEA667 #FFC87E #F4C21A #FDE79E