From 144a0cc8b05aaf3531812c7e15124ad435db33b6 Mon Sep 17 00:00:00 2001 From: Jina-n11 Date: Mon, 8 Aug 2022 20:20:24 +0300 Subject: [PATCH 01/16] add observable search --- app/build.gradle | 2 ++ .../com/cheese/weatherapp/ui/MainActivity.kt | 35 ++++++++----------- .../com/cheese/weatherapp/util/Constants.kt | 1 + app/src/main/res/values-night/themes.xml | 16 --------- app/src/main/res/values/themes.xml | 6 ++-- 5 files changed, 21 insertions(+), 39 deletions(-) delete mode 100644 app/src/main/res/values-night/themes.xml diff --git a/app/build.gradle b/app/build.gradle index 89e3805..90add49 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,4 +46,6 @@ dependencies { implementation 'com.google.code.gson:gson:2.9.0' implementation 'com.squareup.okhttp3:okhttp:4.10.0' implementation 'com.airbnb.android:lottie:5.2.0' + implementation "io.reactivex.rxjava3:rxjava:3.1.5" + } \ No newline at end of file diff --git a/app/src/main/java/com/cheese/weatherapp/ui/MainActivity.kt b/app/src/main/java/com/cheese/weatherapp/ui/MainActivity.kt index 81da396..db6eb6d 100644 --- a/app/src/main/java/com/cheese/weatherapp/ui/MainActivity.kt +++ b/app/src/main/java/com/cheese/weatherapp/ui/MainActivity.kt @@ -3,6 +3,7 @@ import android.text.Editable import android.text.TextWatcher import android.view.LayoutInflater import android.widget.Toast +import androidx.core.widget.doOnTextChanged import com.cheese.weatherapp.R import com.cheese.weatherapp.databinding.ActivityMainBinding import com.cheese.weatherapp.models.WeatherState @@ -11,8 +12,10 @@ import com.example.mikmok.util.Constants import com.example.mikmok.util.toCelsius import com.example.mikmok.util.toPercent import com.google.gson.Gson +import io.reactivex.rxjava3.core.Observable import okhttp3.* import java.io.IOException +import java.util.concurrent.TimeUnit class MainActivity : BaseActivity(){ private val client = OkHttpClient() @@ -26,32 +29,24 @@ class MainActivity : BaseActivity(){ override fun addCallbacks() { onSearchChange() - onClickSearchButton() } - private fun onClickSearchButton()= binding.buttonSearch.setOnClickListener { - cityName = binding.searchCity.text.toString() - getWeather(cityName=cityName) - } - private fun onSearchChange() = binding.searchCity.addTextChangedListener(object : TextWatcher { - override fun afterTextChanged(s: Editable) {} - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - visibleButton() - } - }) + private fun onSearchChange(){ + val observableSearch =Observable.create{ emitter-> + binding.searchCity.doOnTextChanged{text, start, before, count -> + emitter.onNext(text.toString()) + } + }.debounce(1,TimeUnit.SECONDS) + observableSearch.subscribe( + {t->getWeather(cityName=t)}, + {e-> showToast(message = "${Constants.CITY_NOT_FOUND} : $e") + } + ) - private fun visibleButton(){ - binding.buttonSearch.visibility =android.view.View.VISIBLE - if(binding.searchCity.text.isEmpty()){ - binding.buttonSearch.visibility =android.view.View.INVISIBLE - } - else{ - binding.buttonSearch.visibility =android.view.View.VISIBLE - } } + private fun getWeather(cityName:String) { val request = Request.Builder() .url(Constants.API_URL+"${cityName.trim()}") diff --git a/app/src/main/java/com/cheese/weatherapp/util/Constants.kt b/app/src/main/java/com/cheese/weatherapp/util/Constants.kt index 7044380..cd87c28 100644 --- a/app/src/main/java/com/cheese/weatherapp/util/Constants.kt +++ b/app/src/main/java/com/cheese/weatherapp/util/Constants.kt @@ -9,4 +9,5 @@ object Constants { const val TYPE ="application/json" const val CITY ="Baghdad" const val MAIN_ACTIVITY ="MAIN_ACTIVITY" + const val CITY_NOT_FOUND ="city not found" } \ No newline at end of file diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml deleted file mode 100644 index 2d4a99f..0000000 --- a/app/src/main/res/values-night/themes.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index a8a7122..2d4a99f 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -2,12 +2,12 @@