-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #566 from KasperskyLab/ISSUE-372-update-tutorial
Edit examples for screenshot test lesson
- Loading branch information
Showing
16 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/ApiFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
object ApiFactory { | ||
|
||
private val retrofit = Retrofit.Builder() | ||
.baseUrl("https://random-data-api.com/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
val apiService = retrofit.create(ApiService::class.java) | ||
} |
9 changes: 9 additions & 0 deletions
9
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/ApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import retrofit2.http.GET | ||
|
||
interface ApiService { | ||
|
||
@GET("api/users/random_user") | ||
suspend fun loadUser(): User | ||
} |
18 changes: 18 additions & 0 deletions
18
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/LoadUserActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.kaspersky.kaspresso.tutorial.R | ||
|
||
class LoadUserActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_load_user) | ||
if (savedInstanceState == null) { | ||
supportFragmentManager.beginTransaction() | ||
.replace(R.id.fragment_container, LoadUserFragment.newInstance()) | ||
.commit() | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/LoadUserFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.view.isVisible | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import com.kaspersky.kaspresso.tutorial.databinding.FragmentLoadUserBinding | ||
import kotlinx.coroutines.launch | ||
|
||
class LoadUserFragment : Fragment() { | ||
|
||
private var _binding: FragmentLoadUserBinding? = null | ||
private val binding: FragmentLoadUserBinding | ||
get() = _binding ?: throw IllegalStateException("FragmentLoadUserBinding is null") | ||
|
||
private lateinit var viewModel: LoadUserViewModel | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
_binding = FragmentLoadUserBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
viewModel = ViewModelProvider(this)[LoadUserViewModel::class.java] | ||
binding.loadingButton.setOnClickListener { | ||
viewModel.loadUser() | ||
} | ||
observeViewModel() | ||
} | ||
|
||
private fun observeViewModel() { | ||
viewLifecycleOwner.lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
viewModel.state.collect { state -> | ||
when (state) { | ||
State.Initial -> { | ||
binding.progressBarLoading.isVisible = false | ||
binding.loadingButton.isEnabled = true | ||
binding.error.isVisible = false | ||
binding.username.isVisible = false | ||
} | ||
is State.Content -> { | ||
binding.progressBarLoading.isVisible = false | ||
binding.loadingButton.isEnabled = true | ||
binding.error.isVisible = false | ||
binding.username.isVisible = true | ||
|
||
val user = state.user | ||
binding.username.text = "${user.name} ${user.lastName}" | ||
} | ||
State.Error -> { | ||
binding.progressBarLoading.isVisible = false | ||
binding.loadingButton.isEnabled = true | ||
binding.error.isVisible = true | ||
binding.username.isVisible = false | ||
} | ||
State.Progress -> { | ||
binding.progressBarLoading.isVisible = true | ||
binding.loadingButton.isEnabled = false | ||
binding.error.isVisible = false | ||
binding.username.isVisible = false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
|
||
companion object { | ||
|
||
fun newInstance(): LoadUserFragment = LoadUserFragment() | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/LoadUserRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
object LoadUserRepository { | ||
|
||
private val apiService = ApiFactory.apiService | ||
|
||
suspend fun loadUser(): User = apiService.loadUser() | ||
} |
38 changes: 38 additions & 0 deletions
38
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/LoadUserViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class LoadUserViewModel : ViewModel() { | ||
|
||
private val repository = LoadUserRepository | ||
|
||
private val _state = MutableStateFlow<State>(State.Initial) | ||
val state = _state.asStateFlow() | ||
|
||
fun loadUser() { | ||
viewModelScope.launch { | ||
_state.value = State.Progress | ||
try { | ||
val user = repository.loadUser() | ||
_state.value = State.Content(user) | ||
} catch (e: Exception) { | ||
_state.value = State.Error | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed class State { | ||
|
||
data class Content(val user: User) : State() | ||
|
||
object Progress : State() | ||
|
||
object Error : State() | ||
|
||
object Initial : State() | ||
} |
8 changes: 8 additions & 0 deletions
8
tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/user/User.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kaspersky.kaspresso.tutorial.user | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class User( | ||
@SerializedName("first_name") val name: String, | ||
@SerializedName("last_name") val lastName: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/fragment_container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_gravity="center_horizontal" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
android:padding="16dp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_gravity="center_horizontal" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
android:padding="16dp"> | ||
|
||
<TextView | ||
android:id="@+id/username" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="16dp" | ||
android:textSize="24sp" | ||
android:visibility="gone" | ||
app:layout_constraintBottom_toTopOf="@id/loading_button" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
tools:text="John Smith" | ||
tools:visibility="visible" /> | ||
|
||
<TextView | ||
android:id="@+id/error" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="16dp" | ||
android:text="@string/something_went_wrong" | ||
android:textColor="@android:color/holo_red_light" | ||
android:textSize="24sp" | ||
android:visibility="gone" | ||
app:layout_constraintBottom_toTopOf="@id/loading_button" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
tools:visibility="visible" /> | ||
|
||
<ProgressBar | ||
android:id="@+id/progress_bar_loading" | ||
android:layout_width="42dp" | ||
android:layout_height="42dp" | ||
android:layout_marginBottom="16dp" | ||
android:visibility="gone" | ||
app:layout_constraintBottom_toTopOf="@id/loading_button" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
tools:visibility="visible" /> | ||
|
||
<Button | ||
android:id="@+id/loading_button" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:backgroundTint="@color/colorPrimary" | ||
android:text="@string/load_user" | ||
android:textColor="@color/white" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="app_name">Tutoriel</string> | ||
|
||
<!-- Simple Activity screen --> | ||
<string name="simple_activity_default_title">Titre par défaut</string> | ||
<string name="simple_activity_input_hint">Texte d\'entrée</string> | ||
<string name="simple_activity_change_title_button">Modifier le titre</string> | ||
|
||
<!-- Wifi Activity screen --> | ||
<string name="enabled_status">permettre</string> | ||
<string name="disabled_status">désactivée</string> | ||
|
||
<!-- Notification Activity screen --> | ||
<string name="show_notification_button">afficher la notification</string> | ||
<string name="notification_title">Titre de la Notification</string> | ||
<string name="notification_content">Contenu de la Notification</string> | ||
|
||
<!-- Login Activity screen --> | ||
<string name="login_activity_hint_username">TODO: add french locale</string> | ||
<string name="login_activity_hint_password">TODO: add french locale</string> | ||
<string name="login_activity_button_login">TODO: add french locale</string> | ||
|
||
<!-- After Login screens --> | ||
<string name="screen_after_login">Écran après autorisation</string> | ||
|
||
<!-- Make call screen--> | ||
<string name="phone_number_hint">Numéro de téléphone:</string> | ||
<string name="make_call_btn">Passer un Appel</string> | ||
|
||
<!-- Flaky Screen --> | ||
<string name="text_1">Texte 1</string> | ||
<string name="text_2">Texte 2</string> | ||
<string name="text_3">Texte 3</string> | ||
<string name="text_4">Texte 4</string> | ||
<string name="text_5">Texte 5</string> | ||
|
||
<!-- Screenshot sample--> | ||
<string name="something_went_wrong">Quelque chose s\'est mal passé</string> | ||
<string name="load_user">Charger l\'utilisateur</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters