Skip to content

Commit

Permalink
Added showing and hiding button of save.
Browse files Browse the repository at this point in the history
Small fix.
  • Loading branch information
Slavik Urdzik committed May 27, 2020
1 parent d52a6e7 commit 9af3e19
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ class DetailFragment : Fragment() {


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 = ViewModelProvider(this, viewModelFactory).get(DetailViewModel::class.java)

viewModel.getSelectedMovieById(args)

Expand All @@ -61,10 +65,20 @@ class DetailFragment : Fragment() {
if (id == null || id == "null"){
Toast.makeText(context, "Please Sing in your account", Toast.LENGTH_SHORT).show()
} else{
viewModel.putMovieInDatabase(id)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ class DetailViewModel @Inject constructor(private val movieDetailSource: MovieDe
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(id: String){

fun putMovieInDatabase(){

val database = Firebase.firestore
_selectProperty.value?.also {
val listOfMovie = SmallMovieList(
Expand All @@ -39,15 +50,46 @@ class DetailViewModel @Inject constructor(private val movieDetailSource: MovieDe
backdropPath = it.backdrop_path)

viewModelScope.launch {
database.collection("users").document(id).collection("movie")
database.collection("users").document(userId.value!!).collection("movie")
.add(listOfMovie)
.addOnSuccessListener { documentReference ->
Log.d("TAG", "DocumentSnapshot added with ID: ${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)
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ProfileFragment : Fragment() {
private lateinit var editor: SharedPreferences.Editor
private lateinit var saveInUserAdapter: SaveInUserAdapter

private var errorSnackbar: Snackbar? = null


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -59,8 +61,6 @@ class ProfileFragment : Fragment() {
sharedPreferences = activity?.getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE)!!




// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
Expand All @@ -78,11 +78,7 @@ class ProfileFragment : Fragment() {
//Navigate to Detail Activity
viewModel.navigateToSelectSaveProperty.observe(viewLifecycleOwner, Observer {
it?.let {
findNavController().navigate(
ProfileFragmentDirections.actionProfileFragmentToDetailFragment2(
it.id
)
)
findNavController().navigate(ProfileFragmentDirections.actionProfileFragmentToDetailFragment2(it.id))
viewModel.displayPropertyDetailsCompleted()
}
})
Expand All @@ -98,6 +94,11 @@ class ProfileFragment : Fragment() {

})

//Looking for the internet connection
viewModel.eventNetworkError.observe(viewLifecycleOwner, Observer {
if (it) onNetworkError()
})


binding.viewModel = viewModel
binding.lifecycleOwner = viewLifecycleOwner
Expand All @@ -115,8 +116,7 @@ class ProfileFragment : Fragment() {
} else {
val currentUser = auth.currentUser
updateUI(currentUser)
// editor?.putString(SHARED_KEY, currentUser?.uid)
// editor.apply()

}
})

Expand Down Expand Up @@ -164,6 +164,15 @@ class ProfileFragment : Fragment() {
}
}

//Function will show a toast when there is no internet
private fun onNetworkError() {
if (!viewModel.isNetworkErrorShown.value!!) {
errorSnackbar = Snackbar.make(binding.root, "Ошибка сети", Snackbar.LENGTH_INDEFINITE)
errorSnackbar?.setAction(R.string.retry, viewModel.errorClickListener)
errorSnackbar?.show()
}
}

private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
Expand Down Expand Up @@ -210,11 +219,13 @@ class ProfileFragment : Fragment() {
viewModel.fetchMovieOfSave()
} else {

binding.googleLoginBtn.visibility = View.VISIBLE
binding.userFragment.visibility = View.GONE
binding.saveText.visibility = View.GONE
binding.recyclerSave.visibility = View.GONE
Log.e("TAG", "tag")
binding.apply {
googleLoginBtn.visibility = View.VISIBLE
userFragment.visibility = View.GONE
saveText.visibility = View.GONE
recyclerSave.visibility = View.GONE
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.movieapp.ui.user.profile

import android.util.Log
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -37,8 +38,20 @@ class ProfileViewModel @Inject constructor() : ViewModel() {
val googleSignInClient: LiveData<GoogleSignInClient>
get() = _googleSignInClient

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

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

var test = MutableLiveData(false)

val errorClickListener = View.OnClickListener { fetchMovieOfSave() }


fun getUser(user: FirebaseUser?) {
_currentUser.value = user!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,17 @@ class SaveInUserAdapter(private val onClickListener: ClickListener) : ListAdapte

override fun onBindViewHolder(holder: SaveInUserHolder, position: Int) {
val item = getItem(position)

holder.itemView.setOnClickListener {
onClickListener.onClick(item)
}

holder.bind(item)
}

inner class SaveInUserHolder(private val binding: ItemBinding) :
RecyclerView.ViewHolder(binding.root) {
inner class SaveInUserHolder(private val binding: ItemBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(movie: SmallMovieList) {

// Glide.with(binding.root.context)
// .load("$IMAGE_BASE_PATH${movie.backdropPath}")
// .apply(
// RequestOptions()
// .placeholder(R.drawable.loading_animation)
// .error(R.drawable.ic_broken_image)
// )
// .into(binding.posterImage)


binding.movie = movie

}
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/res/layout/item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/cardView"
app:layout_constraintBottom_toTopOf="@id/rating"
android:layout_marginVertical="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:lines="2"
android:maxLines="2"
android:ellipsize="end"
android:text="@{movie.title}" />
android:text="@{movie.title}"
app:layout_constraintBottom_toTopOf="@id/rating"
app:layout_constraintEnd_toEndOf="@+id/cardView"
app:layout_constraintStart_toStartOf="@+id/cardView"
app:layout_constraintTop_toBottomOf="@id/cardView" />

<RatingBar
android:id="@+id/rating"
Expand All @@ -65,8 +64,11 @@
android:layout_height="15dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:hapticFeedbackEnabled="false"
android:numStars="10"
android:progressTint="@color/appColor"
android:rating="@{movie.voteAverage}"
android:secondaryProgressTint="@color/appColor"
android:stepSize="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/res/layout/profile_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,27 @@
android:clipToPadding="false"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/save_text"
app:layout_constraintTop_toBottomOf="@+id/save_text"
app:layout_constraintVertical_bias="0.0"
tools:listitem="@layout/item" />

<ProgressBar
style="@style/progressBarStyle"
android:foregroundGravity="center"
android:layout_width="64dp"
android:layout_height="64dp"
android:indeterminateTint="@color/appColor"
android:indeterminateTintMode="src_in"
app:isNetworkError="@{safeUnbox(viewModel.eventNetworkError)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:playlist="@{viewModel.movieOfSave}" />


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 9af3e19

Please sign in to comment.