-
Notifications
You must be signed in to change notification settings - Fork 0
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
Karakurik
wants to merge
3
commits into
master
Choose a base branch
from
homework6-card-view
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
app/src/main/java/com/itis/androidlabproject/MainActivity.kt
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/itis/androidlabproject/activity/MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/itis/androidlabproject/adapter/PlanetAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
list.clear() | ||
list.addAll(newData) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/itis/androidlabproject/adapter/PlanetCardViewAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/itis/androidlabproject/adapter/PlanetCardViewHolder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/itis/androidlabproject/adapter/PlanetDiffItemCallback.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/itis/androidlabproject/adapter/PlanetDiffUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
app/src/main/java/com/itis/androidlabproject/adapter/PlanetHolder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше через листадаптер, ну ладно