Переглянути джерело

Added button for sharing user profile

Vadik Sirekanyan 7 роки тому
батько
коміт
e9ea265eb9

+ 18 - 0
app/src/main/java/me/vadik/knigopis/MainActivity.kt

@@ -12,6 +12,7 @@ import android.support.v7.app.AppCompatActivity
 import android.support.v7.widget.LinearLayoutManager
 import android.support.v7.widget.RecyclerView
 import android.support.v7.widget.Toolbar
+import android.util.Log
 import android.view.MenuItem
 import android.view.View
 import com.tbruyelle.rxpermissions2.RxPermissions
@@ -59,6 +60,7 @@ class MainActivity : AppCompatActivity(), Router {
     private var userLoggedIn = false
     private var booksChanged = false
     private lateinit var loginOption: MenuItem
+    private lateinit var shareOption: MenuItem
     private lateinit var currentTab: CurrentTab
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -158,6 +160,20 @@ class MainActivity : AppCompatActivity(), Router {
                     login()
                     true
                 }
+                R.id.option_share -> {
+                    auth.getUserProfile()?.let { profile ->
+                        val sharingIntent = Intent(Intent.ACTION_SEND)
+                            .setType("text/plain")
+                            .putExtra(Intent.EXTRA_TEXT, profile)
+                        startActivity(
+                            Intent.createChooser(
+                                sharingIntent,
+                                getString(R.string.option_share_title)
+                            )
+                        )
+                    } ?: Log.e(TAG, "Cannot share user profile: it's empty")
+                    true
+                }
                 R.id.option_about -> {
                     val dialogView = View.inflate(this, R.layout.about, null)
                     val versionView = dialogView.aboutAppVersion
@@ -191,6 +207,7 @@ class MainActivity : AppCompatActivity(), Router {
             }
         }
         loginOption = toolbar.menu.findItem(R.id.option_login)
+        shareOption = toolbar.menu.findItem(R.id.option_share)
     }
 
     private fun login() {
@@ -240,6 +257,7 @@ class MainActivity : AppCompatActivity(), Router {
 
     private fun refreshOptionsMenu() {
         loginOption.isVisible = true
+        shareOption.isVisible = auth.isAuthorized()
         if (auth.isAuthorized()) {
             loginOption.setTitle(R.string.option_logout)
             loginOption.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)

+ 10 - 1
app/src/main/java/me/vadik/knigopis/auth/KAuth.kt

@@ -11,11 +11,13 @@ import java.util.*
 private const val PREFS_NAME = "knigopis"
 private const val TOKEN_KEY = "token"
 private const val ACCESS_TOKEN_KEY = "access_token"
+private const val USER_PROFILE = "user_profile"
 
 interface KAuth {
     fun isAuthorized(): Boolean
     fun getAccessToken(): String
     fun getTokenRequest(): Intent
+    fun getUserProfile(): String?
     fun saveTokenResponse(data: Intent)
     fun requestAccessToken(onSuccess: () -> Unit)
     fun logout()
@@ -37,6 +39,10 @@ class KAuthImpl(
             .putExtra(UloginAuthActivity.FIELDS, arrayOf(TOKEN_KEY))
     }
 
+    override fun getUserProfile(): String? {
+        return preferences.getString(USER_PROFILE, null)
+    }
+
     override fun saveTokenResponse(data: Intent) {
         val userData = data.getSerializableExtra(UloginAuthActivity.USERDATA) as HashMap<*, *>
         preferences.edit().putString(TOKEN_KEY, userData[TOKEN_KEY].toString()).apply()
@@ -48,7 +54,10 @@ class KAuthImpl(
             api.getCredentials(token)
                 .io2main()
                 .subscribe({
-                    preferences.edit().putString(ACCESS_TOKEN_KEY, it.accessToken).apply()
+                    preferences.edit()
+                        .putString(ACCESS_TOKEN_KEY, it.accessToken)
+                        .putString(USER_PROFILE, it.user.profile)
+                        .apply()
                     onSuccess()
                 }, {
                     logError("cannot get credentials", it)

+ 1 - 1
app/src/main/java/me/vadik/knigopis/extensions.kt

@@ -25,7 +25,7 @@ import io.reactivex.Single
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.schedulers.Schedulers
 
-private const val TAG = "Knigopis"
+const val TAG = "Knigopis"
 private val HTTP_SCHEMES = setOf("http", "https")
 
 inline fun Context.startActivityOrElse(intent: Intent, onError: () -> Unit) {

+ 1 - 1
app/src/main/java/me/vadik/knigopis/model/Credentials.kt

@@ -5,7 +5,7 @@ import com.google.gson.annotations.SerializedName
 class Credentials(
     @SerializedName("access-token")
     val accessToken: String,
-    val userFull: UserFull
+    val user: UserFull
 ) {
 
     class UserFull(

+ 9 - 0
app/src/main/res/drawable/ic_share.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
+</vector>

+ 7 - 0
app/src/main/res/menu/options.xml

@@ -2,6 +2,13 @@
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
 
+    <item
+        android:id="@+id/option_share"
+        android:icon="@drawable/ic_share"
+        android:title="@string/option_share"
+        android:visible="false"
+        app:showAsAction="ifRoom" />
+
     <item
         android:id="@+id/option_login"
         android:visible="false" />

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -12,6 +12,8 @@
     <!-- menu -->
     <string name="option_login">Login</string>
     <string name="option_logout">Logout</string>
+    <string name="option_share">Share</string>
+    <string name="option_share_title">Share your profile</string>
     <string name="option_about">About</string>
 
     <!-- books -->