Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
New : Image like (trello.com/c/oaoLaUEU)
Browse files Browse the repository at this point in the history
  • Loading branch information
DawnImpulse committed Oct 8, 2018
1 parent b5c92f0 commit 533d4e4
Show file tree
Hide file tree
Showing 19 changed files with 504 additions and 265 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation 'com.dawnimpulse:permissions:0.1.1' //permission manager
implementation 'com.pixplicity.easyprefs:library:1.9.0' //preferences manager
implementation 'me.grantland:autofittextview:0.2.1' //text auto-fit
implementation 'org.greenrobot:eventbus:3.1.1' //event bus

//------------------ Additional -------------------------
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.dawnimpulse.wallup.R
import com.dawnimpulse.wallup.adapters.MainAdapter
Expand All @@ -13,8 +14,12 @@ import com.dawnimpulse.wallup.interfaces.OnLoadMoreListener
import com.dawnimpulse.wallup.models.UnsplashModel
import com.dawnimpulse.wallup.pojo.ImagePojo
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Event
import com.dawnimpulse.wallup.utils.L
import kotlinx.android.synthetic.main.activity_general_images.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
* @author Saksham
Expand All @@ -32,6 +37,7 @@ class GeneralImagesActivity : AppCompatActivity(), View.OnClickListener,
private val NAME = "GeneralImagesActivity"
private var current: Int = 0
private var nextPage = 2
private var randomImages = false //if random images are set
private lateinit var model: UnsplashModel
private lateinit var randomAdapter: RandomAdapter
private lateinit var mainAdapter: MainAdapter
Expand Down Expand Up @@ -71,6 +77,20 @@ class GeneralImagesActivity : AppCompatActivity(), View.OnClickListener,
generalImagesSwipe.setOnRefreshListener(this)
}

// on start
override fun onStart() {
if (!EventBus.getDefault().isRegistered(this))
EventBus.getDefault().register(this)
super.onStart()
}

// on destroy
override fun onDestroy() {
if (EventBus.getDefault().isRegistered(this))
EventBus.getDefault().unregister(this)
super.onDestroy()
}

// on click
override fun onClick(v: View) {
when (v.id) {
Expand Down Expand Up @@ -108,64 +128,59 @@ class GeneralImagesActivity : AppCompatActivity(), View.OnClickListener,
finish()
}

/**
* random images
*/
// on message event
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
fun onEvent(event: Event) {
if (event.obj.has(C.TYPE)) {
if (event.obj.getString(C.TYPE) == C.LIKE) {
// get id of the image
val id = event.obj.getString(C.ID)
// get position array for the image
var position = images.asSequence().withIndex().filter { it.value!!.id == id }.map { it.index }.toList()
// if position found
if (position.isNotEmpty()) {
// change like state in images array
for (pos in position) {
L.d(NAME, pos)
images[pos]!!.liked_by_user = event.obj.getBoolean(C.LIKE)
if (randomImages)
randomAdapter.notifyItemChanged(pos)
else
mainAdapter.notifyItemChanged(pos)
}
}
}
}
}

// random images
private fun randomImages() {
randomImages = true
generalImagesProgress.visibility = View.VISIBLE
generalImagesSwipe.visibility = View.GONE

when (current) {
0 -> {
model.randomImages { e, r ->
setRandomImages(e, r)
}
model.randomImages(callbackR)
}
1 -> {
toast("shuffling images")
model.randomUserImages(username) { e, r ->
setRandomImages(e, r)
}
model.randomUserImages(username, callbackR)
}
2 -> {
toast("shuffling images")
if (colType == C.FEATURED)
model.randomCollectionPhotos(colId) { e, r ->
setRandomImages(e, r)
}
else
model.randomImages(callback)
model.randomCollectionPhotos(colId, callbackR)
else // we cant get curated random images hence shuffling normal ones
model.randomImages(callbackR)
}
3 -> {
model.randomImagesTag(tag) { e, r ->
setRandomImages(e, r)
}
model.randomImagesTag(tag, callbackR)
}
}
}

/**
* set random images in adapter
*/
private fun setRandomImages(error: Any?, images: Any?) {
if (error != null) {
L.d(NAME, error.toString())
generalImagesSwipe.isRefreshing = false
generalImagesProgress.visibility = View.GONE
toast("Error while fetching random images")
} else {
generalImagesProgress.visibility = View.GONE
generalImagesSwipe.visibility = View.VISIBLE
randomAdapter = RandomAdapter(lifecycle, images as List<ImagePojo?>)
generalImagesSwipe.isRefreshing = false
generalImagesRecycler.layoutManager = LinearLayoutManager(this)
generalImagesRecycler.adapter = randomAdapter
}
}

/**
* paginated images
*/
// paginated images
private fun paginatedImages() {
when (current) {
1 -> {
Expand All @@ -180,43 +195,63 @@ class GeneralImagesActivity : AppCompatActivity(), View.OnClickListener,
}
}

/**
* callback for setting images in adapter
*/
// set random images in adapter
private var callbackR = object : (Any?, Any?) -> Unit {
override fun invoke(error: Any?, r: Any?) {
L.d(NAME, "called")
error?.let {
L.d(NAME, error)
generalImagesSwipe.isRefreshing = false
generalImagesProgress.visibility = View.GONE
toast("Error while fetching random images")
}
r?.let {
images = (r as List<ImagePojo?>).toMutableList()
generalImagesProgress.visibility = View.GONE
generalImagesSwipe.visibility = View.VISIBLE

randomAdapter = RandomAdapter(lifecycle, images)
generalImagesSwipe.isRefreshing = false
generalImagesRecycler.layoutManager = LinearLayoutManager(this@GeneralImagesActivity)
generalImagesRecycler.adapter = randomAdapter
(generalImagesRecycler.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
}
}
}

// callback for setting images in adapter
private var callback = object : (Any?, Any?) -> Unit {
override fun invoke(error: Any?, response: Any?) {
randomImages = false

error?.let {
L.d(NAME, error)
generalImagesProgress.visibility = View.GONE
generalImagesSwipe.isRefreshing = false
toast("error fetching images")
}
response?.let {
if ((response as List<ImagePojo>).size < 30) {
setRandomImages(error, response)
randomImages = true
callbackR(error, response)
} else {
if (error != null) {
L.d(NAME, error)
toast("Error in fetching images")
generalImagesProgress.visibility = View.GONE
generalImagesSwipe.isRefreshing = false
} else {
images = response.toMutableList()
mainAdapter = MainAdapter(lifecycle, images, generalImagesRecycler)
generalImagesRecycler.layoutManager = LinearLayoutManager(this@GeneralImagesActivity)
generalImagesRecycler.adapter = mainAdapter
generalImagesSwipe.visibility = View.VISIBLE
generalImagesSwipe.isRefreshing = false
generalImagesProgress.visibility = View.GONE

mainAdapter.setOnLoadMoreListener(this@GeneralImagesActivity)
}
images = response.toMutableList()
mainAdapter = MainAdapter(lifecycle, images, generalImagesRecycler)
generalImagesRecycler.layoutManager = LinearLayoutManager(this@GeneralImagesActivity)
generalImagesRecycler.adapter = mainAdapter

generalImagesSwipe.isRefreshing = false
generalImagesSwipe.visibility = View.VISIBLE
generalImagesProgress.visibility = View.GONE
(generalImagesRecycler.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false

mainAdapter.setOnLoadMoreListener(this@GeneralImagesActivity)
}
}
}
}

/**
* callback for setting images in adapter
*/
// callback for setting images in adapter
private var callbackPaginated = object : (Any?, Any?) -> Unit {
override fun invoke(error: Any?, response: Any?) {
error?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import com.dawnimpulse.wallup.handlers.*
import com.dawnimpulse.wallup.models.UnsplashModel
import com.dawnimpulse.wallup.pojo.ImagePojo
import com.dawnimpulse.wallup.sheets.ModalSheetExif
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Config
import com.dawnimpulse.wallup.utils.F
import com.dawnimpulse.wallup.utils.Toast
import com.dawnimpulse.wallup.sheets.ModalSheetUnsplash
import com.dawnimpulse.wallup.utils.*
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_image.*
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
* @author Saksham
Expand All @@ -58,19 +58,22 @@ class ImageActivity : AppCompatActivity(), View.OnClickListener {
private var bitmap: Bitmap? = null
private var fullDetails: ImagePojo? = null
private var color: Int = 0
private var position = -1
private var like = false //state of like button
private var likeStateChange = false //since we make multiple calls we set like once
private lateinit var details: ImagePojo
private lateinit var model: UnsplashModel
private lateinit var exifSheet: ModalSheetExif
private lateinit var loginSheet: ModalSheetUnsplash

/**
* On create
*/
// On create
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image)

model = UnsplashModel(lifecycle)
exifSheet = ModalSheetExif()
loginSheet = ModalSheetUnsplash()

// checking to handle the app links
if (intent.hasExtra(C.IMAGE_POJO)) {
Expand All @@ -84,13 +87,16 @@ class ImageActivity : AppCompatActivity(), View.OnClickListener {
getImageDetails(appLinkData.lastPathSegment)
}

position = intent.getIntExtra(C.POSITION, -1)

imagePreviewWallpaper.setOnClickListener(this)
imagePreviewDownload.setOnClickListener(this)
imagePreviewAuthorL.setOnClickListener(this)
imagePreviewExif.setOnClickListener(this)
imagePreviewShare.setOnClickListener(this)
imagePreviewStats.setOnClickListener(this)
imagePreviewUnsplash.setOnClickListener(this)
imagePreviewFab.setOnClickListener(this)

//Ripple.add(Colors(this).GREY_400, imagePreviewStats)

Expand Down Expand Up @@ -170,6 +176,28 @@ class ImageActivity : AppCompatActivity(), View.OnClickListener {
}
imagePreviewStats.id -> toast("Upcoming feature")
imagePreviewUnsplash.id -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(F.unsplashImage(details.id))))
imagePreviewFab.id -> {
if (Config.USER_API_KEY.isNotEmpty()) {
details?.let {
like = !like
F.like(this, imagePreviewFabI, it.id, like, true)
if (like) {
imagePreviewLikesCount.text = (it.likes + 1).toString()
details.likes = details.likes + 1
} else {
imagePreviewLikesCount.text = (it.likes - 1).toString()
details.likes = details.likes - 1
}

var obj = JSONObject()
obj.put(C.TYPE, C.LIKE)
obj.put(C.LIKE, like)
obj.put(C.ID, details.id)
EventBus.getDefault().postSticky(Event(obj))
}
} else
loginSheet.show(supportFragmentManager, loginSheet.tag)
}
}
}

Expand Down Expand Up @@ -217,12 +245,20 @@ class ImageActivity : AppCompatActivity(), View.OnClickListener {
} else
imagePreviewDownloadCount.visibility = View.VISIBLE

if (details.liked_by_user && !likeStateChange)
F.like(this, imagePreviewFabI, true, true)

if (!likeStateChange)
like = details.liked_by_user

likeStateChange = true
}

// set color on a resources
private fun color() {
var down = imagePreviewDownload.background.current as GradientDrawable
var wall = imagePreviewWallpaper.background.current as GradientDrawable
var fab = imagePreviewFab.background.current as GradientDrawable

imagePreviewAuthorImagesL.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
imagePreviewAuthorCollectionsL.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
Expand All @@ -231,8 +267,10 @@ class ImageActivity : AppCompatActivity(), View.OnClickListener {
imagePreviewExifI.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
imagePreviewStatsI.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
imagePreviewUnsplashI.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
//imagePreviewLikeI.drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)

down.setColor(color)
fab.setColor(color)
wall.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
//imagePreviewWallpaperT.setTextColor(color)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.dawnimpulse.wallup.pojo.UnsplashAuthError
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Config
import com.dawnimpulse.wallup.utils.L
import com.google.gson.Gson
import com.pixplicity.easyprefs.library.Prefs

/**
Expand Down Expand Up @@ -63,6 +64,15 @@ class LoginActivity : AppCompatActivity() {
toast("Logged in successfully")
Config.USER_API_KEY = "Bearer ${it.access_token}"
Prefs.putString(C.USER_TOKEN,Config.USER_API_KEY)
model.selfProfile() { e, r ->
e?.let {
L.d(NAME, e)
toast("error fetching profile")
}
r?.let {
Prefs.putString(C.USER, Gson().toJson(it))
}
}
finish()
}

Expand Down
Loading

0 comments on commit 533d4e4

Please sign in to comment.