Skip to content

Commit

Permalink
Implements Collapse Toolbar in MainActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Sothearith Sreang committed Nov 4, 2017
1 parent 027c3e3 commit 1fa5a18
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package thearith.github.com.github_search.view.activities.search

import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
Expand All @@ -20,6 +21,7 @@ import javax.inject.Inject
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.SearchView
import android.widget.ProgressBar
import android.widget.Toolbar
import com.astro.astro.views.utils.setVisibility
import com.jakewharton.rxbinding2.support.v7.widget.RxSearchView
import thearith.github.com.github_search.view.activities.base.goToExternalUrl
Expand All @@ -33,6 +35,7 @@ class MainActivity : BaseActivity(), MainContract.View, GitHubSearchAdapter.OnCl
private val mWelcomeView : LogoWithTextView by bindView(R.id.view_welcome)
private val mErrorView : LogoWithTextView by bindView(R.id.view_error)
private val mNoResultView : LogoWithTextView by bindView(R.id.view_empty_result)
private val mToolbar : AppBarLayout by bindView(R.id.appbar_layout)

// Presenter
@Inject
Expand Down Expand Up @@ -77,7 +80,8 @@ class MainActivity : BaseActivity(), MainContract.View, GitHubSearchAdapter.OnCl
private fun setUpSearchViewTextChangeStream() {
val searchViewTextChangeStream =
RxSearchView.queryTextChanges(mSearchView)
.debounce(300, TimeUnit.MILLISECONDS)
.debounce(400, TimeUnit.MILLISECONDS)
.distinctUntilChanged()
.map { it.toString() }

val newSearchStream = searchViewTextChangeStream
Expand Down Expand Up @@ -147,20 +151,19 @@ class MainActivity : BaseActivity(), MainContract.View, GitHubSearchAdapter.OnCl
private fun showProgressMode(isRefresh : Boolean) {
val mode = if(isRefresh) Status.IN_PROGRESS else Status.IN_PROGRESS_WITH_REFRESH
updateUIVisibility(mode)
expandToolbar()

showLoading(true)
if(isRefresh) {
mSearchAdapter.clearAll()
}
}

private fun showLoading(isShown : Boolean) {
mSearchProgressBar.setVisibility(isShown)
private fun expandToolbar() {
mToolbar.setExpanded(true, true)
}

private fun showCompleteMode(response: SearchFeedResponse) {
updateUIVisibility(Status.COMPLETE)
showLoading(false)

showResultCount(response.response?.totalCount)
populateList(response.response)
Expand All @@ -177,23 +180,25 @@ class MainActivity : BaseActivity(), MainContract.View, GitHubSearchAdapter.OnCl

private fun showEmptyResult() {
updateUIVisibility(Status.NO_RESULT)
showLoading(false)
}

private fun handleError() {
updateUIVisibility(Status.ERROR)
showLoading(false)
}

private fun updateUIVisibility(mode : Status) {
val viewList = listOf(mWelcomeView, mErrorView, mNoResultView, mSearchRecyclerView)
val viewList = listOf(mWelcomeView, mErrorView, mNoResultView, mSearchRecyclerView, mSearchProgressBar)
viewList.forEach { it.setVisibility(false) }

when(mode) {
Status.IDLE -> mWelcomeView.visibility = View.VISIBLE

Status.IN_PROGRESS,
Status.IN_PROGRESS_WITH_REFRESH,
Status.IN_PROGRESS_WITH_REFRESH -> {
mSearchRecyclerView.visibility = View.VISIBLE
mSearchProgressBar.visibility = View.VISIBLE
}

Status.COMPLETE -> mSearchRecyclerView.visibility = View.VISIBLE

Status.NO_RESULT -> mNoResultView.visibility = View.VISIBLE
Expand Down
72 changes: 38 additions & 34 deletions GitHub-Search/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,56 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:fitsSystemWindows="true"
tools:context=".view.activities.search.MainActivity">

<include
layout="@layout/toolbar_main" />

<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:background="@drawable/logo_welcome"
app:text="@string/message_welcome" />

<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_empty_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:background="@drawable/logo_empty_result"
android:visibility="gone"
app:text="@string/message_empty_result"/>

<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_error"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:background="@drawable/logo_error"
android:visibility="gone"
app:text="@string/message_error"/>
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:background="@drawable/logo_welcome"
app:text="@string/message_welcome" />

<android.support.v7.widget.RecyclerView
android:id="@+id/rc_search"
<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_empty_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/search_item_padding"
android:layout_marginRight="@dimen/search_item_padding"/>
app:background="@drawable/logo_empty_result"
android:visibility="gone"
app:text="@string/message_empty_result"/>

<ProgressBar
android:id="@+id/pb_search"
<thearith.github.com.github_search.view.custom.LogoWithTextView
android:id="@+id/view_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/search_item_padding"
android:layout_gravity="bottom"/>
android:layout_height="match_parent"
app:background="@drawable/logo_error"
android:visibility="gone"
app:text="@string/message_error"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/rc_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/search_item_padding"
android:layout_marginRight="@dimen/search_item_padding" />

<ProgressBar
android:id="@+id/pb_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/search_item_padding"
android:layout_gravity="bottom"/>

</FrameLayout>

</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
43 changes: 20 additions & 23 deletions GitHub-Search/app/src/main/res/layout/toolbar_main.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_github" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_github"
android:layout_gravity="center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/white"
android:layout_marginLeft="@dimen/spacing_large"
android:text="@string/app_name"/>

</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/white"
android:paddingLeft="@dimen/spacing_large"
android:text="@string/app_name"
android:layout_gravity="center"
/>

</android.support.v7.widget.Toolbar>

Expand All @@ -57,4 +54,4 @@
</LinearLayout>


</LinearLayout>
</android.support.design.widget.AppBarLayout>

0 comments on commit 1fa5a18

Please sign in to comment.