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

Commit

Permalink
New: Unsplash login handling + like/unlike images (trello.com/c/HS3gc…
Browse files Browse the repository at this point in the history
…PVW)
  • Loading branch information
DawnImpulse committed Oct 3, 2018
1 parent 99d5a00 commit 4d24e1a
Show file tree
Hide file tree
Showing 26 changed files with 697 additions and 144 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ android {
}

dependencies {
def ktx_version = '1.0.0-alpha1'
def x_version = '1.0.0'

implementation fileTree(dir: 'libs', include: ['*.jar'])
//--------------- Support Libraries ---------------------
implementation "androidx.core:core-ktx:$ktx_version"
implementation "com.google.android.material:material:$x_version"
implementation "androidx.appcompat:appcompat:$x_version"
implementation "androidx.exifinterface:exifinterface:$x_version"
Expand Down Expand Up @@ -67,9 +65,10 @@ 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.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0' //kotlin co-routines

//------------------ Additional -------------------------
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0' //kotlin co-routines
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
android:theme="@style/AppTheme.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

Expand Down Expand Up @@ -133,14 +134,17 @@
android:theme="@style/AppTheme.Fullscreen"></activity>

<!-- Login -->
<activity android:name=".activities.LoginActivity">
<activity
android:name=".activities.LoginActivity"
android:theme="@style/AppTheme.Fullscreen">
<intent-filter>
<data
android:host="sourcei.org"
android:scheme="wallup" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import androidx.core.widget.toast
import com.dawnimpulse.wallup.R
import com.dawnimpulse.wallup.models.UnsplashModel
import com.dawnimpulse.wallup.pojo.BearerBody
import com.dawnimpulse.wallup.pojo.BearerToken
import com.dawnimpulse.wallup.pojo.UnsplashAuthError
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Config
import com.dawnimpulse.wallup.utils.F
import com.dawnimpulse.wallup.utils.L
import kotlinx.android.synthetic.main.activity_login.*
import com.pixplicity.easyprefs.library.Prefs

/**
* @author Saksham
Expand All @@ -43,29 +44,30 @@ class LoginActivity : AppCompatActivity() {
intent?.let {
it.data?.let { uri ->
val code = uri.getQueryParameter("code")
toast("making next call")
val model = UnsplashModel(lifecycle)
val body = BearerBody(Config.UNSPLASH_API_KEY.replace("Client-ID ",""), Config.UNSPLASH_SECRET, C.REDIRECT, code, "authorization_code")
//val body = BearerBody(Config.UNSPLASH_API_KEY.replace("Client-ID ",""), Config.UNSPLASH_SECRET, C.REDIRECT, "7cf79952614bca27b93f8e1812878a52f9347d2ac2f4898ffb54cf17ccdd7eea", "authorization_code")
val body = BearerBody(
Config.UNSPLASH_API_KEY.replace("Client-ID ", ""),
Config.UNSPLASH_SECRET,
C.REDIRECT,
code,
"authorization_code")

model.bearerToken(body) { e, r ->
L.dO(NAME,body)
e?.let {
L.d(NAME,e)
toast(e.toString())
L.dO(NAME, e)
toast((e as UnsplashAuthError).error)
finish()
}
r?.let {
L.dO(NAME, r)
it as BearerToken
toast("Logged in successfully")
Config.USER_API_KEY = "Bearer ${it.access_token}"
Prefs.putString(C.USER_TOKEN,Config.USER_API_KEY)
finish()
}

}
}
}
loginButton.setOnClickListener {
F.startWeb(this, "${C.UNSPLASH_OAUTH}" +
"?client_id=${Config.UNSPLASH_API_KEY.replace("Client-ID ", "")}" +
"&redirect_uri=${C.REDIRECT}" +
"&response_type=code" +
"&scope=public+read_user+write_user+read_photos+write_photos+write_likes+write_followers+read_collections+write_collections")
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/kotlin/com/dawnimpulse/wallup/adapters/MainAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -30,6 +31,7 @@ import com.dawnimpulse.wallup.activities.ImageActivity
import com.dawnimpulse.wallup.handlers.ImageHandler
import com.dawnimpulse.wallup.interfaces.OnLoadMoreListener
import com.dawnimpulse.wallup.pojo.ImagePojo
import com.dawnimpulse.wallup.sheets.ModalSheetUnsplash
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Config
import com.dawnimpulse.wallup.utils.F
Expand Down Expand Up @@ -127,12 +129,24 @@ class MainAdapter(private val lifecycle: Lifecycle, private val images: List<Ima
holder.image.background = ColorDrawable(Color.parseColor(images[position]!!.color!!))
holder.name.text = F.capWord(images[position]!!.user!!.name)

if (images[position]!!.liked_by_user)
holder.like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.vd_like))


holder.image.setOnClickListener {
var intent = Intent(context, ImageActivity::class.java)
intent.putExtra(C.TYPE, "")
intent.putExtra(C.IMAGE_POJO, Gson().toJson(images[position]))
(context as AppCompatActivity).startActivity(intent)
}
holder.likeL.setOnClickListener {
if (Config.USER_API_KEY.isNotEmpty())
F.like(context, holder.like, images[position]!!.id)
else {
var sheet = ModalSheetUnsplash()
sheet.show((context as AppCompatActivity).supportFragmentManager, sheet.tag)
}
}
holder.name.setOnClickListener(artistClick)
holder.circleImage.setOnClickListener(artistClick)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.dawnimpulse.wallup.repositories.UnsplashRepository
* 2018 10 01 - master - Saksham - generate bearer token
*/
class UnsplashModel() {
lateinit var lifecycle: Lifecycle
private lateinit var lifecycle: Lifecycle

// constructor
constructor(lifecycle: Lifecycle) : this() {
Expand Down Expand Up @@ -281,6 +281,16 @@ class UnsplashModel() {
}
}

// like a photo
fun likePhoto(id: String) {
UnsplashRepository.likePhoto(id)
}

// like a photo
fun unlikePhoto(id: String) {
UnsplashRepository.unlikePhoto(id)
}

// generate bearer token
fun bearerToken(body: BearerBody,callback: (Any?, Any?) -> Unit){
UnsplashRepository.bearerToken(body) { e, r ->
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/kotlin/com/dawnimpulse/wallup/pojo/ImagePojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ data class ImagePojo(
@SerializedName("timestamp") val timestamp: Int = 0,
@SerializedName("views") val views: Int = 0,
@SerializedName("downloads") val downloads: Int = 0,
@SerializedName("exif") val exif: Exif? = null
@SerializedName("exif") val exif: Exif? = null,
@SerializedName("liked_by_user") val liked_by_user: Boolean = false,
@SerializedName("current_user_collections") val current_user_collections: List<CollectionPojo>? = null
)

data class Urls(
Expand Down
Loading

0 comments on commit 4d24e1a

Please sign in to comment.