Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HomeWork_07: Виталий Чупин #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 7 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
compileSdkVersion 34
namespace = "otus.homework.customview"

defaultConfig {
applicationId "otus.homework.customview"
minSdkVersion 23
targetSdkVersion 30
targetSdkVersion 34
versionCode 1
versionName "1.0"

Expand All @@ -24,11 +24,11 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}
}

Expand All @@ -40,6 +40,7 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
implementation 'com.google.code.gson:gson:2.10.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CustomView">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
164 changes: 164 additions & 0 deletions app/src/main/java/otus/homework/customview/GraphBarView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package otus.homework.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import otus.homework.customview.data.GraphBarModel

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

private var maxPrice: Int = 2000
private val stepsPrice: Int = 5
private val stepsDate: Int = 16
private val maxVerticalLine = 500f
private val paddingHorizontal = 50f
private var list: ArrayList<GraphBarModel> = arrayListOf()

private val paintText = Paint().apply {
color = Color.GRAY
}

private val paintGrid = Paint().apply {
color = Color.LTGRAY
strokeWidth = 2f
style = Paint.Style.STROKE
}

private val paintLine = Paint().apply {
strokeWidth = 8f
style = Paint.Style.STROKE
strokeCap = Paint.Cap.ROUND
strokeJoin = Paint.Join.ROUND
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val wMode = MeasureSpec.getMode(widthMeasureSpec)
val hMode = MeasureSpec.getMode(heightMeasureSpec)
val wSize = MeasureSpec.getSize(widthMeasureSpec)
val hSize = MeasureSpec.getSize(heightMeasureSpec)

when (wMode) {
MeasureSpec.EXACTLY -> {
setMeasuredDimension(wSize, hSize)
}

MeasureSpec.AT_MOST -> {
setMeasuredDimension(wSize, hSize)
}

MeasureSpec.UNSPECIFIED -> {
setMeasuredDimension(wSize, hSize)
}
}
}

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

drawPrice(canvas)
}

fun setMaxPrice(newMaxPrice: Int) {
maxPrice = newMaxPrice
}

private fun drawPrice(canvas: Canvas) {
val stepPriceForGrid = maxPrice / stepsPrice.minus(1)
val stepDateForGrid = (measuredWidth - paddingHorizontal.times(2)) / stepsDate.minus(1)

repeat(stepsPrice) {
val sum = maxPrice - stepPriceForGrid.times(it)
canvas.drawLine(
0f + paddingHorizontal,
100f + it * 100f,
measuredWidth.toFloat() - paddingHorizontal,
100f + it * 100f,
paintGrid
)
canvas.drawText(
"$sum ₽",
measuredWidth - 100f,
50f + it * 100f + paddingHorizontal.div(2),
paintText.apply { textSize = 32f })
}

repeat(stepsDate) {
canvas.drawLine(
(it * stepDateForGrid) + paddingHorizontal,
0f + paddingHorizontal.times(2),
(it * stepDateForGrid) + paddingHorizontal,
maxVerticalLine,
paintGrid
)
canvas.drawText(
if (it == 0) "1" else "${((it + 1) * 2) - 1}",
(it * stepDateForGrid) + paddingHorizontal - paddingHorizontal.div(4),
maxVerticalLine.plus(100),
paintText.apply { textSize = 28f })
}

list.map { data ->
val listDays = data.listCategory.map { it.day }
paintLine.apply {
color = data.color
}

val path = Path()

if (data.listCategory.first().day == 1) {
val sumFirst = data.listCategory.first().sum
val percentFirst = sumFirst.toFloat().div(maxPrice.toFloat())
path.moveTo(
0f + paddingHorizontal,
(maxVerticalLine) - maxVerticalLine.minus(100f).times(percentFirst)
)

} else
path.moveTo(0f + paddingHorizontal, maxVerticalLine)

repeat(stepsDate.times(2).minus(1)) { repeatInt ->
if (listDays.contains(repeatInt.plus(1))) {
val currSum = data.listCategory.find { it.day == repeatInt.plus(1) }?.sum
val percent = currSum?.toFloat()?.div(maxPrice.toFloat()) ?: 0f
path.quadTo(
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
(maxVerticalLine) - maxVerticalLine.minus(100f).times(percent),
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
(maxVerticalLine) - maxVerticalLine.minus(100f).times(percent)
)
path.moveTo(
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
(maxVerticalLine) - maxVerticalLine.minus(100f).times(percent)
)
} else {
path.quadTo(
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
maxVerticalLine,
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
maxVerticalLine
)
path.moveTo(
(repeatInt * (stepDateForGrid / 2)) + paddingHorizontal,
maxVerticalLine
)
}
}

canvas.drawPath(path, paintLine)
}
}

fun setList(newList: List<GraphBarModel>) {
list.clear()
list.addAll(newList)
requestLayout()
invalidate()
}
}
Loading