11package com.rozetka.yandexauth
22
33import android.app.Activity
4+ import android.content.ClipData
5+ import android.content.ClipboardManager
46import android.content.Context
57import android.content.Intent
68import android.net.Uri
@@ -9,6 +11,7 @@ import androidx.activity.ComponentActivity
911import androidx.activity.compose.setContent
1012import androidx.compose.foundation.Image
1113import androidx.compose.foundation.layout.*
14+ import androidx.compose.foundation.text.ClickableText
1215import androidx.compose.material3.AlertDialog
1316import androidx.compose.material3.Button
1417import androidx.compose.material3.MaterialTheme
@@ -18,7 +21,10 @@ import androidx.compose.ui.Alignment
1821import androidx.compose.ui.Modifier
1922import androidx.compose.ui.platform.LocalContext
2023import androidx.compose.ui.res.painterResource
24+ import androidx.compose.ui.text.SpanStyle
25+ import androidx.compose.ui.text.buildAnnotatedString
2126import androidx.compose.ui.text.style.TextAlign
27+ import androidx.compose.ui.text.withStyle
2228import androidx.compose.ui.unit.dp
2329import androidx.core.app.ActivityCompat
2430import androidx.core.view.WindowCompat
@@ -30,9 +36,11 @@ import com.yandex.authsdk.YandexAuthSdk
3036
3137const val REQUEST_LOGIN_SDK = 1337
3238const val BOT_DEEP_LINK = " https://t.me/music_yandex_bot?start="
39+ const val SOURCE_CODE_LINK = " https://github.com/MarshalX/yandex-music-token"
3340
3441class MainActivity : ComponentActivity () {
3542 private var showErrorDialog: MutableState <Boolean > = mutableStateOf(false )
43+ private val token: MutableState <String > = mutableStateOf(" " )
3644
3745 private lateinit var sdk: YandexAuthSdk
3846 private lateinit var _context : Context
@@ -42,7 +50,7 @@ class MainActivity : ComponentActivity() {
4250 try {
4351 val yandexAuthToken = sdk.extractToken(resultCode, data)
4452 yandexAuthToken?.let {
45- openUriInBrowser( " $BOT_DEEP_LINK${it .value} " , _context )
53+ token .value = it.value
4654 }
4755
4856 } catch (_: YandexAuthException ) {
@@ -59,6 +67,13 @@ class MainActivity : ComponentActivity() {
5967 context.startActivity(intent)
6068 }
6169
70+ private fun copyToken (context : Context ) {
71+ val clipboardManager =
72+ context.getSystemService(Context .CLIPBOARD_SERVICE ) as ClipboardManager
73+ val clip = ClipData .newPlainText(" token" , token.value)
74+ clipboardManager.setPrimaryClip(clip)
75+ }
76+
6277 @Composable
6378 private fun ShowAlertDialog (title : String , message : String , onDismiss : () -> Unit ) {
6479 var openDialog by remember { mutableStateOf(true ) }
@@ -102,7 +117,7 @@ class MainActivity : ComponentActivity() {
102117 if (showErrorDialog.value) {
103118 ShowAlertDialog (
104119 " Ошибка" ,
105- " Авторизация не удалась " ,
120+ " Не удалось авторизоваться " ,
106121 fun () { showErrorDialog.value = false })
107122 }
108123
@@ -124,24 +139,87 @@ class MainActivity : ComponentActivity() {
124139 textAlign = TextAlign .Center
125140 )
126141
127- Button (
128- onClick = {
129- ActivityCompat .startActivityForResult(
130- this @MainActivity as Activity ,
131- sdk.createLoginIntent(YandexAuthLoginOptions .Builder ().build()),
132- REQUEST_LOGIN_SDK ,
133- null
134- )
135- },
142+ if (token.value == " " ) {
143+ Button (
144+ onClick = {
145+ ActivityCompat .startActivityForResult(
146+ this @MainActivity as Activity ,
147+ sdk.createLoginIntent(
148+ YandexAuthLoginOptions .Builder ().build()
149+ ),
150+ REQUEST_LOGIN_SDK ,
151+ null
152+ )
153+ },
154+ Modifier
155+ .padding(vertical = 10 .dp)
156+ .align(Alignment .CenterHorizontally )
157+ .height(48 .dp)
158+ .width(200 .dp)
159+ ) {
160+ Text (text = " Авторизоваться" )
161+ }
162+ } else {
163+ Button (
164+ onClick = {
165+ openUriInBrowser(" $BOT_DEEP_LINK${token.value} " , _context )
166+ },
167+ Modifier
168+ .padding(vertical = 10 .dp)
169+ .align(Alignment .CenterHorizontally )
170+ .height(48 .dp)
171+ .width(200 .dp)
172+ ) {
173+ Text (text = " Перейти в бота" )
174+ }
175+ }
176+ }
177+ }
178+ Box (
179+ Modifier
180+ .fillMaxSize()
181+ .padding(horizontal = 5 .dp)
182+ .padding(vertical = 5 .dp)) {
183+ if (token.value != " " ) {
184+ Column (
136185 Modifier
137- .padding(vertical = 10 .dp)
138- .align(Alignment .CenterHorizontally )
139- .height(48 .dp)
140- .width(200 .dp)
186+ .align(Alignment .BottomStart )
187+ .fillMaxWidth()
188+ .navigationBarsPadding()
141189 ) {
142- Text (text = " Войти" )
190+ val copyTokenString = buildAnnotatedString {
191+ withStyle(style = SpanStyle (color = MaterialTheme .colorScheme.primary)) {
192+ append(" Скопировать токен" )
193+ }
194+ }
195+
196+ ClickableText (
197+ text = copyTokenString,
198+ onClick = { copyToken(context = _context ) },
199+ )
200+
143201 }
144202 }
203+ Column (
204+ Modifier
205+ .align(Alignment .BottomEnd )
206+ .fillMaxWidth()
207+ .navigationBarsPadding()
208+ ) {
209+ val sourceCodeString = buildAnnotatedString {
210+ append(" Исходный код: " )
211+
212+ withStyle(style = SpanStyle (color = MaterialTheme .colorScheme.primary)) {
213+ append(" yandex-music-token" )
214+ }
215+ }
216+
217+ ClickableText (
218+ text = sourceCodeString,
219+ onClick = { openUriInBrowser(SOURCE_CODE_LINK , _context ) },
220+ modifier = Modifier .align(Alignment .End )
221+ )
222+ }
145223 }
146224 }
147225 }
0 commit comments