Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Add simple and clean empty state behavior. #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import android.support.v7.util.DiffUtil
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent.KEYCODE_ENTER
import android.view.View
import android.view.View.INVISIBLE
Expand All @@ -20,6 +22,7 @@ import android.view.inputmethod.EditorInfo.IME_ACTION_GO
import android.view.inputmethod.InputMethodManager
import android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS
import android.widget.EditText
import android.widget.TextView
import androidx.content.systemService
import com.jakewharton.rxbinding2.support.v7.widget.scrollEvents
import com.jakewharton.rxbinding2.view.keys
Expand Down Expand Up @@ -101,10 +104,45 @@ class MainActivity : Activity() {
dividerDecoration.setDrawable(getDrawable(R.drawable.list_divider))
recycler.addItemDecoration(dividerDecoration)

val emptyStateTextView = findViewById<TextView>(R.id.emptyState)

val queryInput = findViewById<EditText>(R.id.query)
if (savedInstanceState == null) {
queryInput.setText(intent.getStringExtra("query") ?: "")
}
queryInput.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
override fun afterTextChanged(s: Editable) {
if (s.isEmpty()) {
emptyStateTextView.text = getString(R.string.enter_class_name)
emptyStateTextView.animate()
.scaleX(1f)
.scaleY(1f)
.alpha(1f)
.start()
} else {
emptyStateTextView.animate()
.scaleX(0f)
.scaleY(0f)
.alpha(0f)
.start()
}
}
})
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (queryInput.length() > 0 && adapter.itemCount == 0) {
emptyStateTextView.visibility = View.VISIBLE
emptyStateTextView.text = getString(R.string.no_results)
emptyStateTextView.animate()
.scaleX(1f)
.scaleY(1f)
.alpha(1f)
.start()
}
}
})

val enterKeys = queryInput.keys(Predicate { it.keyCode == KEYCODE_ENTER })
.filter { it.keyCode == KEYCODE_ENTER }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="96dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="96dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#8A000000" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
12 changes: 12 additions & 0 deletions frontend/android/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
/>
<TextView
android:id="@+id/emptyState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/enter_class_name"
android:drawableTop="@drawable/ic_search_black_24dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
2 changes: 2 additions & 0 deletions frontend/android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<item quantity="one">Search 1 class…</item>
<item quantity="other">Search %d classes…</item>
</plurals>
<string name="enter_class_name">Enter a class name</string>
<string name="no_results">No results</string>
<string name="copy">Copy</string>
<string name="share">Share…</string>
<string name="copied">%s URL copied to clipboard!</string>
Expand Down