Skip to content

Commit

Permalink
Merge pull request #9 from ZarinPal/impl-suspanble
Browse files Browse the repository at this point in the history
Impl suspanble
  • Loading branch information
ImanX authored Mar 15, 2021
2 parents b9f6835 + 8c6ba89 commit f8ec889
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 33 deletions.
6 changes: 0 additions & 6 deletions auth/src/main/java/com/zarinpal/ZarinPalAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@ public static ContextBuilder with(Context context) {
}
}


public interface Callback {
void onIssueAccessToken(String typeToken, String accessToken, String refreshToken, long expireIn);

void onException(Throwable throwable);
}
}
7 changes: 7 additions & 0 deletions auth/src/main/java/com/zarinpal/auth/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zarinpal.auth;

public interface Callback {
void onIssueAccessToken(String typeToken, String accessToken, String refreshToken, long expireIn);

void onException(Throwable throwable);
}
4 changes: 2 additions & 2 deletions auth/src/main/java/com/zarinpal/auth/Holder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.graphics.drawable.Drawable
import com.zarinpal.ZarinPalAuth

object Holder {
internal var callback: ZarinPalAuth.Callback? = null
internal var callback: Callback? = null
internal var drawable: Drawable? = null
internal var authClient: String? = null

fun setCallback(callback: ZarinPalAuth.Callback) {
fun setCallback(callback: Callback) {
this.callback = callback
}

Expand Down
35 changes: 34 additions & 1 deletion auth/src/main/java/com/zarinpal/auth/tools/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.zarinpal.auth.tools

import android.content.Context
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Patterns
import android.widget.Toast
import androidx.core.os.bundleOf
import com.zarinpal.ZarinPalAuth
import com.zarinpal.auth.Callback
import com.zarinpal.auth.exception.HttpException
import com.zarinpal.builder.ZarinPalAuthPresentation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
import org.json.JSONArray
import org.json.JSONObject
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resumeWithException

internal fun runCountDownTimer(
milliInFuture: Long,
Expand Down Expand Up @@ -62,7 +69,8 @@ internal fun Throwable.toToast(
}
}

internal val HttpException.readableCode: String? get() = JSONObject(message)
internal val HttpException.readableCode: String?
get() = JSONObject(message)
.getJSONArray("errors")
.first()
.getString("readable_code")
Expand All @@ -73,3 +81,28 @@ internal fun <T> mutableSingleLiveEvent(block: SingleLiveEvent<T>.() -> Unit): S
return SingleLiveEvent<T>().apply { block(this) }
}

suspend fun ZarinPalAuthPresentation.start(): Bundle {
return suspendCancellableCoroutine {
start(object : Callback {
override fun onIssueAccessToken(
typeToken: String?,
accessToken: String?,
refreshToken: String?,
expireIn: Long
) {
bundleOf(
"typeToken" to typeToken,
"accessToken" to accessToken,
"refreshToken" to refreshToken,
"expireIn" to expireIn
).apply { it.resumeWith(Result.success(this)) }
}

override fun onException(throwable: Throwable?) {
it.resumeWithException(throwable ?: Exception())
}

})
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.NonNull;

import com.zarinpal.ZarinPalAuth;
import com.zarinpal.auth.Callback;
import com.zarinpal.auth.Holder;

public class ZarinPalAuthPresentation {
Expand All @@ -15,7 +16,7 @@ public ZarinPalAuthPresentation(RequestBuilder requestBuilder) {
this.builder = requestBuilder;
}

public void start(@NonNull ZarinPalAuth.Callback callback) {
public void start(@NonNull Callback callback) {
Holder.INSTANCE.setCallback(callback);
Holder.INSTANCE.setDrawable(builder.getIcon());
Holder.INSTANCE.setAuthClient(builder.getAuthType().toSerialiaze());
Expand Down
4 changes: 2 additions & 2 deletions auth/src/main/res/layout/parent_auth_bottosheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp" />
</LinearLayout>

Expand Down
45 changes: 25 additions & 20 deletions sample/src/main/java/com/zarinpal/auth/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.zarinpal.Request
import com.zarinpal.ZarinPalAuth
import com.zarinpal.auth.Callback
import com.zarinpal.auth.tools.start
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
Expand All @@ -23,11 +27,11 @@ class MainActivity : AppCompatActivity() {
.asBottomSheet()
.setMessage("Message")
.make()
.start(object : ZarinPalAuth.Callback {
.start(object : Callback {
override fun onIssueAccessToken(
typeToken: String?, accessToken: String?, refreshToken: String?, expireIn: Long
) {
Log.i("TAG", accessToken)
println(accessToken)
}

override fun onException(throwable: Throwable?) {
Expand All @@ -36,24 +40,25 @@ class MainActivity : AppCompatActivity() {
})


// ZarinPalAuth.Builder(this,)
// .setMessage("آلودگی هوا و خشکی پوست دو عامل اصلی بیماری های پوستی می باشند.")
// .build()
// .start(object : ZarinPalAuth.Callback {
// override fun onIssueAccessToken(
// typeToken: String?,
// accessToken: String?,
// refreshToken: String?,
// expireIn: Long
// ) {
// Log.i("TAG token", accessToken)
// }
//
// override fun onException(throwable: Throwable?) {
//
// }
//
// })
//Use by Coroutine
GlobalScope.launch {
runCatching {
ZarinPalAuth.with(this@MainActivity)
.byRequest(Request.asPasswordGrant(GRANT_TYPE, CLIENT_SECRET, CLIENT_ID, SCOPE))
.asBottomSheet()
.setMessage("Message")
.make()
.start()
}.getOrElse {
println(it.message)
return@launch
}.let {
println(it["accessToken"])
}

}


}

}
2 changes: 1 addition & 1 deletion version.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {

artifactVersion = "0.0.3-alpha1"
artifactVersion = "0.0.4-alpha1"

library = [
'appcompat' : '1.2.0',
Expand Down

0 comments on commit f8ec889

Please sign in to comment.