Skip to content

Commit

Permalink
updating the number of like when user liked or dislike a photo
Browse files Browse the repository at this point in the history
  • Loading branch information
FdevTech committed Mar 31, 2021
1 parent 7cc98d6 commit 621d718
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class SinglePhotoRepository (private val unsplashDatabase: UnsplashDatabase)
Log.d(TAG, "getAllPhotosFromCollection: ${it.size}")
it.asDomainModelFromCollectionPhoto()
})
val userPhotos=Transformations.map(unsplashDatabase.photosDao.getAllUserPhotos(),
{
it.asUserPhotoDomainModel()
})

val userLikedPhoto=Transformations.map(unsplashDatabase.photosDao.getAllUserLikedPhotos()){
val userPhotos=Transformations.map(unsplashDatabase.photosDao.getAllUserPhotos()){
Log.d(TAG, "userPhotos it size: ${it.size} ")
it.asUserPhotoDomainModel()
}
val userLikedPhotos=Transformations.map(unsplashDatabase.photosDao.getAllUserLikedPhotos()){
Log.d(TAG, "userLikedPhotos it size: ${it.size} ")
it.asUserLikedPhotoDomainModel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PhotoViewModel(val application: Application, val credential:String,from:St
}
private fun refrechFromRepository(from:String?)
{
Log.d(TAG, "refrechFromRepository: ")
Log.d(TAG, "refrechFromRepository: $from ")
if(from==null)
{
_photosRetrived=singlePhotoRepository.photos as MutableLiveData<List<PhotoDomain>>
Expand All @@ -51,12 +51,12 @@ class PhotoViewModel(val application: Application, val credential:String,from:St
else if(from=="userPhotos")
{
_photosRetrived=singlePhotoRepository.userPhotos as MutableLiveData<List<PhotoDomain>>
Log.d(TAG, "photosRetrived from: $from size:${photosRetrived.value?.size} ")
Log.d(TAG, "photosRetrived from: $from size:${photosRetrived.value?.size} ")
}
else if(from=="userLikedPhoto")
{
_photosRetrived=singlePhotoRepository.userLikedPhoto as MutableLiveData<List<PhotoDomain>>
Log.d(TAG, "photosRetrived from: $from size:${photosRetrived.value?.size} ")
_photosRetrived=singlePhotoRepository.userLikedPhotos as MutableLiveData<List<PhotoDomain>>
Log.d(TAG, "photosRetrived from: $from==> ${singlePhotoRepository.userLikedPhotos.value?.size} size:${_photosRetrived.value?.size} ")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ interface PhotosDao
@Query("update photos_table set liked_by_user = :liked_by_user where phot_id=:photo_id ")
suspend fun insertLikedByUser(photo_id:String,liked_by_user:Boolean)

@Query("update photos_table set likes = :numberOfLikes where phot_id=:photo_id ")
suspend fun addOrRemoveLiked(photo_id:String,numberOfLikes:Int)

//this method is just used to test


@Query("select liked_by_user from photos_table where phot_id=:photo_id")
suspend fun chekLike(photo_id:String):Boolean

@Query("select likes from photos_table where phot_id=:photo_id")
suspend fun cheknumberOflikes(photo_id:String):Int

@Query("select * from photos_table")
fun getAllPhotos():LiveData<List<PhotoEntity>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,25 @@ class PhotoRepository(private val unsplashDatabase: UnsplashDatabase,private val
suspend fun likeDesLikePhoto(photoid:String)
{
val photolikedornot=unsplashDatabase.photosDao.chekLike(photoid)
var numberOflikes=unsplashDatabase.photosDao.cheknumberOflikes(photoid)
Log.d(TAG, "likeDesLikePhoto: photolikedornot=>${photolikedornot}")
try{
val responseRequest=if(photolikedornot)
{
numberOflikes-=1
Photos.PhotosAPI.photos.unLikePhoto(autorization = credentials,photoid = photoid)

}else
{
numberOflikes+=1
Photos.PhotosAPI.photos.likePhoto(autorization = credentials,photoid = photoid)
}
Log.d(TAG, "likeDesLikePhoto: responseRequest is succefull=${responseRequest.isSuccessful} reponse code =<>${responseRequest.code()} ")
if(responseRequest.isSuccessful)
{

unsplashDatabase.photosDao.insertLikedByUser(photoid,!photolikedornot)
unsplashDatabase.photosDao.addOrRemoveLiked(photoid,numberOflikes)
}
}catch (e:Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ProfileViewModel (val application:Application,val credential:String,val us
val photgrapherProfile=profileRepository.photographer
init {
Log.d(TAG, "${user}: ")
if(user!=null )
if(user!="me" )
{
refrechPhotographerProfileFromProfileRepository(user)
refrechPhotographerProfileFromProfileRepository(user!!)
}
else
{
Expand Down

0 comments on commit 621d718

Please sign in to comment.