Skip to content

Commit

Permalink
Merge pull request #4 from Urdzik/No_Binding_Adapter
Browse files Browse the repository at this point in the history
Error Correction
  • Loading branch information
Slavik Urdzik authored Feb 7, 2020
2 parents 0ef23b4 + 24ada98 commit 4c885c7
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 156 deletions.
13 changes: 3 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store

.idea

/build
/captures
.externalNativeBuild
.cxx
91 changes: 7 additions & 84 deletions app/src/main/java/com/example/movieapp/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.movieapp

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.net.toUri
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -15,7 +14,12 @@ import com.example.movieapp.overview.MovieAdapter
@BindingAdapter("imageUrl")
fun bindImage(imageView: ImageView, imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
val imgUri = imgUrl
.toUri()
.buildUpon()
.scheme("https")
.build()

Glide.with(imageView.context)
.load(imgUri)
.apply(
Expand All @@ -27,70 +31,6 @@ fun bindImage(imageView: ImageView, imgUrl: String?) {
}
}

//Binding adapter used to display title from object
@BindingAdapter("title")
fun bindTitle(textView: TextView, title: String?) {
title?.let {
textView.text = title
}
}

//Binding adapter used to display plot from object
@BindingAdapter("plot")
fun TextView.bindPlot(plot: String?) {
plot?.let {
text = plot
}
}

//Binding adapter used to display year from object
@BindingAdapter("year")
fun TextView.bindYear(year: Int?) {
year?.let {
text = year.toString()
}
}

//Binding adapter used to display time from object
@BindingAdapter("time")
fun TextView.bindTime(time: String?) {
time?.let {
text = time
}
}

//Binding adapter used to display language from object
@BindingAdapter("language")
fun TextView.bindLanguage(language: String?) {
language?.let {
text = language
}
}

//Binding adapter used to display writer from object
@BindingAdapter("writer")
fun TextView.bindWriter(writer: String?) {
writer?.let {
text = writer
}
}

//Binding adapter used to display actors from object
@BindingAdapter("actors")
fun TextView.bindActors(actors: String?) {
actors?.let {
text = actors
}
}

//Binding adapter used to display genre from object
@BindingAdapter("genre")
fun TextView.bindGenre(genre: String?) {
genre?.let {
text = genre
}
}


//Binding adapter used to set adapter of RecyclerView
@BindingAdapter("listData")
Expand All @@ -108,21 +48,4 @@ fun hideIfNetworkError(view: View, isNetWorkError: Boolean, playlist: Any?) {
if (isNetWorkError) {
view.visibility = View.GONE
}
}

















}
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/movieapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class MainActivity : AppCompatActivity() {
val graph = inflater.inflate(R.navigation.navigation)
myNavHostFragment.navController.graph = graph
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ fun List<DatabaseMovie>.asDomainModel(): List<Movie> {

)
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/example/movieapp/detail/DetailFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.movieapp.databinding.DetailFragmentBinding
import java.util.*


class DetailFragment : Fragment() {
Expand All @@ -30,12 +29,13 @@ class DetailFragment : Fragment() {
// Create Toolbar and button of back in toolbar
val myToolbar = binding.toolbar

@Suppress("UNNECESSARY_SAFE_CALL")
Objects.requireNonNull((activity as AppCompatActivity)).apply {
(activity as AppCompatActivity).apply {
setSupportActionBar(myToolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setHomeButtonEnabled(true)
setDisplayShowTitleEnabled(false)
}
title = movie.title
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class DetailViewModel(movieProperty: Movie) : ViewModel() {
init {
_selectProperty.value = movieProperty
}
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/com/example/movieapp/domain/Movie.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.movieapp.domain

import android.os.Parcelable
import android.view.View
import kotlinx.android.parcel.Parcelize

/**
Expand All @@ -23,6 +24,6 @@ data class Movie(
val writer: String,
val actors: String
) : Parcelable {
val isR
get() = rated == "R"

val plus18 = if (rated == "R") View.GONE else View.VISIBLE
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/movieapp/network/MovieApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.movieapp.network

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

private const val BASE_URL = "https://api.myjson.com/bins/"

//Main entry point for network access
object MovieApi {

private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()

val retrofitService: MovieApiService = retrofit.create(MovieApiService::class.java)
}
15 changes: 0 additions & 15 deletions app/src/main/java/com/example/movieapp/network/MovieApiService.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.example.movieapp.network

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET

private const val BASE_URL = "https://api.myjson.com/bins/"

//A retrofit service to fetch movie playlist.
interface MovieApiService {
Expand All @@ -14,14 +10,3 @@ interface MovieApiService {
}


//Main entry point for network access
object MovieApi {

private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()

val retrofitService: MovieApiService = retrofit.create(MovieApiService::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ data class MovieProperty(
@Json(name = "Runtime") val time: String,
@Json(name = "Language") val language: String,
@Json(name = "Writer") val writer: String,
@Json(name = "Actors") val actors:String

@Json(name = "Actors") val actors: String
)

//Convert network result to database object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class MovieAdapter(private val onClickListener: ClickListener) :
}
}


//Class for comparing the old list and the new one, and updating it
class DiffCallback : DiffUtil.ItemCallback<Movie>() {
override fun areItemsTheSame(oldItem: Movie, newItem: Movie): Boolean {
Expand All @@ -50,5 +49,4 @@ class MovieAdapter(private val onClickListener: ClickListener) :
class ClickListener(val clickListener: (movie: Movie) -> Unit) {
fun onClick(movie: Movie) = clickListener(movie)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class OverviewFragment : Fragment() {
})

//Looking for the internet connection
viewModel.eventNetworkError.observe(
viewLifecycleOwner,
Observer<Boolean> { isNetworkError ->
viewModel.eventNetworkError.observe(viewLifecycleOwner, Observer<Boolean> { isNetworkError ->
if (isNetworkError) onNetworkError()
})

Expand All @@ -71,4 +69,4 @@ class OverviewFragment : Fragment() {
viewModel.onNetworkErrorShown()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.example.movieapp.domain.Movie
import com.example.movieapp.repository.MoviesRepository
import kotlinx.coroutines.launch


class OverviewViewModel(application: Application) : ViewModel() {

//LiveData object of movie
Expand Down Expand Up @@ -42,7 +41,7 @@ class OverviewViewModel(application: Application) : ViewModel() {
_eventNetworkError.value = false
_isNetworkErrorShown.value = false
} catch (e: Exception) {
if (playList.value!!.isEmpty()) {
if (playList.value.isNullOrEmpty()) {
_eventNetworkError.value = true
}
}
Expand All @@ -60,4 +59,4 @@ class OverviewViewModel(application: Application) : ViewModel() {
fun onNetworkErrorShown() {
_isNetworkErrorShown.value = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class MoviesRepository(private val movieDatabase: MovieDatabase) {
val movies: LiveData<List<Movie>> = Transformations.map(movieDatabase.movieDao.getMovies()) {
it.asDomainModel()
}
}
}
Loading

0 comments on commit 4c885c7

Please sign in to comment.