Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
84218c8
feat: declares the layoutManager and the adapter of recyclerSearch in…
SergioRiosRibeiro Jun 14, 2024
0343d44
Merge branch 'feature/user_search-screen-lists-random-tattoos' of htt…
SergioRiosRibeiro Jun 14, 2024
8c60278
feat: creates the AdapterUserSearch class
SergioRiosRibeiro Jun 14, 2024
4ecfe6f
feat: creates UserSearchViewHolder class
SergioRiosRibeiro Jun 14, 2024
7258e2b
feat: creates grid_search.xml to configure the creation of the elemen…
SergioRiosRibeiro Jun 15, 2024
0795f8f
feat: suspend fun getRandomTattoos signature has been created in APIS…
SergioRiosRibeiro Jul 15, 2024
102742e
feat: Creates UserSearchRepository and implementes suspend fun getRan…
SergioRiosRibeiro Jul 15, 2024
dfe2abf
feat: implements the whole AdapterUserSearch
SergioRiosRibeiro Jul 15, 2024
4d464a0
feat: implements the whole UserSearchViewHolder
SergioRiosRibeiro Jul 15, 2024
669f78c
feat: creates const val LIMIT_USER_SEARCH_QUERY at Constants.kt
SergioRiosRibeiro Jul 15, 2024
329a4f6
feat: creates fun getRandomTattoos, live data variables imagesTattooU…
SergioRiosRibeiro Jul 15, 2024
5adf1ff
feat: creates fun observe and fun getRandomTattoos to deal with the s…
SergioRiosRibeiro Jul 15, 2024
95ed9df
refactor: small changes in AdapterUserSearch after sonarcloud review
SergioRiosRibeiro Jul 16, 2024
dfd9d15
fix: resolves branches conflicts
SergioRiosRibeiro Jul 16, 2024
236f52e
Merge branch 'develop' into feature/user_search-screen-lists-random-t…
natanaelsc Jul 27, 2024
09edf30
Merge branch 'develop' into feature/user_search-screen-lists-random-t…
natanaelsc Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/src/main/java/br/com/connectattoo/adapter/AdapterUserSearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package br.com.connectattoo.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import br.com.connectattoo.databinding.GridSearchBinding

class AdapterUserSearch(private val myDataSet: MutableList<MyImage>) :
RecyclerView.Adapter<UserSearchViewHolder>() {
var i: Int = 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserSearchViewHolder {
val item = GridSearchBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
return UserSearchViewHolder(item)
}

override fun onBindViewHolder(holder: UserSearchViewHolder, position: Int) {
holder.bind(myDataSet[position])
}

override fun getItemCount(): Int {
return myDataSet.count()
}

fun updateTattooImages(listImagesTattoo: List<String>): MutableList<MyImage> {
for (image in listImagesTattoo) {
i++
if (i != 8) {
myDataSet.add(MyImage(image, false))
} else {
myDataSet.add(MyImage(image, true))
}
}
notifyDataSetChanged()
return myDataSet
}

data class MyImage(val tattooRandomImage: String, val isFullSpan: Boolean)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.connectattoo.adapter

import androidx.recyclerview.widget.RecyclerView
import br.com.connectattoo.databinding.GridSearchBinding
import com.bumptech.glide.Glide

class UserSearchViewHolder(private val bind: GridSearchBinding) : RecyclerView.ViewHolder(bind.root) {

fun bind(listImagesTattoo: AdapterUserSearch.MyImage) {
bind.imgTattooCard.apply { Glide.with(this).load(listImagesTattoo).into(this) }
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/br/com/connectattoo/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import retrofit2.http.Multipart
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Query

interface ApiService {

Expand Down Expand Up @@ -51,6 +52,12 @@ interface ApiService {
@Part image: MultipartBody.Part
): Response<Unit>

@GET("tattoos/random")
suspend fun getRandomTattoos(
@Query("limit") limit: Int,
@Header("Authorization") authorization: String
): Call<String>

@PATCH("profile/me")
suspend fun updateProfile(
@Header("Authorization") authorization: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package br.com.connectattoo.repository

import br.com.connectattoo.api.ApiService
import br.com.connectattoo.api.ApiUrl
import br.com.connectattoo.ui.search.UserSearchViewModel
import br.com.connectattoo.utils.Constants
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class UserSearchRepository {
val apiService: ApiService = ApiUrl.instance.create(ApiService::class.java)

suspend fun getRandomTattoos(
limit: Int,
token: String,
listener: UserSearchViewModel.Listener
) {
val call = apiService.getRandomTattoos(limit, token)
call.enqueue(object : Callback<String> {
override fun onResponse(call: Call<String>, response: Response<String>) {
if (response.code() == Constants.CODE_SUCCESS_200) {
response.body()?.let { listener.onSuccess(it) }
} else {
listener.onFailure("Erro ao carregar. Tente mais tarde")
}

}

override fun onFailure(call: Call<String>, t: Throwable) {
listener.onFailure("Erro ao carregar. Tente mais tarde")
}

})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import br.com.connectattoo.adapter.AdapterUserSearch
import br.com.connectattoo.databinding.FragmentUserSearchBinding
import br.com.connectattoo.repository.UserSearchRepository
import br.com.connectattoo.ui.BaseFragment
import br.com.connectattoo.utils.Constants
import br.com.connectattoo.utils.DataStoreManager
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch

class UserSearchFragment : BaseFragment<FragmentUserSearchBinding>() {
private lateinit var viewModel: UserSearchViewModel
private var repository: UserSearchRepository = UserSearchRepository()
private val recyclerSearch = binding.recyclerImagesSearch
private lateinit var adapter: AdapterUserSearch

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -20,10 +31,11 @@ class UserSearchFragment : BaseFragment<FragmentUserSearchBinding>() {
val searchView = binding.fragmentUserSearch
searchView.visibility = View.VISIBLE

val recyclerSearch = binding.recyclerImagesSearch

recyclerSearch.layoutManager = GridLayoutManager(context,3)

recyclerSearch.layoutManager =
StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
recyclerSearch.adapter = adapter
observe()
getRandomTattoos()
}

override fun inflateBinding(
Expand All @@ -34,6 +46,17 @@ class UserSearchFragment : BaseFragment<FragmentUserSearchBinding>() {
return FragmentUserSearchBinding.inflate(inflater, container, false)
}

private fun getRandomTattoos() {
viewLifecycleOwner.lifecycleScope.launch {
val token = DataStoreManager.getUserSettings(requireContext(), Constants.API_TOKEN)
viewModel.getRandomTattoos(repository, token)
}
}


private fun observe() {
viewModel.imagesTattooUserSearch.observe(this) {
adapter.updateTattooImages(it)
}
viewModel.message.observe(this) { Snackbar.make(requireView(), it, Snackbar.LENGTH_SHORT) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,41 @@ package br.com.connectattoo.ui.search
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import br.com.connectattoo.repository.UserSearchRepository
import br.com.connectattoo.utils.Constants
import kotlinx.coroutines.launch

class UserSearchViewModel : ViewModel() {

private val imagesTattooUserSearch: MutableLiveData<List<String>> = MutableLiveData(
listOf(
))
private val _imagesTattooUserSearch = MutableLiveData<MutableList<String>>()
val imagesTattooUserSearch: LiveData<MutableList<String>> = _imagesTattooUserSearch

val imagesTattooUserSearch2: LiveData<List<String>> = imagesTattooUserSearch
private val _message = MutableLiveData<String>()
val message: LiveData<String> = _message

fun getRandomTattoos(repository: UserSearchRepository, token: String) {

viewModelScope.launch {
repository.getRandomTattoos(
Constants.LIMIT_USER_SEARCH_QUERY,
token,
object : Listener {

override fun onSuccess(link: String) {
_imagesTattooUserSearch.value?.add(link)
}

override fun onFailure(error: String) {
_message.value = error
}

})
}
}

interface Listener {
fun onSuccess(link: String)
fun onFailure(error: String)
}
}
1 change: 1 addition & 0 deletions app/src/main/java/br/com/connectattoo/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ object Constants {
const val TYPE_RANDOM_TATTOOS = 4
const val TYPE_MORE_ITEMS_RANDOM_TATTOOS = 5
const val DATABASE_NAME: String = "connectattoo"
const val LIMIT_USER_SEARCH_QUERY = 9
const val BEARER: String = "Bearer"
}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/grid_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
android:id="@+id/img_tattoo_card"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="8dp"
android:layout_width="100dp"
android:layout_height="125dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>