-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'kotlin-android' | ||
id 'kotlin-android-extensions' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.2" | ||
|
||
defaultConfig { | ||
applicationId "com.developersbreach.simplesearchapp" | ||
minSdkVersion 22 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.3.2' | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.material:material:1.2.1' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.developersbreach.simplesearchapp"> | ||
|
||
<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/Base.AppTheme" | ||
tools:ignore="AllowBackup"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity android:name=".DetailActivity" /> | ||
</application> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.developersbreach.simplesearchapp | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
|
||
class DetailActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_detail) | ||
|
||
val sports: Sports? = intent.getParcelableExtra("DETAIL_SPORTS_DATA") | ||
|
||
findViewById<TextView>(R.id.detail_title_text).text = sports?.title | ||
findViewById<TextView>(R.id.detail_subtitle_text).text = sports?.originated | ||
findViewById<TextView>(R.id.detail_about_text).text = sports?.about | ||
sports?.icon?.let { | ||
findViewById<ImageView>(R.id.detail_image_view).setImageResource(it) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.developersbreach.simplesearchapp | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.speech.RecognizerIntent | ||
import android.view.View | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.appcompat.widget.AppCompatEditText | ||
import androidx.core.widget.doOnTextChanged | ||
import androidx.recyclerview.widget.RecyclerView | ||
import java.util.* | ||
import kotlin.collections.ArrayList | ||
|
||
class MainActivity : AppCompatActivity(), SearchAdapter.SportsAdapterListener { | ||
|
||
private lateinit var recyclerView: RecyclerView | ||
private lateinit var searchAdapter: SearchAdapter | ||
private lateinit var editText: AppCompatEditText | ||
private lateinit var noSearchResultsFoundText: TextView | ||
private lateinit var sportsList: List<Sports> | ||
private lateinit var clearQueryImageView: ImageView | ||
private lateinit var voiceSearchImageView: ImageView | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
recyclerView = findViewById(R.id.search_list) | ||
editText = findViewById(R.id.search_edit_text) | ||
noSearchResultsFoundText = findViewById(R.id.no_search_results_found_text) | ||
clearQueryImageView = findViewById(R.id.clear_search_query) | ||
voiceSearchImageView = findViewById(R.id.voice_search_query) | ||
|
||
sportsList = sportsList(applicationContext) | ||
attachAdapter(sportsList) | ||
|
||
editText.doOnTextChanged { text, _, _, _ -> | ||
val query = text.toString().toLowerCase(Locale.getDefault()) | ||
filterWithQuery(query) | ||
toggleImageView(query) | ||
} | ||
|
||
voiceSearchImageView.setOnClickListener { | ||
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { | ||
putExtra( | ||
RecognizerIntent.EXTRA_LANGUAGE_MODEL, | ||
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM | ||
) | ||
} | ||
startActivityForResult(intent, SPEECH_REQUEST_CODE) | ||
} | ||
|
||
clearQueryImageView.setOnClickListener { | ||
editText.setText("") | ||
} | ||
} | ||
|
||
private fun attachAdapter(list: List<Sports>) { | ||
searchAdapter = SearchAdapter(list, this) | ||
recyclerView.adapter = searchAdapter | ||
} | ||
|
||
private fun filterWithQuery(query: String) { | ||
if (query.isNotEmpty()) { | ||
val filteredList: List<Sports> = onFilterChanged(query) | ||
attachAdapter(filteredList) | ||
toggleRecyclerView(filteredList) | ||
} else if (query.isEmpty()) { | ||
attachAdapter(sportsList) | ||
} | ||
} | ||
|
||
private fun onFilterChanged(filterQuery: String): List<Sports> { | ||
val filteredList = ArrayList<Sports>() | ||
for (currentSport in sportsList) { | ||
if (currentSport.title.toLowerCase(Locale.getDefault()).contains(filterQuery)) { | ||
filteredList.add(currentSport) | ||
} | ||
} | ||
return filteredList | ||
} | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
if (requestCode == SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) { | ||
val spokenText: String? = | ||
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).let { results -> | ||
results?.get(0) | ||
} | ||
// Do something with spokenText | ||
editText.setText(spokenText) | ||
} | ||
super.onActivityResult(requestCode, resultCode, data) | ||
} | ||
|
||
private fun toggleRecyclerView(sportsList: List<Sports>) { | ||
if (sportsList.isEmpty()) { | ||
recyclerView.visibility = View.INVISIBLE | ||
noSearchResultsFoundText.visibility = View.VISIBLE | ||
} else { | ||
recyclerView.visibility = View.VISIBLE | ||
noSearchResultsFoundText.visibility = View.INVISIBLE | ||
} | ||
} | ||
|
||
private fun toggleImageView(query: String) { | ||
if (query.isNotEmpty()) { | ||
clearQueryImageView.visibility = View.VISIBLE | ||
voiceSearchImageView.visibility = View.INVISIBLE | ||
} else if (query.isEmpty()) { | ||
clearQueryImageView.visibility = View.INVISIBLE | ||
voiceSearchImageView.visibility = View.VISIBLE | ||
} | ||
} | ||
|
||
override fun onSportSelected(sports: Sports?) { | ||
val intent = Intent(applicationContext, DetailActivity::class.java) | ||
intent.putExtra("DETAIL_SPORTS_DATA", sports) | ||
startActivity(intent) | ||
} | ||
|
||
companion object { | ||
const val SPEECH_REQUEST_CODE = 0 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.developersbreach.simplesearchapp | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class SearchAdapter( | ||
private val sportsList: List<Sports>, | ||
private val listener: SportsAdapterListener | ||
) : RecyclerView.Adapter<SearchAdapter.SearchViewHolder>() { | ||
|
||
class SearchViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
val titleTextView: TextView = itemView.findViewById(R.id.search_title_text_view) | ||
val iconImageView: ImageView = itemView.findViewById(R.id.search_icon_image_view) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder { | ||
return SearchViewHolder( | ||
LayoutInflater.from(parent.context).inflate( | ||
R.layout.item_search, parent, false | ||
) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: SearchViewHolder, position: Int) { | ||
val sports: Sports = sportsList[position] | ||
holder.titleTextView.text = sports.title | ||
holder.iconImageView.setImageResource(sports.icon) | ||
|
||
holder.itemView.setOnClickListener { | ||
listener.onSportSelected(sports) | ||
} | ||
} | ||
|
||
override fun getItemCount() = sportsList.size | ||
|
||
interface SportsAdapterListener { | ||
fun onSportSelected(sports: Sports?) | ||
} | ||
} |