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

Commit

Permalink
New : User profile + logout (trello.com/c/9eFy2Z4p)
Browse files Browse the repository at this point in the history
  • Loading branch information
DawnImpulse committed Oct 4, 2018
1 parent 4d24e1a commit b5c92f0
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0' //kotlin co-routines
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
}

apply plugin: 'com.google.gms.google-services'
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<!-- About -->
<activity
android:name=".activities.AboutActivity"
android:theme="@style/AppTheme.Fullscreen"></activity>
android:theme="@style/AppTheme.Fullscreen" />

<!-- Login -->
<activity
Expand All @@ -141,12 +141,18 @@
<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" />
</intent-filter>
</activity>

<!--User Profile-->
<activity
android:name=".activities.UserActivity"
android:theme="@style/AppTheme.Fullscreen" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
ISC License
Copyright 2018, Saksham (DawnImpulse)
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
OR PERFORMANCE OF THIS SOFTWARE.*/
package com.dawnimpulse.wallup.activities

import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.toast
import com.dawnimpulse.wallup.R
import com.dawnimpulse.wallup.handlers.ImageHandler
import com.dawnimpulse.wallup.models.UnsplashModel
import com.dawnimpulse.wallup.pojo.UserPojo
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.Config
import com.dawnimpulse.wallup.utils.Dialog
import com.dawnimpulse.wallup.utils.L
import com.google.gson.Gson
import com.pixplicity.easyprefs.library.Prefs
import kotlinx.android.synthetic.main.activity_user.*

/**
* @author Saksham
*
* @note Last Branch Update - master
* @note Created on 2018-10-04 by Saksham
*
* @note Updates :
*/
class UserActivity : AppCompatActivity() {
private val NAME = "UserActivity"
private lateinit var model: UnsplashModel

// on create
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_user)

// fetching details from prefs
if (Prefs.contains(C.USER))
setDetails(Gson().fromJson(Prefs.getString(C.USER, ""), UserPojo::class.java))

// fetching details from unsplash
model = UnsplashModel(lifecycle)
model.selfProfile() { e, r ->
e?.let {
L.d(NAME, e)
toast("error fetching profile")
}
r?.let {
Prefs.putString(C.USER, Gson().toJson(it))
setDetails(it as UserPojo)
}

}

// logout user
logoutL.setOnClickListener {
Dialog.simpleOk(this,
"User Profile Logout",
"Wish to logout from your profile ?",
DialogInterface.OnClickListener { dialog, _ ->
Prefs.remove(C.USER_TOKEN)
Prefs.remove(C.USER)
Config.USER_API_KEY = ""
dialog.dismiss()
toast("Successfully logout from your profile")
finish()
})
}
}

//set user details
private fun setDetails(user: UserPojo) {
userFullName.text = user.name
userName.text = "@${user.username}"
ImageHandler.setImageInView(lifecycle, userImage, user.profile_image.large)
ImageHandler.setImageInView(lifecycle, userBg, "${C.UNSPLASH_SOURCE}/user/${user.username}/720x1280")
}
}
17 changes: 17 additions & 0 deletions app/src/main/kotlin/com/dawnimpulse/wallup/models/UnsplashModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.dawnimpulse.wallup.repositories.UnsplashRepository
* 2018 09 08 - master - Saksham - featured curatedCollections
* 2018 09 22 - master - Saksham - random images tag
* 2018 10 01 - master - Saksham - generate bearer token
* 2018 10 04 - master - Saksham - self profile
*/
class UnsplashModel() {
private lateinit var lifecycle: Lifecycle
Expand Down Expand Up @@ -306,4 +307,20 @@ class UnsplashModel() {
})
}
}

// self profile
fun selfProfile(callback: (Any?, Any?) -> Unit){
UnsplashRepository.selfProfile() { e, r ->
lifecycle.addObserver(object : LifecycleObserver {
var once = true
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
if (once) {
callback(e, r)
once = false
}
}
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import retrofit2.Response
* 2018 09 14 - master - Saksham - user's collections
* 2018 09 22 - master - Saksham - random images tag
* 2018 10 01 - master - Saksham - generate bearer token
* 2018 10 04 - master - Saksham - user profile
*/
object UnsplashRepository {
private val NAME = "UnsplashRepository"
Expand Down Expand Up @@ -481,5 +482,26 @@ object UnsplashRepository {
})
}

// user profile
fun selfProfile(callback: (Any?, Any?) -> Unit){
val apiClient = RetroApiClient.getClient()!!.create(RetroUnsplashSource::class.java)
val call = apiClient.selfProfile(
Config.apiKey()
)

call.enqueue(object : Callback<UserPojo> {

override fun onResponse(call: Call<UserPojo>?, response: Response<UserPojo>) {
if (response.isSuccessful)
callback(null, response.body())
else
callback(ErrorUtils.parseError(response), null)
}

override fun onFailure(call: Call<UserPojo>?, t: Throwable?) {
t?.toString()?.let { L.d(NAME, it) }
}
})
}
}

21 changes: 17 additions & 4 deletions app/src/main/kotlin/com/dawnimpulse/wallup/sheets/ModalSheetNav.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.dawnimpulse.wallup.BuildConfig
import com.dawnimpulse.wallup.R
import com.dawnimpulse.wallup.activities.AboutActivity
import com.dawnimpulse.wallup.activities.CollectionLayoutActivity
import com.dawnimpulse.wallup.activities.GeneralImagesActivity
import com.dawnimpulse.wallup.activities.UserActivity
import com.dawnimpulse.wallup.utils.C
import com.dawnimpulse.wallup.utils.F
import com.dawnimpulse.wallup.utils.RemoteConfig
import com.pixplicity.easyprefs.library.Prefs
import kotlinx.android.synthetic.main.bottom_sheet_navigation.*


Expand All @@ -37,8 +40,10 @@ import kotlinx.android.synthetic.main.bottom_sheet_navigation.*
*
* @note Updates :
* Saksham - 2018 09 15 - master - update handling
* Saksham - 2018 10 04 - master - user
*/
class ModalSheetNav : RoundedBottomSheetDialogFragment(), View.OnClickListener {
private lateinit var sheet: ModalSheetUnsplash

// on create
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand All @@ -49,10 +54,13 @@ class ModalSheetNav : RoundedBottomSheetDialogFragment(), View.OnClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

sheet = ModalSheetUnsplash()

sheetNavRandom.setOnClickListener(this)
sheetNavFeedback.setOnClickListener(this)
sheetNavCollection.setOnClickListener(this)
sheetNavAbout.setOnClickListener(this)
sheetNavUser.setOnClickListener(this)

RemoteConfig.getProductionUpdateValues()?.let {
if (it.next_version_code > BuildConfig.VERSION_CODE) {
Expand All @@ -74,19 +82,24 @@ class ModalSheetNav : RoundedBottomSheetDialogFragment(), View.OnClickListener {
startActivity(intent)
dismiss()
}
sheetNavFeedback.id -> F.sendMail(activity!!)
sheetNavCollection.id -> {
startActivity(Intent(activity, CollectionLayoutActivity::class.java))
dismiss()
}

sheetNavAbout.id -> {
startActivity(Intent(activity, AboutActivity::class.java))
dismiss()
}
sheetNavFeedback.id -> F.sendMail(activity!!)
sheetNavUpdateL.id -> F.startWeb(context!!, C.WALLUP_PLAY)
sheetNavUser.id -> {
if (!Prefs.contains(C.USER_TOKEN))
sheet.show((context as AppCompatActivity).supportFragmentManager, sheet.tag)
else
startActivity(Intent(context, UserActivity::class.java))

sheetNavUpdateL.id ->
F.startWeb(context!!, C.WALLUP_PLAY)
dismiss()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ interface RetroUnsplashSource {
@POST("/photos/{id}/like")
fun likeImage(
@Header(C.AUTHORIZATION) authorization: String,
@Path(C.ID) id:String
@Path(C.ID) id: String
): Call<ImagePojo>

// ------------------------------
Expand All @@ -121,10 +121,9 @@ interface RetroUnsplashSource {
@DELETE("/photos/{id}/like")
fun unlikeImage(
@Header(C.AUTHORIZATION) authorization: String,
@Path(C.ID) id:String
@Path(C.ID) id: String
): Call<ImagePojo>


//________________________________
// USER
//________________________________
Expand Down Expand Up @@ -161,6 +160,11 @@ interface RetroUnsplashSource {
@Query(C.USERNAME) username: String
): Call<List<ImagePojo>>

@GET("/me")
fun selfProfile(
@Header(C.AUTHORIZATION) authorization: String
): Call<UserPojo>


//________________________________
// Collection
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/com/dawnimpulse/wallup/utils/C.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ object C {
const val FEATURE = "feature"
const val BUG = "bug"
const val USER_TOKEN = "userToken"
const val USER = "user"



const val UTM = "?utm_source=wallup&utm_medium=referral"
const val UNSPLASH = "https://unsplash.com$UTM"
Expand All @@ -76,6 +79,7 @@ object C {
const val REDIRECT = "wallup://sourcei.org"
const val UNSPLASH_OAUTH = "https://unsplash.com/oauth/authorize"
const val UNSPLASH_TOKEN = "https://unsplash.com/oauth/token"
const val UNSPLASH_SOURCE = "https://source.unsplash.com"

const val ERROR_CODE_1 = 601
const val ERROR_CODE_2 = 602
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/kotlin/com/dawnimpulse/wallup/utils/Dialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
ISC License
Copyright 2018, Saksham (DawnImpulse)
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
OR PERFORMANCE OF THIS SOFTWARE.*/package com.dawnimpulse.wallup.utils

import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog


/**
* @author Saksham
*
* @note Last Branch Update -
* @note Created on 2018-10-04 by Saksham
*
* @note Updates :
*/
object Dialog {
private lateinit var alertDialog: AlertDialog

// simple ok dialog
fun simpleOk(context: Context, title: String,message: String, positive: DialogInterface.OnClickListener) {
var builder = AlertDialog.Builder(context)
builder
.setTitle(title)
.setMessage(message)
.setPositiveButton("OK", positive)
.setNegativeButton("CANCEL") { dialog, _ ->
dialog.dismiss()
}
.setCancelable(false)

alertDialog = builder.create()
alertDialog.show()

}

// dismiss
fun dismiss(){
alertDialog.dismiss()
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/vd_account.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:viewportWidth="24"
android:viewportHeight="24" android:width="24dp" android:height="24dp">
<path android:fillColor="@color/blue"
android:pathData="M12 4A4 4 0 0 1 16 8A4 4 0 0 1 12 12A4 4 0 0 1 8 8A4 4 0 0 1 12 4M12 14C16.42 14 20 15.79 20 18V20H4V18C4 15.79 7.58 14 12 14Z" />
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/vd_logout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:viewportWidth="24"
android:viewportHeight="24" android:width="24dp" android:height="24dp">
<path android:fillColor="@color/black"
android:pathData="M17 17.25V14H10V10H17V6.75L22.25 12L17 17.25M13 2A2 2 0 0 1 15 4V8H13V4H4V20H13V16H15V20A2 2 0 0 1 13 22H4A2 2 0 0 1 2 20V4A2 2 0 0 1 4 2H13Z" />
</vector>
Loading

0 comments on commit b5c92f0

Please sign in to comment.