Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.nglauber.marvel">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CharactersActivity">
<activity android:name=".view.characterslist.CharactersActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.com.nglauber.marvel.view.characterslist

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.os.Parcelable
Expand All @@ -9,6 +10,7 @@ import android.support.v7.widget.RecyclerView
import android.util.Log
import br.com.nglauber.marvel.R
import br.com.nglauber.marvel.model.api.MarvelApi.loadCharacters
import kotlinx.android.synthetic.main.activity_characters.*

class CharactersActivity : AppCompatActivity() {

Expand All @@ -28,21 +30,26 @@ class CharactersActivity : AppCompatActivity() {

val llm = LinearLayoutManager(this)
recyclerCharacters.layoutManager = llm
// recyclerCharacters.adapter = Companion.adapter

recyclerCharacters.adapter = adapter
recyclerCharacters.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val lastVisibleItemPosition = llm.findLastVisibleItemPosition()
// if (lastVisibleItemPosition == Companion.adapter.itemCount - 1 && !viewModel.isLoading) {
if (lastVisibleItemPosition == adapter.itemCount - 1 && !viewModel.isLoading) {
Log.d("NGVL", "Loading more...")
loadCharacters(viewModel.currentPage + 1)

}
}
})
viewModel.getCharacters().observe(this, Observer { characters ->
characters?.let {
adapter.setItems(characters)
}
if (recyclerState != null) {
recyclerCharacters.layoutManager.onRestoreInstanceState(recyclerState)
recyclerState = null
}
})
loadCharacters(0)
}

Expand All @@ -57,16 +64,6 @@ class CharactersActivity : AppCompatActivity() {
}

private fun loadCharacters(page: Int) {
// viewModel.load(page)
viewModel.load(page, { characters ->
characters.forEach{ character ->
Log.d("NGVL", "${character.id} - ${character.name}")
adapter.add(character)
}
if (recyclerState != null) {
recyclerCharacters.layoutManager.onRestoreInstanceState(recyclerState)
recyclerState = null
}
})
viewModel.load(page)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CharactersViewModel : ViewModel() {
characters.value = emptyList()
}

fun load(page: Int, param: (Any) -> Unit) {
fun load(page: Int) {
launch(UI) {
isLoading = true
if (page > currentPage) {
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/res/layout/activity_characters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CharactersActivity">
tools:context="br.com.nglauber.marvel.view.characterslist.CharactersActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerCharacters"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
34 changes: 34 additions & 0 deletions app/src/main/res/layout/item_character.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content">

<ImageView
android:id="@+id/imgThumbnail"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imgThumbnail"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
</android.support.constraint.ConstraintLayout>