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

Commit 88ae9ce

Browse files
nickbutcherGerrit Code Review
authored andcommitted
Merge "Add search screen."
2 parents d45df87 + c1ac7c7 commit 88ae9ce

File tree

6 files changed

+187
-14
lines changed

6 files changed

+187
-14
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.materialstudies.owl.model
18+
19+
import androidx.recyclerview.widget.DiffUtil
20+
21+
typealias Subject = String
22+
23+
object SubjectDiff : DiffUtil.ItemCallback<Subject>() {
24+
override fun areItemsTheSame(oldItem: Subject, newItem: Subject) = oldItem == newItem
25+
override fun areContentsTheSame(oldItem: Subject, newItem: Subject) = oldItem == newItem
26+
}
27+
28+
val subjects = listOf(
29+
"Architecture",
30+
"Arts & Crafts",
31+
"Business",
32+
"Culinary",
33+
"Design",
34+
"Fashion",
35+
"Film",
36+
"Gaming",
37+
"Illustration",
38+
"Lifestyle",
39+
"Music",
40+
"Painting 101",
41+
"Photography",
42+
"Technology"
43+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.materialstudies.owl.ui.search
18+
19+
import android.view.LayoutInflater
20+
import android.view.ViewGroup
21+
import androidx.recyclerview.widget.ListAdapter
22+
import androidx.recyclerview.widget.RecyclerView
23+
import com.materialstudies.owl.databinding.SearchItemBinding
24+
import com.materialstudies.owl.model.Subject
25+
import com.materialstudies.owl.model.SubjectDiff
26+
27+
class SearchAdapter : ListAdapter<Subject, SearchViewHolder>(SubjectDiff) {
28+
29+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
30+
val binding = SearchItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
31+
return SearchViewHolder(binding)
32+
}
33+
34+
override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
35+
holder.bind(getItem(position))
36+
}
37+
}
38+
39+
class SearchViewHolder(
40+
private val binding: SearchItemBinding
41+
) : RecyclerView.ViewHolder(binding.root) {
42+
43+
fun bind(subject: Subject) {
44+
binding.run {
45+
this.subject = subject
46+
executePendingBindings()
47+
}
48+
}
49+
50+
}

app/src/main/java/com/materialstudies/owl/ui/search/SearchFragment.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ import android.view.View
2222
import android.view.ViewGroup
2323
import androidx.fragment.app.Fragment
2424
import com.materialstudies.owl.R
25+
import com.materialstudies.owl.databinding.FragmentSearchBinding
26+
import com.materialstudies.owl.model.subjects
2527

2628
class SearchFragment : Fragment() {
2729

28-
override fun onCreate(savedInstanceState: Bundle?) {
29-
super.onCreate(savedInstanceState)
30-
}
31-
3230
override fun onCreateView(
3331
inflater: LayoutInflater,
3432
container: ViewGroup?,
3533
savedInstanceState: Bundle?
3634
): View? {
37-
// Inflate the layout for this fragment
38-
return inflater.inflate(R.layout.fragment_search, container, false)
35+
val binding = FragmentSearchBinding.inflate(inflater, container, false).apply {
36+
searchResults.apply {
37+
adapter = SearchAdapter().apply {
38+
submitList(subjects)
39+
}
40+
}
41+
}
42+
return binding.root
3943
}
4044
}

app/src/main/res/layout/fragment_search.xml

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,56 @@
1212
or implied. See the License for the specific language governing permissions and limitations under
1313
the License.
1414
-->
15-
<FrameLayout
15+
<layout
1616
xmlns:android="http://schemas.android.com/apk/res/android"
17-
xmlns:tools="http://schemas.android.com/tools"
18-
android:layout_width="match_parent"
19-
android:layout_height="match_parent"
20-
tools:context=".ui.search.SearchFragment">
17+
xmlns:app="http://schemas.android.com/apk/res-auto"
18+
xmlns:tools="http://schemas.android.com/tools">
2119

22-
<TextView
20+
<androidx.coordinatorlayout.widget.CoordinatorLayout
2321
android:layout_width="match_parent"
2422
android:layout_height="match_parent"
25-
android:text="Search"/>
23+
android:background="?attr/colorBrandedSurface"
24+
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
25+
tools:context=".ui.search.SearchFragment">
2626

27-
</FrameLayout>
27+
<com.google.android.material.appbar.AppBarLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
app:liftOnScroll="true"
31+
app:liftOnScrollTargetViewId="@id/search_results">
32+
33+
<androidx.appcompat.widget.Toolbar
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:minHeight="?attr/actionBarSize"
37+
app:layout_scrollFlags="scroll|enterAlways|snap"
38+
app:menu="@menu/account"
39+
app:navigationIcon="@drawable/ic_search">
40+
41+
<EditText
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:background="@null"
45+
android:lines="1"
46+
android:selectAllOnFocus="true"
47+
android:inputType="textCapWords"
48+
android:imeOptions="actionSearch"
49+
android:hint="@string/search_owl"
50+
android:textAppearance="?attr/textAppearanceSubtitle1" />
51+
52+
</androidx.appcompat.widget.Toolbar>
53+
54+
</com.google.android.material.appbar.AppBarLayout>
55+
56+
<androidx.recyclerview.widget.RecyclerView
57+
android:id="@+id/search_results"
58+
android:layout_width="match_parent"
59+
android:layout_height="match_parent"
60+
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
61+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
62+
app:hasFixedSize="@{true}"
63+
android:scrollbars="vertical"/>
64+
65+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
66+
67+
</layout>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (c) 2019 Google Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
in compliance with the License. You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software distributed under the License
11+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
or implied. See the License for the specific language governing permissions and limitations under
13+
the License.
14+
-->
15+
<layout
16+
xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:tools="http://schemas.android.com/tools"
18+
tools:targetApi="o">
19+
20+
<data>
21+
<variable name="subject" type="String"/>
22+
</data>
23+
24+
<TextView
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:paddingHorizontal="@dimen/grid_2"
28+
android:paddingVertical="@dimen/grid_1"
29+
android:background="?attr/selectableItemBackground"
30+
android:focusable="true"
31+
android:clickable="true"
32+
android:textAppearance="?attr/textAppearanceHeadline5"
33+
android:text="@{subject}"/>
34+
35+
</layout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<string name="course_steps">%1$s / %2$s</string>
1919
<string name="add_course">Add Course</string>
2020
<string name="account">Account</string>
21+
<string name="search_owl">Search Owl</string>
2122

2223
</resources>

0 commit comments

Comments
 (0)