From c4ef3acdc0ad1ab949dfbd336db9b660b7165399 Mon Sep 17 00:00:00 2001 From: leavesCZY <1990724437@qq.com> Date: Sun, 24 Apr 2022 11:04:54 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=BC=80=E6=94=BE=20ReactiveFragment=202.?= =?UTF-8?q?=20=E8=A1=A5=E5=85=85=20ReactiveFragment=20=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 16 +-- .../ExampleInstrumentedTest.kt | 22 --- app/src/main/AndroidManifest.xml | 12 +- .../reactivehttpsamples/MainActivity.kt | 8 +- .../{PlaceAdapter.kt => AreaAdapter.kt} | 10 +- .../adapter/DailyForecastAdapter.kt | 2 + .../reactivehttpsamples/base/BaseFragment.kt | 11 ++ .../core/http/AppRemoteDataSource.kt | 8 +- .../core/viewmodel/MapViewModel.kt | 128 ------------------ .../core/viewmodel/weather/CityViewModel.kt | 30 ++++ .../core/viewmodel/weather/CountyViewModel.kt | 31 +++++ .../viewmodel/weather/ProvinceViewModel.kt | 30 ++++ .../{ => weather}/WeatherViewModel.kt | 2 +- .../reactivehttpsamples/ui/MapActivity.kt | 71 ---------- .../ui/SingleRequestActivity.kt | 4 +- .../ui/TogetherRequestActivity.kt | 4 +- .../ui/weather/AreaActivity.kt | 78 +++++++++++ .../ui/weather/CityFragment.kt | 84 ++++++++++++ .../ui/weather/CountyFragment.kt | 90 ++++++++++++ .../ui/weather/ProvinceFragment.kt | 61 +++++++++ .../ui/{ => weather}/WeatherActivity.kt | 30 ++-- app/src/main/res/drawable/bg_weather_item.xml | 6 - .../main/res/drawable/divider_plan_detail.xml | 6 - app/src/main/res/layout/activity_area.xml | 5 + app/src/main/res/layout/activity_main.xml | 18 +-- app/src/main/res/layout/activity_weather.xml | 15 +- .../{activity_map.xml => fragment_area.xml} | 4 +- .../layout/{item_place.xml => item_area.xml} | 4 +- .../main/res/layout/item_daily_forecast.xml | 71 +++++----- .../reactivehttpsamples/ExampleUnitTest.kt | 16 --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- reactivehttp/build.gradle | 8 +- .../reactivehttp/base/ReactiveFragment.kt | 6 +- 34 files changed, 541 insertions(+), 354 deletions(-) delete mode 100644 app/src/androidTest/java/github/leavesczy/reactivehttpsamples/ExampleInstrumentedTest.kt rename app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/{PlaceAdapter.kt => AreaAdapter.kt} (87%) create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/base/BaseFragment.kt delete mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/MapViewModel.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CityViewModel.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CountyViewModel.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/ProvinceViewModel.kt rename app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/{ => weather}/WeatherViewModel.kt (90%) delete mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/ui/MapActivity.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/AreaActivity.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CityFragment.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CountyFragment.kt create mode 100644 app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/ProvinceFragment.kt rename app/src/main/java/github/leavesczy/reactivehttpsamples/ui/{ => weather}/WeatherActivity.kt (69%) delete mode 100644 app/src/main/res/drawable/bg_weather_item.xml delete mode 100644 app/src/main/res/drawable/divider_plan_detail.xml create mode 100644 app/src/main/res/layout/activity_area.xml rename app/src/main/res/layout/{activity_map.xml => fragment_area.xml} (93%) rename app/src/main/res/layout/{item_place.xml => item_area.xml} (88%) delete mode 100644 app/src/test/java/github/leavesczy/reactivehttpsamples/ExampleUnitTest.kt diff --git a/app/build.gradle b/app/build.gradle index 50763f5..3049f55 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { } signingConfigs { releaseConfig { - storeFile file("..\\key.jks") + storeFile file(rootDir.absolutePath + File.separator + "key.jks") storePassword "123456" keyAlias "leavesCZY" keyPassword "123456" @@ -43,20 +43,18 @@ android { } dependencies { - testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test:runner:1.4.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'androidx.appcompat:appcompat:1.4.1' + implementation "androidx.fragment:fragment-ktx:1.4.1" + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' implementation 'androidx.core:core-ktx:1.7.0' - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0' - implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.recyclerview:recyclerview:1.2.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.2' - implementation 'com.google.android.material:material:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'com.google.android.material:material:1.5.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1' debugImplementation 'com.github.leavesCZY.Monitor:monitor:1.1.5' releaseImplementation 'com.github.leavesCZY.Monitor:monitor-no-op:1.1.5' implementation project(path: ':reactivehttp') diff --git a/app/src/androidTest/java/github/leavesczy/reactivehttpsamples/ExampleInstrumentedTest.kt b/app/src/androidTest/java/github/leavesczy/reactivehttpsamples/ExampleInstrumentedTest.kt deleted file mode 100644 index 07e1f44..0000000 --- a/app/src/androidTest/java/github/leavesczy/reactivehttpsamples/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,22 +0,0 @@ -package github.leavesczy.reactivehttpsamples - -import androidx.test.InstrumentationRegistry -import androidx.test.runner.AndroidJUnit4 -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getTargetContext() - assertEquals("github.leavesczy.reactivehttpsamples", appContext.packageName) - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e88b366..81c015c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,9 +15,11 @@ android:supportsRtl="true" android:theme="@style/ReactiveHttpAppTheme" android:usesCleartextTraffic="true" - tools:ignore="GoogleAppIndexingWarning"> + tools:ignore="GoogleAppIndexingWarning" + tools:targetApi="m"> @@ -26,18 +28,22 @@ diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/MainActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/MainActivity.kt index 98dd2d1..2ebb1da 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/MainActivity.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/MainActivity.kt @@ -3,9 +3,9 @@ package github.leavesczy.reactivehttpsamples import android.os.Bundle import github.leavesczy.reactivehttpsamples.base.BaseActivity import github.leavesczy.reactivehttpsamples.databinding.ActivityMainBinding -import github.leavesczy.reactivehttpsamples.ui.MapActivity import github.leavesczy.reactivehttpsamples.ui.SingleRequestActivity import github.leavesczy.reactivehttpsamples.ui.TogetherRequestActivity +import github.leavesczy.reactivehttpsamples.ui.weather.AreaActivity /** * @Author: leavesCZY @@ -19,15 +19,15 @@ class MainActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - bind.btnWeather.setOnClickListener { - startActivity() - } bind.btnSingle.setOnClickListener { startActivity() } bind.btnTogether.setOnClickListener { startActivity() } + bind.btnWeather.setOnClickListener { + startActivity() + } } } \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/PlaceAdapter.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/AreaAdapter.kt similarity index 87% rename from app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/PlaceAdapter.kt rename to app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/AreaAdapter.kt index 921718e..d544966 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/PlaceAdapter.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/AreaAdapter.kt @@ -14,11 +14,11 @@ import github.leavesczy.reactivehttpsamples.core.mode.DistrictMode * @Desc: * @Github:https://github.com/leavesCZY */ -class PlaceAdapter( +class AreaAdapter( private val dataList: List, private val onClickListener: OnClickListener ) : - RecyclerView.Adapter() { + RecyclerView.Adapter() { interface OnClickListener { fun onClick(position: Int) @@ -26,7 +26,7 @@ class PlaceAdapter( override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlaceViewHolder { return PlaceViewHolder( - LayoutInflater.from(parent.context).inflate(R.layout.item_place, parent, false) + LayoutInflater.from(parent.context).inflate(R.layout.item_area, parent, false) ) } @@ -35,13 +35,13 @@ class PlaceAdapter( override fun onBindViewHolder(holder: PlaceViewHolder, position: Int) { holder.tvPlaceName.text = dataList[position].name holder.tvPlaceName.setOnClickListener { - onClickListener.onClick(holder.adapterPosition) + onClickListener.onClick(holder.bindingAdapterPosition) } } class PlaceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - var tvPlaceName: TextView = itemView.findViewById(R.id.tvPlaceName) + var tvPlaceName: TextView = itemView.findViewById(R.id.tvAreaName) } diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/DailyForecastAdapter.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/DailyForecastAdapter.kt index 708cf8d..b4fbeef 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/DailyForecastAdapter.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/adapter/DailyForecastAdapter.kt @@ -1,5 +1,6 @@ package github.leavesczy.reactivehttpsamples.adapter +import android.annotation.SuppressLint import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -31,6 +32,7 @@ class WeatherAdapter(private val districtsModeList: List) : return districtsModeList.size } + @SuppressLint("SetTextI18n") override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) { val bean = districtsModeList[position] holder.tvDate.text = bean.date diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/base/BaseFragment.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/base/BaseFragment.kt new file mode 100644 index 0000000..affb726 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/base/BaseFragment.kt @@ -0,0 +1,11 @@ +package github.leavesczy.reactivehttpsamples.base + +import github.leavesczy.reactivehttp.base.ReactiveFragment + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 20:38 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +open class BaseFragment : ReactiveFragment() \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/http/AppRemoteDataSource.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/http/AppRemoteDataSource.kt index d330350..d7501a3 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/http/AppRemoteDataSource.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/http/AppRemoteDataSource.kt @@ -29,9 +29,9 @@ class AppRemoteDataSource(iuiAction: IUIAction?) : RemoteExtendDataSource() - - init { - stateLiveData.value = TYPE_PROVINCE - } - - private val provinceLiveData = MutableLiveData>() - - private val cityLiveData = MutableLiveData>() - - val realLiveData = MutableLiveData>() - - val adCodeSelectedLiveData = MutableLiveData() - - fun getProvince() { - remoteDataSource.enqueueLoading({ - //主动延迟一段时间,避免弹窗太快消失 - delay(1200) - getProvince() - }) { - onSuccess { - log("onSuccess") - stateLiveData.value = TYPE_PROVINCE - provinceLiveData.value = it[0].districts - realLiveData.value = it[0].districts - } - } - } - - private fun getCity(province: String) { - remoteDataSource.enqueueLoading({ - getCity(province) - }) { - onSuccess { - stateLiveData.value = TYPE_CITY - cityLiveData.value = it[0].districts - realLiveData.value = it[0].districts - } - } - } - - private fun getCounty(city: String) { - remoteDataSource.enqueueLoading({ - getCounty(city) - }) { - onSuccess { - val districts = it[0].districts - if (districts.isNullOrEmpty()) { - adCodeSelectedLiveData.value = city - } else { - stateLiveData.value = TYPE_COUNTY - realLiveData.value = it[0].districts - } - } - } - } - - fun onBackPressed(): Boolean { - when (stateLiveData.value) { - TYPE_PROVINCE -> { - return true - } - TYPE_CITY -> { - stateLiveData.value = TYPE_PROVINCE - realLiveData.value = provinceLiveData.value - } - TYPE_COUNTY -> { - stateLiveData.value = TYPE_CITY - realLiveData.value = cityLiveData.value - } - } - return false - } - - fun onPlaceClicked(position: Int) { - when (stateLiveData.value) { - TYPE_PROVINCE -> { - realLiveData.value?.get(position)?.adcode?.let { - getCity(it) - } - } - TYPE_CITY -> { - realLiveData.value?.get(position)?.adcode?.let { - getCounty(it) - } - } - TYPE_COUNTY -> { - adCodeSelectedLiveData.value = realLiveData.value?.get(position)?.adcode - } - } - } - - private var log = "" - - @Synchronized - private fun log(msg: String) { - val newLog = "[${Thread.currentThread().name}]-${msg}" - log = log + "\n" + newLog - Log.e("TAG", newLog) - } - -} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CityViewModel.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CityViewModel.kt new file mode 100644 index 0000000..9aa2331 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CityViewModel.kt @@ -0,0 +1,30 @@ +package github.leavesczy.reactivehttpsamples.core.viewmodel.weather + +import androidx.lifecycle.MutableLiveData +import github.leavesczy.reactivehttpsamples.base.BaseViewModel +import github.leavesczy.reactivehttpsamples.core.mode.DistrictMode +import kotlinx.coroutines.delay + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:07 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class CityViewModel(private val province: String) : BaseViewModel() { + + val cityLiveData = MutableLiveData>() + + fun getCity() { + remoteDataSource.enqueueLoading({ + //主动延迟一段时间,避免弹窗太快消失 + delay(1200) + getCity(province) + }) { + onSuccess { + cityLiveData.value = it[0].districts + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CountyViewModel.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CountyViewModel.kt new file mode 100644 index 0000000..098ca47 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/CountyViewModel.kt @@ -0,0 +1,31 @@ +package github.leavesczy.reactivehttpsamples.core.viewmodel.weather + +import androidx.lifecycle.MutableLiveData +import github.leavesczy.reactivehttpsamples.base.BaseViewModel +import github.leavesczy.reactivehttpsamples.core.mode.DistrictMode +import kotlinx.coroutines.delay + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:07 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class CountyViewModel(private val city: String) : BaseViewModel() { + + val countyLiveData = MutableLiveData>() + + fun getCounty() { + remoteDataSource.enqueueLoading({ + //主动延迟一段时间,避免弹窗太快消失 + delay(1200) + getCounty(city) + }) { + onSuccess { + val districts = it[0].districts + countyLiveData.value = districts + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/ProvinceViewModel.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/ProvinceViewModel.kt new file mode 100644 index 0000000..90667b3 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/ProvinceViewModel.kt @@ -0,0 +1,30 @@ +package github.leavesczy.reactivehttpsamples.core.viewmodel.weather + +import androidx.lifecycle.MutableLiveData +import github.leavesczy.reactivehttpsamples.base.BaseViewModel +import github.leavesczy.reactivehttpsamples.core.mode.DistrictMode +import kotlinx.coroutines.delay + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:06 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class ProvinceViewModel : BaseViewModel() { + + val provinceLiveData = MutableLiveData>() + + fun getProvince() { + remoteDataSource.enqueueLoading({ + //主动延迟一段时间,避免弹窗太快消失 + delay(1200) + getProvince() + }) { + onSuccess { + provinceLiveData.value = it[0].districts + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/WeatherViewModel.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/WeatherViewModel.kt similarity index 90% rename from app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/WeatherViewModel.kt rename to app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/WeatherViewModel.kt index ad39e11..39666fe 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/WeatherViewModel.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/core/viewmodel/weather/WeatherViewModel.kt @@ -1,4 +1,4 @@ -package github.leavesczy.reactivehttpsamples.core.viewmodel +package github.leavesczy.reactivehttpsamples.core.viewmodel.weather import androidx.lifecycle.MutableLiveData import github.leavesczy.reactivehttpsamples.base.BaseViewModel diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/MapActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/MapActivity.kt deleted file mode 100644 index b1581a1..0000000 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/MapActivity.kt +++ /dev/null @@ -1,71 +0,0 @@ -package github.leavesczy.reactivehttpsamples.ui - -import android.content.Intent -import android.os.Bundle -import androidx.recyclerview.widget.LinearLayoutManager -import github.leavesczy.reactivehttpsamples.adapter.PlaceAdapter -import github.leavesczy.reactivehttpsamples.base.BaseActivity -import github.leavesczy.reactivehttpsamples.core.mode.DistrictMode -import github.leavesczy.reactivehttpsamples.core.viewmodel.MapViewModel -import github.leavesczy.reactivehttpsamples.databinding.ActivityMapBinding - -/** - * @Author: leavesCZY - * @Date: 2020/10/26 15:31 - * @Desc: - * @Github:https://github.com/leavesCZY - */ -class MapActivity : BaseActivity() { - - override val bind by getBind() - - private val dataList = mutableListOf() - - private val mapViewModel by getViewModel { - stateLiveData.observe(this@MapActivity, { - when (it) { - MapViewModel.TYPE_PROVINCE -> { - bind.tvTopBarTitle.text = "省份" - } - MapViewModel.TYPE_CITY -> { - bind.tvTopBarTitle.text = "城市" - } - MapViewModel.TYPE_COUNTY -> { - bind.tvTopBarTitle.text = "区县" - } - } - }) - realLiveData.observe(it, { - dataList.clear() - dataList.addAll(it) - placeAdapter.notifyDataSetChanged() - }) - adCodeSelectedLiveData.observe(it, { - val intent = Intent(this@MapActivity, WeatherActivity::class.java) - intent.putExtra("adCode", it) - startActivity(intent) - finish() - }) - } - - private val placeAdapter: PlaceAdapter = - PlaceAdapter(dataList, object : PlaceAdapter.OnClickListener { - override fun onClick(position: Int) { - mapViewModel.onPlaceClicked(position) - } - }) - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - bind.rvPlaceList.layoutManager = LinearLayoutManager(this) - bind.rvPlaceList.adapter = placeAdapter - mapViewModel.getProvince() - } - - override fun onBackPressed() { - if (mapViewModel.onBackPressed()) { - super.onBackPressed() - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/SingleRequestActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/SingleRequestActivity.kt index 632847e..10be959 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/SingleRequestActivity.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/SingleRequestActivity.kt @@ -16,9 +16,9 @@ class SingleRequestActivity : BaseActivity() { override val bind by getBind() private val singleRequestViewModel by getViewModel { - logLiveData.observe(this@SingleRequestActivity, { + logLiveData.observe(this@SingleRequestActivity) { bind.tvLog.text = it - }) + } } override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/TogetherRequestActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/TogetherRequestActivity.kt index 59290a2..bde3ddd 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/TogetherRequestActivity.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/TogetherRequestActivity.kt @@ -16,9 +16,9 @@ class TogetherRequestActivity : BaseActivity() { override val bind by getBind() private val togetherRequestViewModel by getViewModel { - logLiveData.observe(this@TogetherRequestActivity, { + logLiveData.observe(this@TogetherRequestActivity) { bind.tvLog.text = it - }) + } } override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/AreaActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/AreaActivity.kt new file mode 100644 index 0000000..a881db8 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/AreaActivity.kt @@ -0,0 +1,78 @@ +package github.leavesczy.reactivehttpsamples.ui.weather + +import android.os.Bundle +import androidx.fragment.app.commit +import github.leavesczy.reactivehttpsamples.R +import github.leavesczy.reactivehttpsamples.base.BaseActivity +import github.leavesczy.reactivehttpsamples.databinding.ActivityAreaBinding + +/** + * @Author: leavesCZY + * @Date: 2020/10/26 15:31 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class AreaActivity : BaseActivity() { + + companion object { + + const val onClickProvinceKey = "onClickProvinceKey" + + const val onClickCityKey = "onClickCityKey" + + const val onClickCountyKey = "onClickCountyKey" + + const val parameterKey = "parameterKey" + + } + + override val bind by getBind() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportFragmentManager.setFragmentResultListener( + onClickProvinceKey, + this + ) { _, result -> + showCityList(result.getString(parameterKey, "")) + } + supportFragmentManager.setFragmentResultListener( + onClickCityKey, + this + ) { _, result -> + showCountyList(result.getString(parameterKey, "")) + } + supportFragmentManager.setFragmentResultListener( + onClickCountyKey, + this + ) { _, result -> + WeatherActivity.navTo(this, result.getString(parameterKey, "")) + finish() + } + showProvinceList() + } + + private fun showProvinceList() { + supportFragmentManager.commit { + add(R.id.fragmentContainerView, ProvinceFragment()) + setReorderingAllowed(true) + } + } + + private fun showCityList(province: String) { + supportFragmentManager.commit { + add(R.id.fragmentContainerView, CityFragment.newInstance(province)) + setReorderingAllowed(true) + addToBackStack(null) + } + } + + private fun showCountyList(city: String) { + supportFragmentManager.commit { + add(R.id.fragmentContainerView, CountyFragment.newInstance(city)) + setReorderingAllowed(true) + addToBackStack(null) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CityFragment.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CityFragment.kt new file mode 100644 index 0000000..e37f24b --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CityFragment.kt @@ -0,0 +1,84 @@ +package github.leavesczy.reactivehttpsamples.ui.weather + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager +import github.leavesczy.reactivehttpsamples.adapter.AreaAdapter +import github.leavesczy.reactivehttpsamples.base.BaseFragment +import github.leavesczy.reactivehttpsamples.core.viewmodel.weather.CityViewModel +import github.leavesczy.reactivehttpsamples.databinding.FragmentAreaBinding + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:08 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class CityFragment : BaseFragment() { + + companion object { + + private const val keyRequestParameter = "keyRequestParameter" + + fun newInstance(province: String): Fragment { + val fragment = CityFragment() + val bundle = Bundle() + bundle.putString(keyRequestParameter, province) + fragment.arguments = bundle + return fragment + } + + } + + private var bind: FragmentAreaBinding? = null + + private val mBind: FragmentAreaBinding + get() = bind!! + + private val province by lazy { + requireArguments().getString(keyRequestParameter) ?: throw IllegalArgumentException() + } + + private val cityViewModel by getViewModelInstance( + create = { + CityViewModel(province) + } + ) { + cityLiveData.observe(viewLifecycleOwner) { + val list = it + val placeAdapter = AreaAdapter(list, object : AreaAdapter.OnClickListener { + override fun onClick(position: Int) { + val bundle = Bundle() + bundle.putString(AreaActivity.parameterKey, list[position].adcode) + parentFragmentManager.setFragmentResult(AreaActivity.onClickCityKey, bundle) + } + }) + mBind.rvPlaceList.adapter = placeAdapter + } + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + bind = FragmentAreaBinding.inflate(inflater, container, false) + return mBind.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mBind.rvPlaceList.layoutManager = LinearLayoutManager(requireActivity()) + mBind.tvTopBarTitle.text = "城市" + cityViewModel.getCity() + } + + override fun onDestroyView() { + super.onDestroyView() + bind = null + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CountyFragment.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CountyFragment.kt new file mode 100644 index 0000000..9d5db5a --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/CountyFragment.kt @@ -0,0 +1,90 @@ +package github.leavesczy.reactivehttpsamples.ui.weather + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager +import github.leavesczy.reactivehttpsamples.adapter.AreaAdapter +import github.leavesczy.reactivehttpsamples.base.BaseFragment +import github.leavesczy.reactivehttpsamples.core.viewmodel.weather.CountyViewModel +import github.leavesczy.reactivehttpsamples.databinding.FragmentAreaBinding + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:08 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class CountyFragment : BaseFragment() { + + companion object { + + private const val keyRequestParameter = "keyRequestParameter" + + fun newInstance(city: String): Fragment { + val fragment = CountyFragment() + val bundle = Bundle() + bundle.putString(keyRequestParameter, city) + fragment.arguments = bundle + return fragment + } + + } + + private var bind: FragmentAreaBinding? = null + + private val mBind: FragmentAreaBinding + get() = bind!! + + private val city by lazy { + requireArguments().getString(keyRequestParameter) ?: throw IllegalArgumentException() + } + + private val countyViewModel by getViewModelInstance(create = { + CountyViewModel(city) + }) { + countyLiveData.observe(viewLifecycleOwner) { + val list = it + if (list.isEmpty()) { + onClickCounty(city) + } else { + val placeAdapter = AreaAdapter(it, object : AreaAdapter.OnClickListener { + override fun onClick(position: Int) { + onClickCounty(list[position].adcode) + } + }) + mBind.rvPlaceList.adapter = placeAdapter + } + } + } + + private fun onClickCounty(adCode: String) { + val bundle = Bundle() + bundle.putString(AreaActivity.parameterKey, adCode) + parentFragmentManager.setFragmentResult(AreaActivity.onClickCountyKey, bundle) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + bind = FragmentAreaBinding.inflate(inflater, container, false) + return mBind.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mBind.rvPlaceList.layoutManager = LinearLayoutManager(requireActivity()) + mBind.tvTopBarTitle.text = "区县" + countyViewModel.getCounty() + } + + override fun onDestroyView() { + super.onDestroyView() + bind = null + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/ProvinceFragment.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/ProvinceFragment.kt new file mode 100644 index 0000000..9dac962 --- /dev/null +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/ProvinceFragment.kt @@ -0,0 +1,61 @@ +package github.leavesczy.reactivehttpsamples.ui.weather + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.LinearLayoutManager +import github.leavesczy.reactivehttpsamples.adapter.AreaAdapter +import github.leavesczy.reactivehttpsamples.base.BaseFragment +import github.leavesczy.reactivehttpsamples.core.viewmodel.weather.ProvinceViewModel +import github.leavesczy.reactivehttpsamples.databinding.FragmentAreaBinding + +/** + * @Author: leavesCZY + * @Date: 2022/4/23 21:07 + * @Desc: + * @Github:https://github.com/leavesCZY + */ +class ProvinceFragment : BaseFragment() { + + private var bind: FragmentAreaBinding? = null + + private val mBind: FragmentAreaBinding + get() = bind!! + + private val provinceViewModel by getViewModel { + provinceLiveData.observe(viewLifecycleOwner) { + val list = it + val placeAdapter = AreaAdapter(list, object : AreaAdapter.OnClickListener { + override fun onClick(position: Int) { + val bundle = Bundle() + bundle.putString(AreaActivity.parameterKey, list[position].adcode) + parentFragmentManager.setFragmentResult(AreaActivity.onClickProvinceKey, bundle) + } + }) + mBind.rvPlaceList.adapter = placeAdapter + } + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + bind = FragmentAreaBinding.inflate(inflater, container, false) + return mBind.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mBind.rvPlaceList.layoutManager = LinearLayoutManager(requireActivity()) + mBind.tvTopBarTitle.text = "省份" + provinceViewModel.getProvince() + } + + override fun onDestroyView() { + super.onDestroyView() + bind = null + } + +} \ No newline at end of file diff --git a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/WeatherActivity.kt b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/WeatherActivity.kt similarity index 69% rename from app/src/main/java/github/leavesczy/reactivehttpsamples/ui/WeatherActivity.kt rename to app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/WeatherActivity.kt index 5677fe5..ca0e337 100644 --- a/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/WeatherActivity.kt +++ b/app/src/main/java/github/leavesczy/reactivehttpsamples/ui/weather/WeatherActivity.kt @@ -1,12 +1,15 @@ -package github.leavesczy.reactivehttpsamples.ui +package github.leavesczy.reactivehttpsamples.ui.weather +import android.annotation.SuppressLint +import android.content.Context +import android.content.Intent import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import github.leavesczy.reactivehttpsamples.adapter.WeatherAdapter import github.leavesczy.reactivehttpsamples.base.BaseActivity import github.leavesczy.reactivehttpsamples.core.mode.CastsMode import github.leavesczy.reactivehttpsamples.core.mode.ForecastsMode -import github.leavesczy.reactivehttpsamples.core.viewmodel.WeatherViewModel +import github.leavesczy.reactivehttpsamples.core.viewmodel.weather.WeatherViewModel import github.leavesczy.reactivehttpsamples.databinding.ActivityWeatherBinding /** @@ -17,12 +20,24 @@ import github.leavesczy.reactivehttpsamples.databinding.ActivityWeatherBinding */ class WeatherActivity : BaseActivity() { + companion object { + + private const val adCodeKey = "adCodeKey" + + fun navTo(context: Context, adCode: String) { + val intent = Intent(context, WeatherActivity::class.java) + intent.putExtra(adCodeKey, adCode) + context.startActivity(intent) + } + + } + override val bind by getBind() private val weatherViewModel by getViewModel { - forecastsModeLiveData.observe(this@WeatherActivity, { + forecastsModeLiveData.observe(this@WeatherActivity) { showWeather(it) - }) + } } private val castsBeanList = mutableListOf() @@ -30,7 +45,7 @@ class WeatherActivity : BaseActivity() { private val weatherAdapter = WeatherAdapter(castsBeanList) private val adCode by lazy { - intent.getStringExtra("adCode") ?: "" + intent.getStringExtra(adCodeKey) ?: "" } override fun onCreate(savedInstanceState: Bundle?) { @@ -40,13 +55,10 @@ class WeatherActivity : BaseActivity() { bind.swipeRefreshLayout.setOnRefreshListener { weatherViewModel.getWeather(adCode) } - bind.ivPlace.setOnClickListener { - startActivity() - finish() - } weatherViewModel.getWeather(adCode) } + @SuppressLint("NotifyDataSetChanged") private fun showWeather(forecastsMode: ForecastsMode) { bind.tvCity.text = forecastsMode.city castsBeanList.clear() diff --git a/app/src/main/res/drawable/bg_weather_item.xml b/app/src/main/res/drawable/bg_weather_item.xml deleted file mode 100644 index 6ac4017..0000000 --- a/app/src/main/res/drawable/bg_weather_item.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/drawable/divider_plan_detail.xml b/app/src/main/res/drawable/divider_plan_detail.xml deleted file mode 100644 index 506b9d9..0000000 --- a/app/src/main/res/drawable/divider_plan_detail.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_area.xml b/app/src/main/res/layout/activity_area.xml new file mode 100644 index 0000000..71c75f4 --- /dev/null +++ b/app/src/main/res/layout/activity_area.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8ef18ed..4f84db6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,27 +4,27 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" + android:padding="20dp" tools:context=".MainActivity">