Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,44 +1,68 @@
package com.devpass.githubapp.presentation

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.ui.AppBarConfiguration
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.devpass.githubapp.R
import com.devpass.githubapp.data.api.GitHubEndpoint
import com.devpass.githubapp.data.model.Repository
import com.devpass.githubapp.databinding.ActivityMainBinding
import com.devpass.githubapp.utils.NetworkUtils
import com.google.android.material.snackbar.Snackbar
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class RepositoryListActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val cellItemAdapter = RepositoryListAdapter()

private val snackBarError: Snackbar
get() = Snackbar.make(
binding.root,
R.string.snackbar_error,
Snackbar.LENGTH_INDEFINITE
).apply {
setAction(R.string.retry) { requestData() }
}

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

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

setSupportActionBar(binding.toolbar)
setupLayout()

requestData()
}

private fun requestData() {
val retrofitClient = NetworkUtils.getRetrofitInstance("https://api.github.com")
val endpoint = retrofitClient.create(GitHubEndpoint::class.java)
val callback = endpoint.getRepositories("devpass-tech")

callback.enqueue(object : Callback<List<Repository>> {
override fun onFailure(call: Call<List<Repository>>, t: Throwable) {
Toast.makeText(baseContext, t.message, Toast.LENGTH_SHORT).show()
snackBarError.show()
}

override fun onResponse(call: Call<List<Repository>>, response: Response<List<Repository>>) {

response.body()?.toString()?.let { Log.d("BODY", it) }
override fun onResponse(
call: Call<List<Repository>>,
response: Response<List<Repository>>
) {
response.body()?.let { list ->
cellItemAdapter.repositories = list
cellItemAdapter.notifyDataSetChanged()
} ?: snackBarError.show()
}
})
}

private fun setupLayout() {
binding.contentList.repositoriesRecyclerview.layoutManager =
LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
binding.contentList.repositoriesRecyclerview.adapter = cellItemAdapter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.devpass.githubapp.databinding.ItemCellRepositoryBinding

class RepositoryListAdapter : RecyclerView.Adapter<RepositoryCellItem>() {

private val repositories: List<Repository> = listOf()
var repositories: List<Repository> = listOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepositoryCellItem {
val inflater = LayoutInflater.from(parent.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_repository_list"/>
<include
android:id="@+id/content_list"
layout="@layout/content_repository_list"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
<string name="previous">Previous</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="avatar_content_description">The image shows the repository avatar</string>
<string name="snackbar_error">An error occurred while trying to get the list</string>
<string name="retry">Retry</string>
</resources>