Skip to content

Commit

Permalink
version 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanishmoral11 committed Aug 16, 2024
1 parent df1d678 commit 25d2b13
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 57 deletions.
68 changes: 68 additions & 0 deletions app/src/main/java/com/example/chantingapp/BeadProgressView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.chantingapp

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import kotlin.math.cos
import kotlin.math.min
import kotlin.math.sin

class BeadProgressView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val totalBeads = 108
private var filledBeads = 0

init {
paint.style = Paint.Style.FILL
}

fun setProgress(progress: Int) {
filledBeads = progress
invalidate()
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

val centerX = width / 2f
val centerY = height / 2f
val radius = 160f // Slightly smaller to fit within the view

// Draw white background circle
paint.color = ContextCompat.getColor(context, android.R.color.white)
canvas.drawCircle(centerX, centerY, radius, paint)

// Draw border
paint.style = Paint.Style.STROKE
paint.color = ContextCompat.getColor(context, android.R.color.darker_gray)
paint.strokeWidth = 2f
canvas.drawCircle(centerX, centerY, radius, paint)

paint.style = Paint.Style.FILL

val angleStep = 360f / totalBeads
val beadRadius = 10f // Smaller bead size

for (i in 0 until totalBeads) {
val angle = Math.toRadians((-90 + i * angleStep).toDouble()) // Start from 12 o'clock
val x = (centerX + radius * 0.85 * cos(angle)).toFloat() // Move beads slightly inward
val y = (centerY + radius * 0.85 * sin(angle)).toFloat() // Move beads slightly inward

paint.color = if (i < filledBeads) {
ContextCompat.getColor(context, R.color.bead_filled)
} else {
ContextCompat.getColor(context, R.color.bead_empty)
}

canvas.drawCircle(x, y, beadRadius, paint)
}
}
}
33 changes: 8 additions & 25 deletions app/src/main/java/com/example/chantingapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
package com.example.chantingapp

import android.animation.ObjectAnimator
import android.content.Context
import android.content.SharedPreferences
import android.media.MediaPlayer
import android.os.Bundle
import android.view.animation.DecelerateInterpolator
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.*

class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickListener {

private lateinit var sharedPreferences: SharedPreferences

private lateinit var countTextView: TextView
private lateinit var roundsTextView: TextView
private lateinit var streakTextView: TextView
private lateinit var mediaPlayer: MediaPlayer
private lateinit var viewPager: ViewPager
private lateinit var adapter: BackgroundPagerAdapter
private lateinit var progressBar: ProgressBar
private lateinit var beadProgressView: BeadProgressView

private var count = 0
private var rounds = 0
Expand All @@ -42,15 +37,12 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

progressBar = findViewById(R.id.progressBar)
progressBar.max = 108
progressBar.progress = 0

sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

countTextView = findViewById(R.id.countTextView)
roundsTextView = findViewById(R.id.roundsTextView)
streakTextView = findViewById(R.id.streakTextView)
beadProgressView = findViewById(R.id.beadProgressView)
val resetButton: Button = findViewById(R.id.resetButton)
viewPager = findViewById(R.id.viewPager)

Expand All @@ -62,7 +54,7 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis
resetButton.setOnClickListener {
count = 0
rounds = 0
progressBar.progress = 0
beadProgressView.setProgress(0)
updateCount()
}

Expand All @@ -83,35 +75,25 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis

override fun onImageClick(position: Int) {
count++
animateProgressBar(count)
beadProgressView.setProgress(count)

if (count == 108) {
rounds++
count = 0
animateProgressBar(0)
beadProgressView.setProgress(0)
updateStreak()
}

updateCount()
}

private fun animateProgressBar(newProgress: Int) {
val animator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, newProgress)
animator.duration = 300 // Duration of the animation in milliseconds
animator.interpolator = DecelerateInterpolator()
animator.start()
}

private fun updateCount() {
if (count == 0 && rounds > 0) {
mediaPlayer.start()
}
countTextView.text = count.toString()
roundsTextView.text = rounds.toString()
streakTextView.text = "Streak: $streak"
// Update progress percentage
val progressPercentage = (count.toFloat() / 108 * 100).toInt()
progressBar.contentDescription = "$progressPercentage% completed"
}

private fun updateStreak() {
Expand Down Expand Up @@ -141,6 +123,7 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis
count = sharedPreferences.getInt("count", 0)
rounds = sharedPreferences.getInt("rounds", 0)
streak = sharedPreferences.getInt("streak", 0)
beadProgressView.setProgress(count)
}

private fun saveCounts() {
Expand Down Expand Up @@ -179,4 +162,4 @@ class MainActivity : AppCompatActivity(), BackgroundPagerAdapter.OnImageClickLis
super.onDestroy()
mediaPlayer.release()
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/circular_progress_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<item android:id="@android:id/background">
<shape
android:shape="ring"
android:thicknessRatio="2.5"
android:thicknessRatio="3"
android:useLevel="false">
<solid android:color="@color/white" />
</shape>
Expand All @@ -14,7 +14,7 @@
android:toDegrees="270">
<shape
android:shape="ring"
android:thicknessRatio="2.5"
android:thicknessRatio="3"
android:useLevel="true">
<solid android:color="@color/progress_color" />
</shape>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/count_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="8dp" />
<stroke
android:width="1dp"
android:color="#CCCCCC" />
</shape>
61 changes: 31 additions & 30 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,42 @@
android:textSize="16sp" />

<RelativeLayout
android:id="@+id/countLayout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true">

<com.example.chantingapp.BeadProgressView
android:id="@+id/beadProgressView"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"/>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="108"
android:progress="0"
android:indeterminate="false"
android:progressDrawable="@drawable/circular_progress_bar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="20dp"
android:orientation="vertical"
android:gravity="center">

<TextView
android:id="@+id/countTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/black"
android:textSize="40sp" />

<TextView
android:id="@+id/countTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0"
android:textColor="@color/black"
android:textSize="50sp" />
<TextView
android:id="@+id/roundsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/black"
android:textSize="14sp" />

</LinearLayout>

<TextView
android:id="@+id/roundsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/countTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="-30dp"
android:text="0"
android:textColor="@color/black"
android:textSize="16sp" />
</RelativeLayout>

<TextView
Expand All @@ -81,4 +82,4 @@
android:textSize="18sp" />
</RelativeLayout>

</RelativeLayout>
</RelativeLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<color name="white">#FFFFFFFF</color>
<color name="Violet">#674FA5</color>
<color name="progress_color">#FF0000</color>
<color name="bead_filled">#FF4081</color>
<color name="bead_empty">#FFFFFFFF</color>


</resources>

0 comments on commit 25d2b13

Please sign in to comment.