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

Custom view #38

Open
wants to merge 5 commits 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
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
}

android {
Expand Down Expand Up @@ -42,4 +43,7 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.jakewharton.threetenabp:threetenabp:1.3.0'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CustomView">
<activity android:name=".MainActivity">
<activity android:name=".views.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/otus/homework/customview/MainActivity.kt

This file was deleted.

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

import android.graphics.Color
import android.os.Parcelable
import androidx.annotation.ColorInt
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import otus.homework.customview.utils.ColorGenerator

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

@Parcelize
data class PieChartSegment(
val id: Int,
val name: String,
val amount: Float,
val category: String,
val startAngle: Float,
val endAngle: Float,
val percentageOfMaximum: Float,
@ColorInt val color: Int
): Parcelable {

@IgnoredOnParcel
val segmentAngle = endAngle - startAngle
}

@Parcelize
data class LinearChartPoint(
val id: Int,
val name: String,
val amount: Float,
val category: String,
val time: Long,
val dayInMonth: Int
) : Parcelable {

fun mapExpenditureCategory() = when (category) {
"Продукты" -> ExpenditureCategory.PRODUCTS
"Здоровье" -> ExpenditureCategory.HEALTH
"Кафе и рестораны" -> ExpenditureCategory.EATING_OUT
"Алкоголь" -> ExpenditureCategory.ALCOHOL
"Доставка еды" -> ExpenditureCategory.DELIVERY
"Транспорт" -> ExpenditureCategory.TRANSPORT
"Спорт" -> ExpenditureCategory.SPORT
else -> throw IllegalArgumentException("Error while parsing unknown category: $category")
}
}

@Parcelize
enum class ExpenditureCategory : Parcelable {
PRODUCTS, HEALTH, EATING_OUT, ALCOHOL, DELIVERY, TRANSPORT, SPORT;

@ColorInt
fun getChartColor(): Int {
return when (this) {
PRODUCTS -> Color.rgb(38, 50, 124)
HEALTH -> Color.rgb(100, 188, 223)
EATING_OUT -> Color.rgb(230, 197, 87)
TRANSPORT -> Color.rgb(202, 58, 46)
else -> ColorGenerator.generateColor()
}
}
}

enum class Chart {
PIE, LINEAR
}
14 changes: 14 additions & 0 deletions app/src/main/java/otus/homework/customview/utils/ColorGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package otus.homework.customview.utils

import android.graphics.Color
import androidx.annotation.ColorInt
import java.util.*

object ColorGenerator {

@ColorInt
fun generateColor(): Int {
val rnd = Random()
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/otus/homework/customview/utils/Extention.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package otus.homework.customview.utils

import android.content.res.Resources
import android.util.TypedValue

val Number.toPx
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
Loading