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

ДЗ CustomView Зацепин #66

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

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
compileSdk 33
buildToolsVersion "33.0.2"

defaultConfig {
applicationId "otus.homework.customview"
minSdkVersion 23
targetSdkVersion 30
applicationId "ru.zatsoft"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -24,22 +25,36 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
}
buildFeatures {
viewBinding true
}
}

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'
constraints {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.20") {
because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
}
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.20") {
because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
}
}
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'androidx.activity:activity-ktx:1.7.2'
implementation 'androidx.navigation:navigation-fragment:2.6.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
10 changes: 6 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="otus.homework.customview">

<application
android:allowBackup="true"
android:name=".MyApp"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CustomView">
<activity android:name=".MainActivity">
android:theme="@style/Theme.CustomView"
tools:targetApi="31">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/otus/homework/customview/ListData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package otus.homework.customview

import android.os.Parcelable
import kotlinx.parcelize.Parcelize


@Parcelize
data class ListData(
var data: Map<String, Int>
) : Parcelable
37 changes: 35 additions & 2 deletions app/src/main/java/otus/homework/customview/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
package otus.homework.customview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import otus.homework.customview.databinding.ActivityMainBinding
import java.nio.charset.Charset

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
configureTabLayout()
val adapter = TabPagerAdapter(this, binding.tabLayout.tabCount)
binding.viewPager.adapter = adapter
TabLayoutMediator(binding.tabLayout, binding.viewPager)
{ tab, position ->
tab.text = MYTITLE.get(position)
}.attach()
}

private fun configureTabLayout() {
repeat(2) {
binding.tabLayout.addTab(binding.tabLayout.newTab())
}
}

companion object {
private val MYTITLE = listOf<String>("PIE CHART", "LINE CHART")
val myData = loadData()

fun loadData(): List<PayLoad> {
val gson = Gson()
val type = object : TypeToken<List<PayLoad>>() {}.type
MyApp.myResource.reset()
val myJson = MyApp.myResource.bufferedReader(Charset.defaultCharset())
return gson.fromJson(myJson, type)
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/otus/homework/customview/MyApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package otus.homework.customview

import android.app.Application
import java.io.InputStream

class MyApp : Application() {
companion object {
lateinit var myResource: InputStream
}

override fun onCreate() {
super.onCreate()
myResource = this.resources.openRawResource(R.raw.payload)
}

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

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class PayLoad(
val id: Long,
val name: String,
val amount: Int,
val category: String,
val time: Long
): Parcelable
33 changes: 33 additions & 0 deletions app/src/main/java/otus/homework/customview/Tab1Fragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package otus.homework.customview

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import otus.homework.customview.databinding.FragmentTab1Binding
import otus.homework.customview.piechart.ChartModel

class Tab1Fragment : Fragment(R.layout.fragment_tab1) {
val chartModel: ChartModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentTab1Binding.bind(view)
binding.chartView.chartModel = chartModel

val touchDown = binding.chartView._clickSector
touchDown.observe(viewLifecycleOwner, {
chartModel.setChecked(it)
chartModel.setScale(it)
binding.chartView.invalidate()
})
}

interface OnFragmentInteractionListener {
fun onFragmentInteraction(uri: Uri)
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/otus/homework/customview/Tab2Fragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package otus.homework.customview

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import otus.homework.customview.databinding.FragmentTab2Binding

class Tab2Fragment : Fragment(R.layout.fragment_tab2) {
val chartModel: LineChartModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentTab2Binding.bind(view)
binding.chartView.lineChartModel = chartModel

}

interface OnFragmentInteractionListener {
fun onFragmentInteraction(uri: Uri)
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/otus/homework/customview/TabPagerAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package otus.homework.customview

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

class TabPagerAdapter(
fa: FragmentActivity,
private var tabCount: Int
) : FragmentStateAdapter(fa) {
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> Tab1Fragment()
1 -> Tab2Fragment()
else -> Tab1Fragment()
}
}

override fun getItemCount(): Int {
return tabCount
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package otus.homework.customview

import androidx.lifecycle.ViewModel

class LineChartModel : ViewModel() {

var grafData: Map<Long, Int>
var maxAmount = 0
var maxTime = 0L
var minTime = 0L

init {

grafData = MainActivity.myData
.groupBy { it.time }
.mapValues {
it.value.map { it.amount }
.fold(0) { summ, time -> summ + time }
}
maxAmount = grafData.maxOf { it.value }
maxTime = grafData.maxOf { it.key }
minTime = grafData.minOf { it.key }
}
}
Loading