Skip to content

Homework6 card view #6

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

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
24 changes: 19 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'androidx.navigation.safeargs.kotlin'
}

android {
compileSdk 30
compileSdk 31

defaultConfig {
applicationId "com.itis.androidlabproject"
minSdk 23
targetSdk 30
targetSdk 31
versionCode 1
versionName "1.0"

Expand All @@ -36,13 +38,25 @@ android {
}

dependencies {
// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

implementation 'androidx.core:core-ktx:1.6.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.2"

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'android.arch.navigation:navigation-fragment:2.3.5'
implementation 'android.arch.navigation:navigation-ui:2.3.5'

implementation 'androidx.recyclerview:recyclerview:1.2.1'

implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itis.androidlabproject">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand All @@ -10,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLabProject">
<activity
android:name=".MainActivity"
android:name=".activity.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/com/itis/androidlabproject/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.itis.androidlabproject.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.ui.setupWithNavController
import com.itis.androidlabproject.R
import com.itis.androidlabproject.databinding.ActivityMainBinding
import com.itis.androidlabproject.extension.findNavController

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater).also {
setContentView(it.root)
}

navController = findNavController(R.id.fragment_container)

binding.btnNavView.setupWithNavController(navController)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.itis.androidlabproject.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.RequestManager
import com.itis.androidlabproject.model.Planet

class PlanetAdapter(
private val list: ArrayList<Planet>,
private val glide: RequestManager,
private val action: (Int) -> Unit
) : RecyclerView.Adapter<PlanetHolder>() {

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PlanetHolder = PlanetHolder.create(parent, glide, action)

override fun onBindViewHolder(holder: PlanetHolder, position: Int) {
holder.bind(list[position])
}

override fun getItemCount(): Int = list.size;

fun updateData(newData: List<Planet>) {
val callback = PlanetDiffUtils(list, newData)
val diffResult = DiffUtil.calculateDiff(callback)
diffResult.dispatchUpdatesTo(this)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше через листадаптер, ну ладно


list.clear()
list.addAll(newData)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.itis.androidlabproject.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.RequestManager
import com.itis.androidlabproject.model.Planet

class PlanetCardViewAdapter(
private val list: List<Planet>,
private val glide: RequestManager,
) : RecyclerView.Adapter<PlanetCardViewHolder>() {

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PlanetCardViewHolder = PlanetCardViewHolder.create(parent, glide)

override fun onBindViewHolder(holder: PlanetCardViewHolder, position: Int) {
holder.bind(list[position])
}

override fun getItemCount(): Int = list.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.itis.androidlabproject.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Priority
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.itis.androidlabproject.databinding.ItemCvPlanetBinding
import com.itis.androidlabproject.model.Planet

class PlanetCardViewHolder(
private val binding: ItemCvPlanetBinding,
private val glide: RequestManager,
) : RecyclerView.ViewHolder(binding.root) {

private var album: Planet? = null

private val options = RequestOptions()
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL)

fun bind(item: Planet) {
this.album = item
with(binding) {
tvName1.text = item.name
tvName2.text = item.name
tvNumberOfSatellite.text = "Количество спутников: ${item.numberOfSatellite}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше через strings.xml

tvDesc.text = item.description

glide.load(item.url)
.apply(options)
.into(ivImage1)
vp2Images.adapter = ViewPagerAdapter(item.url, glide)
}

}

companion object {
fun create(
parent: ViewGroup,
glide: RequestManager,
) = PlanetCardViewHolder(
ItemCvPlanetBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
), glide
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.itis.androidlabproject.adapter

import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import com.itis.androidlabproject.model.Planet

class PlanetDiffItemCallback : DiffUtil.ItemCallback<Planet>() {
override fun areItemsTheSame(
oldItem: Planet,
newItem: Planet
): Boolean = oldItem.id == newItem.id

override fun areContentsTheSame(
oldItem: Planet,
newItem: Planet
): Boolean = oldItem == newItem

override fun getChangePayload(oldItem: Planet, newItem: Planet): Any? {
var bundle = Bundle()

if (oldItem.name != newItem.name) {
bundle.putString("NAME", newItem.name)
}
if (oldItem.numberOfSatellite != newItem.numberOfSatellite) {
bundle.putInt("NUMBER_OF_SATELLITE", newItem.numberOfSatellite)
}
if (oldItem.description != newItem.description) {
bundle.putString("DESCRIPTION", newItem.description)
}
if (oldItem.url != newItem.url) {
bundle.putString("URL", newItem.url)
}
if (bundle.isEmpty) return null
return bundle
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.itis.androidlabproject.adapter

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.DiffUtil
import com.itis.androidlabproject.model.Planet

class PlanetDiffUtils(
private var oldList: List<Planet>,
private var newList: List<Planet>
) : DiffUtil.Callback() {
override fun getOldListSize(): Int = oldList.size

override fun getNewListSize(): Int = newList.size

override fun areItemsTheSame(
oldItemPosition: Int,
newItemPosition: Int,
): Boolean =
oldList[oldItemPosition].id == newList[newItemPosition].id

override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int,
): Boolean = oldList[oldItemPosition] == newList[newItemPosition]

override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
var oldPlanet = oldList[oldItemPosition]
var newPlanet = newList[newItemPosition]

var bundle = Bundle()

if (oldPlanet.name != newPlanet.name) {
bundle.putString("NAME", newPlanet.name)
}
if (oldPlanet.numberOfSatellite != newPlanet.numberOfSatellite) {
bundle.putInt("NUMBER_OF_SATELLITE", newPlanet.numberOfSatellite)
}
if (oldPlanet.description != newPlanet.description) {
bundle.putString("DESCRIPTION", newPlanet.description)
}
if (oldPlanet.url != newPlanet.url) {
bundle.putString("URL", newPlanet.url)
}
if (bundle.isEmpty) return null

return bundle
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.itis.androidlabproject.adapter

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Priority
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.itis.androidlabproject.databinding.ItemPlanetBinding
import com.itis.androidlabproject.model.Planet
import com.itis.androidlabproject.repository.PlanetRepository

class PlanetHolder(
private val binding: ItemPlanetBinding,
private val glide: RequestManager,
private val action: (Int) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
private var planet: Planet? = null

private val options = RequestOptions()
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL)

// init {
// itemView.setOnClickListener {
// planet?.run {
// action(this.id)
// }
// }
// }

@SuppressLint("SetTextI18n")
fun bind(item: Planet) {
this.planet = item
with(binding) {
tvDetName.text = item.name
tvNumberOfSatellite.text = "Количество спутников: ${item.numberOfSatellite}"

glide.load(item.url)
.apply(options)
.into(ivImage)

btnDelete.setOnClickListener {
action(PlanetRepository.getIndex(item))
}
}
}

fun updateFields(bundle: Bundle) {
bundle.run {
getString("NAME")?.also {
updateName(it)
}
getInt("NUMBER_OF_SATELLITE").also {
updateNumberOfSatellite(it)
}
}
}

fun updateName(newName: String) {
binding.tvDetName.text = newName
}

@SuppressLint("SetTextI18n")
fun updateNumberOfSatellite(newNumberOfSatellite: Int) {
binding.tvNumberOfSatellite.text = "Количество спутников: $newNumberOfSatellite"
}

companion object {
fun create(
parent: ViewGroup,
glide: RequestManager,
action: (Int) -> Unit
) = PlanetHolder(
ItemPlanetBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
),
glide,
action
)
}
}
Loading