Skip to content
This repository has been archived by the owner. It is now read-only.
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
3 changes: 3 additions & 0 deletions HomeWorkNetwork/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions HomeWorkNetwork/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ plugins {
android {
compileSdk 31

packagingOptions {
pickFirst 'META-INF/DEPENDENCIES'
pickFirst 'META-INF/INDEX.LIST'
pickFirst 'META-INF/io.netty.versions.properties'
}

defaultConfig {

buildConfigField "String", "API_BASE_URL", '"api.themoviedb.org"'
buildConfigField "String", "API_KEY", '"/*ваш ap_key*/"'
buildConfigField "String", "API_KEY", '"6259c72d2e1c19b81663e5e9e9ef7356"'
buildConfigField "String", "API_IMAGE_BASE_URL", '"https://image.tmdb.org/t/p/w500"'

applicationId "com.pg.homeworknetwork"
minSdk 30
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -46,4 +52,22 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"

//Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'

//Interceptor
implementation 'com.squareup.okhttp3:logging-interceptor:4.8.1'

// ktor
implementation 'io.ktor:ktor-client-core:1.6.7'
implementation 'io.ktor:ktor-client-android:1.6.7'
implementation 'io.ktor:ktor-client-serialization:1.6.7'
implementation 'io.ktor:ktor-client-logging:1.6.7'
implementation 'io.ktor:ktor-client-cio:1.6.7'
implementation 'io.ktor:ktor-server-core:1.6.7'
implementation 'io.ktor:ktor-server-netty:1.6.7'
}
31 changes: 29 additions & 2 deletions HomeWorkNetwork/app/src/main/java/com/pg/homeworknetwork/Models.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package com.pg.homeworknetwork

class Movies
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

class Movie
//https://developers.themoviedb.org/3/movies/get-movie-lists
@Serializable
data class Movies(
@SerialName("page")
val page: Int,
@SerialName("results")
val results: List<Movie> = ArrayList()
)

//https://developers.themoviedb.org/3/movies/get-movie-details
@Serializable
data class Movie(
@SerialName("id")
val id: Int,
@SerialName("title")
val title: String? = null,
@SerialName("poster_path")
val posterPath: String? = null,
@SerialName("overview")
val overview: String? = null,
@SerialName("popularity")
val popularity: Double? = null,
@SerialName("original_title")
val originalTitle: String? = null,
@SerialName("release_date")
val releaseDate: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import coil.load
import coil.transform.RoundedCornersTransformation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class MovieDetailFragment : Fragment(R.layout.fragment_movie_preview) {
lateinit var poster: ImageView
Expand All @@ -18,6 +22,8 @@ class MovieDetailFragment : Fragment(R.layout.fragment_movie_preview) {
lateinit var popularity: TextView
lateinit var releaseDate: TextView

private val corScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(view) {
Expand All @@ -28,24 +34,29 @@ class MovieDetailFragment : Fragment(R.layout.fragment_movie_preview) {
releaseDate = findViewById(R.id.releaseDate)
}

val movieId = arguments?.getInt(ARG_ID) ?: 550
val movie = //получаем фильм
poster.load("${BuildConfig.API_IMAGE_BASE_URL}${movie.posterPath}") {
transformations(RoundedCornersTransformation(16f))
}
originalTitle.text = movie.originalTitle
overview.text = movie.overview
popularity.text = movie.popularity.toString()
releaseDate.text = movie.releaseDate

activity?.onBackPressedDispatcher?.addCallback(this.viewLifecycleOwner, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val manager: FragmentManager = parentFragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
transaction.replace(R.id.mainFragment, MovieListFragment())
transaction.commit()
corScope.launch {
val movieId = arguments?.getInt(ARG_ID) ?: 550
val movie = Api().getMovieById(movieId) //получаем фильм
poster.load("${BuildConfig.API_IMAGE_BASE_URL}${movie.posterPath}") {
transformations(RoundedCornersTransformation(16f))
}
})
originalTitle.text = movie.originalTitle
overview.text = movie.overview
popularity.text = movie.popularity.toString()
releaseDate.text = movie.releaseDate
}


activity?.onBackPressedDispatcher?.addCallback(
this.viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val manager: FragmentManager = parentFragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
transaction.replace(R.id.mainFragment, MovieListFragment())
transaction.commit()
}
})
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class MovieItemAdapter : ListAdapter<Movie, MovieItemAdapter.ViewHolder
clickListener?.onItemClick(movie)
}
image.requestLayout()
itemTitle.text = movie.title
itemTitle.text = movie.originalTitle

image.load("${BuildConfig.API_IMAGE_BASE_URL}${movie.posterPath}") {
transformations(RoundedCornersTransformation(16f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class MovieListFragment : Fragment(R.layout.fragment_movie_list) {
lateinit var recycler: RecyclerView

private val corScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

private val goToDetails = object : MovieItemAdapter.IOnItemClick {
override fun onItemClick(movie: Movie) {
val manager: FragmentManager = parentFragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
val detailsFragment = MovieDetailFragment()
detailsFragment.arguments = Bundle().apply { putInt(MovieDetailFragment.ARG_ID, movie.id) }
detailsFragment.arguments =
Bundle().apply { putInt(MovieDetailFragment.ARG_ID, movie.id) }
transaction.replace(R.id.mainFragment, detailsFragment)
transaction.commit()
}
Expand All @@ -31,8 +38,10 @@ class MovieListFragment : Fragment(R.layout.fragment_movie_list) {
}
}
val adapter = (recycler.adapter as MovieItemAdapter)
val movies = //получаем фильмы
adapter.submitList(movies)
corScope.launch(Dispatchers.IO) {
val movies: Movies = Api().getTopRatedMovies() //получаем фильмы
adapter.submitList(movies.results)
}
super.onViewCreated(view, savedInstanceState)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
package com.pg.homeworknetwork

import io.ktor.client.*
import io.ktor.client.engine.android.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
import io.ktor.client.request.*

// https://api.themoviedb.org/3/movie/{movie_id}?api_key=API_KEY (6259c72d2e1c19b81663e5e9e9ef7356)
// https://api.themoviedb.org/3/movie/550?api_key=6259c72d2e1c19b81663e5e9e9ef7356

class Api {
companion object {
private const val BASE_URL = BuildConfig.API_BASE_URL
const val TOP_RATED_MOVIES = "3/movie/top_rated"
const val MOVIE_DETAILS = "3/movie/"
}

private val client = HttpClient(Android) {
install(Logging) {
level = LogLevel.ALL
}
install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
}

suspend fun getTopRatedMovies(): Movies {
return client.use {
it.get(host = BASE_URL, path = TOP_RATED_MOVIES) {
parameter("api_key", BuildConfig.API_KEY)
}
}
}

suspend fun getMovieById(id: Int): Movie {
return client.use {
it.get(host = BASE_URL, path = MOVIE_DETAILS + id.toString()) {
parameter("api_key", BuildConfig.API_KEY)
}
}
}
}
10 changes: 5 additions & 5 deletions HomeWorkNetwork/app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HomeWorkNetwork" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.HomeWorkNetwork" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:statusBarColor" tools:targetApi="l">#45000000</item>
<!-- Customize your theme here. -->
</style>
</resources>
4 changes: 2 additions & 2 deletions HomeWorkNetwork/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HomeWorkNetwork" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.HomeWorkNetwork" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand All @@ -10,7 +10,7 @@
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:statusBarColor" tools:targetApi="l">#45000000</item>
<!-- Customize your theme here. -->
</style>
</resources>