Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
14 deletions.
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
101 changes: 93 additions & 8 deletions
101
...java/com/example/tamaskozmer/kotlinrxexample/presentation/view/activities/MainActivity.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 |
---|---|---|
@@ -1,23 +1,108 @@ | ||
package com.example.tamaskozmer.kotlinrxexample.view.activities | ||
|
||
import android.os.Bundle | ||
import android.support.design.widget.Snackbar | ||
import android.support.v7.app.AppCompatActivity | ||
import android.support.v7.widget.LinearLayoutManager | ||
import android.support.v7.widget.RecyclerView | ||
import android.view.View | ||
import android.widget.Toast | ||
import com.example.tamaskozmer.kotlinrxexample.R | ||
import com.example.tamaskozmer.kotlinrxexample.view.fragments.UserListFragment | ||
import com.example.tamaskozmer.kotlinrxexample.di.modules.UserListFragmentModule | ||
import com.example.tamaskozmer.kotlinrxexample.presentation.presenters.UserListPresenter | ||
import com.example.tamaskozmer.kotlinrxexample.presentation.view.UserListView | ||
import com.example.tamaskozmer.kotlinrxexample.presentation.view.viewmodels.UserViewModel | ||
import com.example.tamaskozmer.kotlinrxexample.util.customApplication | ||
import com.example.tamaskozmer.kotlinrxexample.view.adapters.UserListAdapter | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
|
||
class MainActivity : AppCompatActivity() { | ||
class MainActivity : AppCompatActivity(), UserListView { | ||
|
||
private val presenter: UserListPresenter by lazy { component.presenter() } | ||
private val component by lazy { customApplication.component.plus(UserListFragmentModule()) } | ||
private val adapter by lazy { | ||
val userList = mutableListOf<UserViewModel>() | ||
UserListAdapter(userList) { | ||
user -> showUserClickedSnackbar(user) | ||
} | ||
} | ||
|
||
private lateinit var layoutManager: LinearLayoutManager | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
addUserListFragment() | ||
initViews() | ||
initAdapter() | ||
|
||
presenter.attachView(this) | ||
|
||
showLoading() | ||
presenter.getUsers() | ||
} | ||
|
||
override fun onDestroy() { | ||
presenter.detachView() | ||
super.onDestroy() | ||
} | ||
|
||
private fun initViews() { | ||
swipeRefreshLayout.setOnRefreshListener { | ||
presenter.getUsers(forced = true) | ||
} | ||
} | ||
|
||
// region View interface methods | ||
override fun showLoading() { | ||
swipeRefreshLayout.isRefreshing = true | ||
} | ||
|
||
override fun hideLoading() { | ||
swipeRefreshLayout.isRefreshing = false | ||
} | ||
|
||
override fun addUsersToList(users: List<UserViewModel>) { | ||
val adapter = recyclerView.adapter as UserListAdapter | ||
adapter.addUsers(users) | ||
} | ||
|
||
override fun showEmptyListError() { | ||
errorView.visibility = View.VISIBLE | ||
} | ||
|
||
override fun hideEmptyListError() { | ||
errorView.visibility = View.GONE | ||
} | ||
|
||
override fun showToastError() { | ||
Toast.makeText(this, "Error loading data", Toast.LENGTH_SHORT).show() | ||
} | ||
|
||
override fun clearList() { | ||
adapter.clearUsers() | ||
} | ||
// endregion | ||
|
||
private fun initAdapter() { | ||
layoutManager = LinearLayoutManager(customApplication) | ||
recyclerView.layoutManager = layoutManager | ||
|
||
recyclerView.adapter = adapter | ||
|
||
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { | ||
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { | ||
|
||
val lastVisibleItemPosition = layoutManager.findFirstVisibleItemPosition() + layoutManager.childCount | ||
val totalItemCount = layoutManager.itemCount | ||
|
||
presenter.onScrollChanged(lastVisibleItemPosition, totalItemCount) | ||
} | ||
}) | ||
} | ||
|
||
private fun addUserListFragment() { | ||
supportFragmentManager | ||
.beginTransaction() | ||
.replace(R.id.fragmentContainer, UserListFragment()) | ||
.commit() | ||
private fun showUserClickedSnackbar(user: UserViewModel) { | ||
Snackbar.make(recyclerView, "${user.displayName}: ${user.reputation} pts", Snackbar.LENGTH_SHORT) | ||
.show() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
tools:context="com.example.tamaskozmer.kotlinrxexample.view.activities.MainActivity"> | ||
|
||
<FrameLayout | ||
android:id="@+id/fragmentContainer" | ||
<TextView | ||
android:id="@+id/errorView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:drawablePadding="16dp" | ||
android:drawableTop="@drawable/ic_error" | ||
android:padding="16dp" | ||
android:text="@string/user_list_error" | ||
android:visibility="gone"/> | ||
|
||
<android.support.v4.widget.SwipeRefreshLayout | ||
android:id="@+id/swipeRefreshLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
android:layout_height="match_parent"> | ||
|
||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/recyclerView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:scrollbars="vertical" /> | ||
|
||
</android.support.v4.widget.SwipeRefreshLayout> | ||
|
||
</LinearLayout> | ||
</FrameLayout> |