Skip to content

Commit

Permalink
Весь код покрыт коментами
Browse files Browse the repository at this point in the history
Исправлен недочёт с toolbar
Удалены лишние пробелы
  • Loading branch information
Slavik Urdzik committed Jan 30, 2020
1 parent aeceea5 commit fdf5255
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 32 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/example/movieapp/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.bumptech.glide.request.RequestOptions
import com.example.movieapp.domain.Movie
import com.example.movieapp.overview.MovieAdapter

//Binding adapter used to display images from URL using Glide
@BindingAdapter("imageUrl")
fun bindImage(imageView: ImageView, imgUrl: String?) {
imgUrl?.let {
Expand All @@ -26,34 +27,40 @@ 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 set adapter of RecyclerView
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<Movie>?) {
val adapter = recyclerView.adapter as MovieAdapter
adapter.submitList(data)
data?.let {
val adapter = recyclerView.adapter as MovieAdapter
adapter.submitList(data)
}
}


//Binding adapter used to hide the spinner once data is available.
@BindingAdapter("isNetworkError", "playlist")
fun hideIfNetworkError(view: View, isNetWorkError: Boolean, playlist: Any?) {
view.visibility = if (playlist != null) View.GONE else View.VISIBLE
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/movieapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//Function for Light-Dark theme
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ class DetailFragment : Fragment() {

return binding.root
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import androidx.lifecycle.ViewModelProvider
import com.example.movieapp.domain.Movie

class DetailViewModelFactory(private val movie: Movie) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(DetailViewModel::class.java)) {
return DetailViewModel(movie) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import retrofit2.http.GET

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

//A retrofit service to fetch movie playlist.
interface MovieApiService {
@GET("19tvhm")
suspend fun getPropertyAsync(): List<MovieProperty>
}


//Main entry point for network access
object MovieApi {

private val retrofit = Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.example.movieapp.database.DatabaseMovie
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass



//
//Network object of movie for REST
@JsonClass(generateAdapter = true)
data class MovieProperty(
@Json(name = "imdbID") val id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.example.movieapp.domain.Movie
class MovieAdapter(private val onClickListener: ClickListener) :
ListAdapter<Movie, MovieAdapter.MovieViewHolder>(DiffCallback()) {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
return MovieViewHolder(
ItemBinding.inflate(
Expand All @@ -28,14 +27,15 @@ class MovieAdapter(private val onClickListener: ClickListener) :
holder.bind(item)
}

class MovieViewHolder(private val binding: ItemBinding) :
RecyclerView.ViewHolder(binding.root) {
class MovieViewHolder(private val binding: ItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(movie: Movie) {
binding.movie = movie
binding.executePendingBindings()
}
}


//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 {
return oldItem == newItem
Expand All @@ -44,10 +44,9 @@ class MovieAdapter(private val onClickListener: ClickListener) :
override fun areContentsTheSame(oldItem: Movie, newItem: Movie): Boolean {
return newItem.id == oldItem.id
}

}


//Class for click by element
class ClickListener(val clickListener: (movie: Movie) -> Unit) {
fun onClick(movie: Movie) = clickListener(movie)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,57 @@ import com.example.movieapp.databinding.OverviewFragmentBinding

class OverviewFragment : Fragment() {


private val viewModel: OverviewViewModel by lazy {
val activity = requireNotNull(this.activity) {}
ViewModelProvider(this, OverviewViewModelFactory(activity.application))
.get(OverviewViewModel::class.java)
}


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val binding = OverviewFragmentBinding.inflate(inflater)
val myToolbar = binding.myToolbar
(activity as AppCompatActivity).setSupportActionBar(myToolbar)

//Toolbar
val myToolbar = binding.myToolbar

binding.lifecycleOwner = viewLifecycleOwner
(activity as AppCompatActivity).apply {
setSupportActionBar(myToolbar)
title = "Movie App"
}

binding.viewModel = viewModel

//Listener of recycler view click
binding.recycler.adapter = MovieAdapter(MovieAdapter.ClickListener {
viewModel.displayPropertyDetails(it)
})

//Navigate to Detail Activity
viewModel.navigateToSelectProperty.observe(viewLifecycleOwner, Observer {
if (it != null) {
this.findNavController()
.navigate(OverviewFragmentDirections.actionOverviewFragmentToDetailFragment(it))
viewModel.displayPropertyDetailsComlited()
viewModel.displayPropertyDetailsCompleted()
}

})

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

binding.lifecycleOwner = viewLifecycleOwner
binding.viewModel = viewModel

return binding.root
}


//Function will show a toast when there is no internet
private fun onNetworkError() {
if (!viewModel.isNetworkErrorShown.value!!) {
Toast.makeText(activity, "Network error", Toast.LENGTH_LONG).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@ import kotlinx.coroutines.launch

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

//LiveData object of movie
private val _navigateToSelectProperty = MutableLiveData<Movie>()
val navigateToSelectProperty: LiveData<Movie>
get() = _navigateToSelectProperty

//LiveData for show Progress Bar
private var _eventNetworkError = MutableLiveData<Boolean>(false)
val eventNetworkError: LiveData<Boolean>
get() = _eventNetworkError

//LiveData for show internet error
private var _isNetworkErrorShown = MutableLiveData<Boolean>(false)
val isNetworkErrorShown: LiveData<Boolean>
get() = _isNetworkErrorShown


private val moviesRepository = MoviesRepository(getDatabase(application))
val playList = moviesRepository.movies

init {
getMovieListProperty()
getMovieList()
}


private fun getMovieListProperty() {
private fun getMovieList() {
viewModelScope.launch {
try {
moviesRepository.refreshMovie()
Expand All @@ -52,13 +53,11 @@ class OverviewViewModel(application: Application) : ViewModel() {
_navigateToSelectProperty.value = movie
}

fun displayPropertyDetailsComlited() {
fun displayPropertyDetailsCompleted() {
_navigateToSelectProperty.value = null
}

fun onNetworkErrorShown() {
_isNetworkErrorShown.value = true
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class OverviewViewModelFactory(private val application: Application) : ViewModelProvider.Factory {
class OverviewViewModelFactory(private val app: Application) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(OverviewViewModel::class.java)) {
return OverviewViewModel(application) as T
return OverviewViewModel(app) as T
}
throw ExceptionInInitializerError("Unknown viewModel Class")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MoviesRepository(private val movieDatabase: MovieDatabase) {
//Refresh the videos stored in the offline cache.
suspend fun refreshMovie() {
withContext(Dispatchers.IO) {
val playList = MovieApi.retrofitService.getPropertyAsync()
movieDatabase.movieDao.insert(playList.asDatabaseModal())
}
}

//Transformation database object to movie object
val movies: LiveData<List<Movie>> = Transformations.map(movieDatabase.movieDao.getMovies()) {
it.asDomainModel()
}
Expand Down

0 comments on commit fdf5255

Please sign in to comment.