Skip to content

Commit 3f65654

Browse files
author
alllexey
committed
Add new login activity
1 parent 94c8c8e commit 3f65654

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
android:roundIcon="@mipmap/ic_launcher_round"
1515
android:supportsRtl="true"
1616
android:theme="@style/AppTheme">
17+
<activity
18+
android:name=".ui.login.LoginActivity"
19+
android:exported="false" />
1720
<activity
1821
android:name=".ui.qr.QrCodeActivity"
1922
android:exported="false" />
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package me.alllexey123.itmowidgets.ui.login
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.lifecycle.lifecycleScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.launch
8+
import kotlinx.coroutines.withContext
9+
import me.alllexey123.itmowidgets.ItmoWidgetsApp
10+
import me.alllexey123.itmowidgets.databinding.ActivityLoginBinding
11+
import me.alllexey123.itmowidgets.ui.widgets.WidgetUtils
12+
13+
class LoginActivity : AppCompatActivity() {
14+
15+
private lateinit var binding: ActivityLoginBinding
16+
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
20+
binding = ActivityLoginBinding.inflate(layoutInflater)
21+
22+
setContentView(binding.root)
23+
24+
binding.buttonLogin.setOnClickListener {
25+
performLogin()
26+
}
27+
28+
}
29+
30+
private fun performLogin() {
31+
binding.loginInputLayout.error = null
32+
binding.passwordInputLayout.error = null
33+
binding.textViewResult.text = null
34+
35+
val login = binding.editTextLogin.text.toString().trim()
36+
val password = binding.editTextPassword.text.toString()
37+
38+
var isValid = true
39+
40+
if (login.isEmpty()) {
41+
binding.loginInputLayout.error = "Логин не может быть пустым"
42+
isValid = false
43+
}
44+
45+
if (password.isEmpty()) {
46+
binding.passwordInputLayout.error = "Пароль не может быть пустым"
47+
isValid = false
48+
}
49+
50+
if (!isValid) {
51+
return
52+
}
53+
54+
lifecycleScope.launch {
55+
try {
56+
binding.buttonLogin.isEnabled = false
57+
binding.textViewResult.text = "Выполняется вход..."
58+
59+
val appContainer = (applicationContext as ItmoWidgetsApp).appContainer
60+
val authResult = withContext(Dispatchers.IO) {
61+
appContainer.myItmo.auth(login, password)
62+
"Вход выполнен. Теперь можно использовать виджеты."
63+
}
64+
65+
appContainer.scheduleRepository.clearCache()
66+
appContainer.qrCodeRepository.clearCache()
67+
WidgetUtils.updateAllWidgets(applicationContext)
68+
binding.textViewResult.text = authResult
69+
70+
} catch (e: Exception) {
71+
if (e.message?.contains("Could not get authorize", ignoreCase = true) ?: false) {
72+
binding.textViewResult.text = "Ошибка: проверьте введённые данные"
73+
} else {
74+
binding.textViewResult.text = "Ошибка: ${e.message}"
75+
}
76+
77+
} finally {
78+
binding.buttonLogin.isEnabled = true
79+
}
80+
}
81+
}
82+
}

app/src/main/java/me/alllexey123/itmowidgets/ui/settings/SettingsFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import me.alllexey123.itmowidgets.data.LESSON_WIDGET_STYLE_CHANGED_KEY
2525
import me.alllexey123.itmowidgets.data.REFRESH_TOKEN_EXPIRES_KEY
2626
import me.alllexey123.itmowidgets.data.REFRESH_TOKEN_KEY
2727
import me.alllexey123.itmowidgets.data.SMART_SCHEDULING_KEY
28+
import me.alllexey123.itmowidgets.ui.login.LoginActivity
2829
import me.alllexey123.itmowidgets.ui.widgets.WidgetUtils
2930
import java.lang.Thread.sleep
3031
import java.text.SimpleDateFormat
@@ -104,6 +105,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
104105
updateAllWidgets()
105106
true
106107
}
108+
109+
val loginItmoId = findPreference<Preference>("login_itmoid")
110+
loginItmoId?.setOnPreferenceClickListener { preference ->
111+
val intent = Intent(context, LoginActivity::class.java)
112+
startActivity(intent)
113+
true
114+
}
107115
}
108116

109117
private fun updateAllWidgets() {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/main"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".ui.login.LoginActivity">
8+
9+
<com.google.android.material.textview.MaterialTextView
10+
android:id="@+id/text_view_title"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_marginTop="64dp"
14+
android:text="Вход через ITMO.ID"
15+
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<com.google.android.material.textfield.TextInputLayout
21+
android:id="@+id/login_input_layout"
22+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
23+
android:layout_width="0dp"
24+
android:layout_height="wrap_content"
25+
android:layout_marginStart="32dp"
26+
android:layout_marginTop="32dp"
27+
android:layout_marginEnd="32dp"
28+
android:hint="Логин"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintStart_toStartOf="parent"
31+
app:layout_constraintTop_toBottomOf="@+id/text_view_title">
32+
33+
<com.google.android.material.textfield.TextInputEditText
34+
android:id="@+id/edit_text_login"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:inputType="text"
38+
android:maxLines="1" />
39+
40+
</com.google.android.material.textfield.TextInputLayout>
41+
42+
<com.google.android.material.textfield.TextInputLayout
43+
android:id="@+id/password_input_layout"
44+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
45+
android:layout_width="0dp"
46+
android:layout_height="wrap_content"
47+
android:layout_marginStart="32dp"
48+
android:layout_marginTop="16dp"
49+
android:layout_marginEnd="32dp"
50+
android:hint="Пароль"
51+
app:endIconMode="password_toggle"
52+
app:layout_constraintEnd_toEndOf="parent"
53+
app:layout_constraintStart_toStartOf="parent"
54+
app:layout_constraintTop_toBottomOf="@+id/login_input_layout">
55+
56+
<com.google.android.material.textfield.TextInputEditText
57+
android:id="@+id/edit_text_password"
58+
android:layout_width="match_parent"
59+
android:layout_height="wrap_content"
60+
android:inputType="textPassword"
61+
android:maxLines="1" />
62+
63+
</com.google.android.material.textfield.TextInputLayout>
64+
65+
<com.google.android.material.button.MaterialButton
66+
android:id="@+id/button_login"
67+
android:layout_width="0dp"
68+
android:layout_height="wrap_content"
69+
android:layout_marginStart="32dp"
70+
android:layout_marginTop="24dp"
71+
android:layout_marginEnd="32dp"
72+
android:text="Войти"
73+
app:layout_constraintEnd_toEndOf="parent"
74+
app:layout_constraintStart_toStartOf="parent"
75+
app:layout_constraintTop_toBottomOf="@+id/password_input_layout" />
76+
77+
<com.google.android.material.textview.MaterialTextView
78+
android:id="@+id/text_view_result"
79+
android:layout_width="0dp"
80+
android:layout_height="wrap_content"
81+
android:layout_marginTop="12dp"
82+
android:layout_marginStart="24dp"
83+
android:layout_marginEnd="24dp"
84+
android:gravity="center"
85+
android:text=""
86+
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
87+
app:layout_constraintEnd_toEndOf="parent"
88+
app:layout_constraintStart_toStartOf="parent"
89+
app:layout_constraintTop_toBottomOf="@id/button_login" />
90+
91+
<com.google.android.material.textview.MaterialTextView
92+
android:id="@+id/text_view_disclaimer"
93+
android:layout_width="0dp"
94+
android:layout_height="wrap_content"
95+
android:layout_marginStart="24dp"
96+
android:layout_marginEnd="24dp"
97+
android:layout_marginBottom="32dp"
98+
android:gravity="center"
99+
android:text="Убедитесь, что используете официальную версию приложения с GitHub. Логин и пароль не сохраняются и используются только для получения токена авторизации.\nТакже вы можете напрямую сообщить токен в настройках, без авторизации."
100+
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
101+
app:layout_constraintBottom_toBottomOf="parent"
102+
app:layout_constraintEnd_toEndOf="parent"
103+
app:layout_constraintStart_toStartOf="parent" />
104+
105+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22

33
<style name="Base.AppTheme" parent="Theme.Material3.DynamicColors.DayNight">
4-
<item name="colorPrimary">@color/primary_color</item>
4+
<!-- <item name="colorPrimary">@color/primary_color</item>-->
55
<item name="android:statusBarColor">?android:colorBackground</item>
66
<item name="android:windowActionBar">false</item>
77
<item name="android:windowNoTitle">true</item>

app/src/main/res/xml/root_preferences.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
<PreferenceCategory app:title="Аккаунт" app:icon="@drawable/ic_account_circle">
55

6+
<Preference
7+
app:key="login_itmoid"
8+
app:title="Войти через ITMO.ID" />
9+
610
<EditTextPreference
711
app:key="refresh_token"
812
app:title="Ключ доступа"

0 commit comments

Comments
 (0)