Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/src/main/java/otus/homework/customview/Expense.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package otus.homework.customview

import android.content.Context
import com.google.gson.Gson

data class Expense(
val id: Int,
val name: String,
val amount: Float,
val category: String,
val time: Long
)

fun Context.loadExpenses(): List<Expense> {
val input = resources.openRawResource(R.raw.payload)
val json = input.bufferedReader().use { it.readText() }
return Gson().fromJson(json, Array<Expense>::class.java).toList()
}

10 changes: 10 additions & 0 deletions app/src/main/java/otus/homework/customview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ package otus.homework.customview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val pie = findViewById<PieChartView>(R.id.pieChart)
pie.setExpenses(loadExpenses())

pie.listener = object : PieChartView.OnSliceClickListener {
override fun onCategoryClick(category: String) {
Toast.makeText(this@MainActivity, category, Toast.LENGTH_SHORT).show()
}
}
}
}
113 changes: 113 additions & 0 deletions app/src/main/java/otus/homework/customview/PieChartView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package otus.homework.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Bundle
import android.os.Parcelable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import kotlin.math.atan2
import kotlin.math.min

class PieChartView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : View(context, attrs) {

interface OnSliceClickListener {
fun onCategoryClick(category: String)
}

var listener: OnSliceClickListener? = null

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val rect = RectF()

private val colors = listOf(
Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.MAGENTA,
Color.YELLOW, Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.BLACK
)

private var data: Map<String, Float> = emptyMap()
private var angles = mutableListOf<Pair<String, Float>>()

fun setExpenses(expenses: List<Expense>) {
data = expenses.groupBy { it.category }
.mapValues { it.value.sumOf { e -> e.amount.toDouble() }.toFloat() }
calculateAngles()
invalidate()
}

private fun calculateAngles() {
angles.clear()
val total = data.values.sum()
data.forEach { (cat, value) ->
angles += cat to (value / total * 360f)
}
}

// --- onMeasure (учтены все MeasureSpec) ---
override fun onMeasure(w: Int, h: Int) {
val size = min(
MeasureSpec.getSize(w),
MeasureSpec.getSize(h)
)

val finalSize = when {
MeasureSpec.getMode(w) == MeasureSpec.EXACTLY -> MeasureSpec.getSize(w)
MeasureSpec.getMode(h) == MeasureSpec.EXACTLY -> MeasureSpec.getSize(h)
else -> size
}
setMeasuredDimension(finalSize, finalSize)
}

override fun onDraw(canvas: Canvas) {
var startAngle = -90f
rect.set(0f, 0f, width.toFloat(), height.toFloat())

angles.forEachIndexed { i, (cat, sweep) ->
paint.color = colors[i % colors.size]
canvas.drawArc(rect, startAngle, sweep, true, paint)
startAngle += sweep
}
}

// --- обработка клика ---
override fun onTouchEvent(e: MotionEvent): Boolean {
if (e.action != MotionEvent.ACTION_DOWN) return true

val cx = width / 2f
val cy = height / 2f
val angle = (Math.toDegrees(
atan2(e.y - cy, e.x - cx).toDouble()
) + 360 + 90) % 360

var acc = 0f
for ((cat, sweep) in angles) {
acc += sweep
if (angle <= acc) {
listener?.onCategoryClick(cat)
break
}
}
return true
}

// --- сохранение состояния ---
override fun onSaveInstanceState(): Parcelable =
Bundle().apply {
putParcelable("super", super.onSaveInstanceState())
putSerializable("data", HashMap(data))
}

override fun onRestoreInstanceState(state: Parcelable) {
val b = state as Bundle
super.onRestoreInstanceState(b.getParcelable("super"))
data = (b.getSerializable("data") as HashMap<String, Float>)
calculateAngles()
}
}
16 changes: 8 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<otus.homework.customview.PieChartView
android:id="@+id/pieChart"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Binary file added art/Screenshot_20260202_230456.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.