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
28 changes: 15 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'org.jetbrains.kotlin.plugin.serialization'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
compileSdk 35

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

Expand All @@ -24,22 +24,24 @@ 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'
}
namespace 'otus.homework.customview'
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.core:core-ktx:1.15.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0-RC'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="otus.homework.customview">
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
Expand All @@ -10,7 +9,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
18 changes: 18 additions & 0 deletions app/src/main/java/otus/homework/customview/ExpensesInfoItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package otus.homework.customview

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ExpensesInfoItem(
@SerialName("id")
val id: Int = 0,
@SerialName("name")
val name: String = "",
@SerialName("amount")
val amount: Int = 0,
@SerialName("category")
val category: String = "",
@SerialName("time")
val time: Long = 0
)
16 changes: 16 additions & 0 deletions app/src/main/java/otus/homework/customview/ExpensesRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package otus.homework.customview

import android.content.Context
import kotlinx.serialization.json.Json


class ExpensesRepository(private val context: Context) {
fun getExpenses(): List<ExpensesInfoItem> {
val payloadString = context.resources.openRawResource(R.raw.payload).bufferedReader().use { it.readText() }
return Json.decodeFromString<List<ExpensesInfoItem>>(payloadString)
}

fun getExpensesByCategory(category: String): List<ExpensesInfoItem> {
return getExpenses().filter { it.category == category }
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/otus/homework/customview/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package otus.homework.customview

import android.content.Context
import android.graphics.Canvas
import android.text.StaticLayout
import android.util.TypedValue
import androidx.core.graphics.withTranslation

fun Context.dpToPx(dp: Int): Float {
return dp.toFloat() * this.resources.displayMetrics.density
}

fun Context.spToPx(sp: Int): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp.toFloat(),
this.resources.displayMetrics
)
}

fun StaticLayout.draw(canvas: Canvas, x: Float, y: Float) {
canvas.withTranslation(x, y) {
draw(this)
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/otus/homework/customview/LineChartFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package otus.homework.customview

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

private const val CATEGORY = "category"
private const val COLOR = "color"

class LineChartFragment : Fragment() {
private var category: String? = null
private var color: Int = Color.RED

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
category = it.getString(CATEGORY)
color = it.getInt(COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
return inflater.inflate(R.layout.fragment_line_chart, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val lineChart = view.findViewById<LineChartView>(R.id.line_chart)
category?.let {
lineChart.setDataAndColor(
ExpensesRepository(requireContext()).getExpensesByCategory(it),
color
)
}
}

companion object {
@JvmStatic
fun newInstance(category: String, color: Int) =
LineChartFragment().apply {
arguments = Bundle().apply {
putString(CATEGORY, category)
putInt(COLOR, color)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package otus.homework.customview

interface LineChartInterface {
fun setDataAndColor(expensesInfoList: List<ExpensesInfoItem>, color: Int)
}
Loading