Skip to content

Commit

Permalink
Merge pull request #29 from Urdzik/feature/save-user-favorite-movie
Browse files Browse the repository at this point in the history
Feature/save user favorite movie
  • Loading branch information
Slavik Urdzik authored May 27, 2020
2 parents f016d48 + ce4b77e commit 942c3d7
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 51 deletions.
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ dependencies {
// UI
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation 'com.yarolegovich:discrete-scrollview:1.4.9'
implementation "androidx.fragment:fragment-ktx:1.3.0-alpha04"
implementation "androidx.fragment:fragment-ktx:1.3.0-alpha05"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha06"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha06"
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-beta01"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-beta01"

// Coroutines for getting off the UI thread
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5"
Expand All @@ -86,20 +86,20 @@ dependencies {
//Firebase
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.android.gms:play-services-auth:18.0.0'
implementation 'com.google.firebase:firebase-database-ktx:19.3.0'
implementation 'com.google.firebase:firebase-firestore-ktx:21.4.3'

// arch components
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha03'

// glide for images
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'

//Database
implementation "androidx.room:room-runtime:2.2.4"
kapt "androidx.room:room-compiler:2.2.4"
// //Database
// implementation "androidx.room:room-runtime:2.2.4"
// kapt "androidx.room:room-compiler:2.2.4"

//Dagger 2
def dagger_version = '2.26'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.movieapp.model.network.data

import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize

/** Data class for Retrofit request from OverviewFragment and getting small data of movie */

Expand All @@ -9,10 +11,13 @@ data class SmallMovie(
@SerializedName("page") val page: Int
)

data class SmallMovieList(

data class SmallMovieList constructor(
@SerializedName("id") val id: Int,
@SerializedName("poster_path") val posterPath: String,
@SerializedName("title") val title: String,
@SerializedName("vote_average")val voteAverage: Float,
@SerializedName("backdrop_path") val backdropPath: String
)
){
constructor(): this(0,"","", 0.0F, "")
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.example.movieapp.ui.home.detail


import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.movieapp.dagger.App
import com.example.movieapp.dagger.module.viewModule.ViewModelFactory
import com.example.movieapp.databinding.DetailFragmentBinding
import com.example.movieapp.model.network.data.SmallMovieList
import com.example.movieapp.utils.SHARED_KEY
import com.example.movieapp.utils.ioTaskAsync
import com.example.movieapp.utils.startJob
import com.google.firebase.firestore.ktx.firestore

import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject

class DetailFragment : Fragment() {
Expand All @@ -19,33 +33,59 @@ class DetailFragment : Fragment() {
lateinit var viewModelFactory: ViewModelFactory
lateinit var viewModel: DetailViewModel
lateinit var binding: DetailFragmentBinding
lateinit var sharedPreferences: SharedPreferences

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

App.appComponent.inject(this)

binding = DetailFragmentBinding.inflate(inflater)

val args = DetailFragmentArgs.fromBundle(requireArguments()).id

binding = DetailFragmentBinding.inflate(inflater)
viewModel = ViewModelProvider(this, viewModelFactory).get(DetailViewModel::class.java)
val args = DetailFragmentArgs.fromBundle(requireArguments()).id

sharedPreferences = activity?.getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE)!!
val id = sharedPreferences.getString(SHARED_KEY, null)
if (id != null) {
viewModel.getUserId(id)
viewModel.checkForSavedMovie(id)
}




viewModel.getSelectedMovieById(args)


binding.imageButton.setOnClickListener {
Toast.makeText(context, "Save Movie", Toast.LENGTH_SHORT).show()
if (id == null || id == "null"){
Toast.makeText(context, "Please Sing in your account", Toast.LENGTH_SHORT).show()
} else{
Toast.makeText(context, "Save", Toast.LENGTH_SHORT).show()
viewModel.putMovieInDatabase()
}
}

viewModel.test.observe(viewLifecycleOwner, Observer {
if(it){
binding.imageButton.visibility = View.GONE
} else {
binding.imageButton.visibility = View.VISIBLE
}

})


binding.viewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}


}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,95 @@
package com.example.movieapp.ui.home.detail

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.movieapp.model.network.MovieDetailSource
import com.example.movieapp.model.network.data.MovieInfo
import com.example.movieapp.model.network.data.SmallMovieList
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.launch
import javax.inject.Inject

class DetailViewModel @Inject constructor(private val movieDetailSource: MovieDetailSource) :
ViewModel() {

//LiveData object of movie
private val _selectProperty = MutableLiveData<MovieInfo>()
private var _selectProperty = MutableLiveData<MovieInfo>()
val selectProperty: LiveData<MovieInfo>
get() = _selectProperty

private var _test = MutableLiveData(false)
val test: LiveData<Boolean>
get() = _test

private var _userId = MutableLiveData<String>()
val userId: LiveData<String>
get() = _userId


fun getSelectedMovieById(id: Int) {
viewModelScope.launch {
_selectProperty.value = movieDetailSource.fetchDetailInformationOfMovie(id)
}
}
}


fun putMovieInDatabase(){

val database = Firebase.firestore
_selectProperty.value?.also {
val listOfMovie = SmallMovieList(
id = it.id,
posterPath = it.poster_path,
title = it.title,
voteAverage = it.vote_average.toFloat(),
backdropPath = it.backdrop_path)

viewModelScope.launch {
database.collection("users").document(userId.value!!).collection("movie")
.add(listOfMovie)
.addOnSuccessListener { documentReference ->
Log.d("TAG", "DocumentSnapshot added with ID: ${documentReference.id}")
_test.value = true
}.addOnFailureListener { e ->
Log.w("TAG", "Error adding document", e)
}
}
}
}

fun getUserId(id: String){
_userId.value = id
}


fun checkForSavedMovie(id: String){
val database = Firebase.firestore
viewModelScope.launch {
database.collection("users")
.document(id)
.collection("movie")
.get()
.addOnSuccessListener { result ->
var i = 0
for (document in result) {
if (selectProperty.value?.title == document.get("title")){
i++
_test.value = true
}else{
if (i == 0){
_test.value = false
}
}
}
}.addOnFailureListener { exception ->
Log.w("TAG", "Error getting documents.", exception)
}
}
}
}

Loading

0 comments on commit 942c3d7

Please sign in to comment.