├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── aranandroid │ │ └── mvvm │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── litepal.xml │ ├── java │ │ └── com │ │ │ └── aranandroid │ │ │ └── mvvm │ │ │ ├── BaseApplication.kt │ │ │ ├── data │ │ │ ├── PlaceRepository.kt │ │ │ ├── Resource.kt │ │ │ ├── WeatherRepository.kt │ │ │ ├── db │ │ │ │ ├── ExampleDatabase.kt │ │ │ │ ├── PlaceDao.kt │ │ │ │ └── WeatherDao.kt │ │ │ ├── model │ │ │ │ ├── place │ │ │ │ │ ├── City.kt │ │ │ │ │ ├── County.kt │ │ │ │ │ └── Province.kt │ │ │ │ └── weather │ │ │ │ │ ├── AQI.kt │ │ │ │ │ ├── Basic.kt │ │ │ │ │ ├── Forecast.kt │ │ │ │ │ ├── HeWeather.kt │ │ │ │ │ ├── Now.kt │ │ │ │ │ ├── Suggestion.kt │ │ │ │ │ └── Weather.kt │ │ │ └── network │ │ │ │ ├── ExampleNetwork.kt │ │ │ │ └── api │ │ │ │ ├── PlaceService.kt │ │ │ │ └── WeatherService.kt │ │ │ ├── ui │ │ │ ├── MainActivity.kt │ │ │ ├── MainViewModel.kt │ │ │ ├── area │ │ │ │ ├── ChooseAreaAdapter.kt │ │ │ │ ├── ChooseAreaFragment.kt │ │ │ │ └── ChooseAreaViewModel.kt │ │ │ └── weather │ │ │ │ ├── WeatherActivity.kt │ │ │ │ └── WeatherViewModel.kt │ │ │ └── util │ │ │ └── BindingAdapters.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable-xxhdpi │ │ ├── ic_back.png │ │ └── ic_home.png │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_weather.xml │ │ ├── aqi.xml │ │ ├── choose_area.xml │ │ ├── forecast.xml │ │ ├── forecast_item.xml │ │ ├── now.xml │ │ ├── simple_item.xml │ │ ├── suggestion.xml │ │ └── title.xml │ │ ├── mipmap-hdpi │ │ └── logo.png │ │ ├── mipmap-mdpi │ │ └── logo.png │ │ ├── mipmap-xhdpi │ │ └── logo.png │ │ ├── mipmap-xxhdpi │ │ └── logo.png │ │ ├── mipmap-xxxhdpi │ │ └── logo.png │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── network_security_config.xml │ └── test │ └── java │ └── com │ └── aranandroid │ └── mvvm │ └── ExampleUnitTest.kt ├── build.gradle ├── frist.patch ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── images ├── Screenshot_1.png ├── Screenshot_2.png └── architecture.jpg ├── mvvm ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── aranandroid │ │ └── mvvm │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── aranandroid │ │ │ └── mvvm │ │ │ ├── MvvmApplication.kt │ │ │ ├── base │ │ │ ├── factory │ │ │ │ └── BaseRepository.kt │ │ │ ├── m │ │ │ │ └── BaseViewModel.kt │ │ │ └── vm │ │ │ │ ├── BaseActivity.kt │ │ │ │ ├── BaseFragment.kt │ │ │ │ └── BaseFragmentActivity.kt │ │ │ ├── live │ │ │ ├── AliveManager.kt │ │ │ ├── AliveReceiver.kt │ │ │ ├── AliveScreenActivity.kt │ │ │ ├── EmptyActivity.kt │ │ │ └── ForgroundService.kt │ │ │ ├── network │ │ │ ├── BaseNetwork.kt │ │ │ └── ServiceCreator.kt │ │ │ ├── permission │ │ │ └── PermissonDialog.kt │ │ │ └── util │ │ │ ├── ActivityLifeCycleCallBackIml.java │ │ │ ├── AppManager.java │ │ │ └── BaseExecutors.kt │ └── res │ │ ├── anim │ │ ├── slide_in_left.xml │ │ ├── slide_in_right.xml │ │ ├── slide_in_zoom.xml │ │ ├── slide_out_left.xml │ │ ├── slide_out_right.xml │ │ └── slide_out_zoom.xml │ │ ├── drawable │ │ ├── custom_dialog_bg.xml │ │ ├── negative_button_bg.xml │ │ └── positive_button_bg.xml │ │ ├── layout │ │ ├── custom_dialog_layout.xml │ │ └── permissions_item.xml │ │ ├── mipmap │ │ └── ic_launcher.png │ │ └── values │ │ ├── styles.xml │ │ └── themes.xml │ └── test │ └── java │ └── com │ └── aranandroid │ └── mvvm │ └── ExampleUnitTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | 67 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 酷欧天气Jetpack版 2 | 酷欧天气的Jetpack版本实现,采用了MVVM架构。 3 | 4 | ## mvvm使用(mvvm被封装成框架包) 5 | 6 | *build.gradle(主项目): 7 | 8 | maven { url 'https://jitpack.io' } 9 | 10 | *build.gradle(app): 11 | 12 | implementation 'com.github.AranAndroid009:Mvvm:1.0.1' 13 | 14 | *网络初始化用: 15 | 16 | 继承 MvvmApplication 然后 ServiceCreator.init("http://guolin.tech/") 17 | 18 | *数据库使用: 19 | 20 | assets文件下创建xml 21 | 22 | 案例: 23 | ``` 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | ``` 33 | #mvvm三部曲: 34 | 35 | *m: 36 | 37 | class ExampleModel : BaseViewModel() 38 | 39 | *v: 40 | ``` 41 | 43 | 44 | 45 | 46 | 47 | 50 | 51 | 52 | ``` 53 | *mv: 54 | ``` 55 | class ExampleActivity : 56 | BaseActivity(R.layout.activity_example) { 57 | override fun onCreate(savedInstanceState: Bundle?) { 58 | super.onCreate(savedInstanceState) 59 | // 必写 60 | binding.viewModel = viewModel 61 | // 必写 62 | binding.lifecycleOwner = this 63 | } 64 | } 65 | 66 | 注意: 67 | app的build.gradle中android {}里边 68 | dataBinding { 69 | enabled true 70 | } 71 | ``` 72 | 73 | 这里我先给出一张酷欧天气Jetpack版的架构设计图,这张图是模仿Google Codelabs的Sunshine项目画出来的。拥有良好架构设计的项目都是可以用简洁清晰的架构图表示出来的,而一个杂乱无章没有架构设计的项目则很难用架构图表示出来。 74 | 75 | 76 | 77 | 上述架构图可能一开始看你会找不着重点,其实这张架构图非常清晰,我来带大家解读一下。 78 | 79 | 首先我们通过这张架构图成功将程序分为了若干层。 80 | 81 | 绿色部分表示的是UI控制层,这部分就是我们平时写的Activity和Fragment。 82 | 83 | 蓝色部分表示的是ViewModel层,ViewModel用于持有和UI元素相关的数据,以保证这些数据在屏幕旋转时不会丢失,以及负责和仓库之间进行通讯。 84 | 85 | 黄色部分表示的是仓库层,仓库层要做的工作是自主判断接口请求的数据应该是从数据库中读取还是从网络中获取,并将数据返回给调用方。如果是从网络中获取的话还要将这些数据存入到数据库当中,以避免下次重复从网络中获取。简而言之,仓库的工作就是在本地和网络数据之间做一个分配和调度的工作,调用方不管你的数据是从何而来的,我只是要从你仓库这里获取数据而已,而仓库则要自主分配如何更好更快地将数据提供给调用方。 86 | 87 | 接下来灰色部分表示是的本地数据层,实现方式并不固定,我使用了LitePal来进行数据持久化处理,你也可以使用别的框架。 88 | 89 | 最后红色部分表示的是网络数据层,这里使用了Retrofit从web服务接口获取数据。 90 | 91 | 借助这张架构图,我想会在很大程度上便于大家理解酷欧天气Jetpack版这个开源项目,而如果你自己编写的项目也能尝试画出这样一张架构图,那么你的代码结构一定是非常不错的。 92 | 93 | 另外对于这张架构图我还有必要再解释一下,图中所有的箭头都是单向的,比方说WeatherActivity指向了WeatherViewModel,表示WeatherActivity持有WeatherViewModel的引用,但是反过来WeatherViewModel不能持有WeatherActivity的引用。其他的几层也是一样的道理,一个箭头就表示持有一个引用。 94 | 95 | 还有,引用不能跨层持有,就比方说UI控制层不能持有仓库层的引用,每一层的组件都只能和它的相邻层交互。 96 | 97 | 大概就介绍这么多吧,剩下的就靠大家自己去阅读源码进行学习了。 98 | 99 | 项目运行截图如下: 100 | 101 |              102 | 103 | 最后,希望这个项目能够帮助大家更好地学习Jetpack,更好地学习MVVM架构。 104 | 105 | 106 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | apply plugin: 'kotlin-kapt' 8 | 9 | android { 10 | compileSdkVersion 28 11 | defaultConfig { 12 | applicationId "com.aranandroid.mvvm" 13 | minSdkVersion 21 14 | targetSdkVersion 28 15 | versionCode 1 16 | versionName "1.0" 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | dataBinding { 26 | enabled true 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 33 | 34 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1' 35 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1' 36 | 37 | implementation 'androidx.appcompat:appcompat:1.1.0-alpha03' 38 | implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' 39 | implementation project(path: ':mvvm') 40 | implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' 41 | testImplementation 'junit:junit:4.12' 42 | androidTestImplementation 'androidx.test:runner:1.1.1' 43 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 44 | 45 | implementation 'com.github.bumptech.glide:glide:3.7.0' 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/aranandroid/mvvm/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.coolweather.coolweatherjetpack", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/assets/litepal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/BaseApplication.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm 2 | 3 | import android.annotation.SuppressLint 4 | import android.content.Context 5 | import com.aranandroid.mvvm.data.network.ServiceCreator 6 | 7 | class BaseApplication : MvvmApplication() { 8 | 9 | override fun onCreate() { 10 | super.onCreate() 11 | context = this 12 | // 初始化网络 13 | ServiceCreator.init("http://guolin.tech/") 14 | 15 | } 16 | 17 | companion object { 18 | @SuppressLint("StaticFieldLeak") 19 | lateinit var context: Context 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/PlaceRepository.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data 2 | 3 | import com.aranandroid.mvvm.data.db.PlaceDao 4 | import com.aranandroid.mvvm.data.network.ExampleNetwork 5 | import kotlinx.coroutines.Dispatchers 6 | import kotlinx.coroutines.withContext 7 | 8 | /** 9 | * 地方厂库 10 | * 先获取数据库数据 获取不到获取网络数据 11 | */ 12 | class PlaceRepository private constructor(private val placeDao: PlaceDao, private val network: ExampleNetwork) { 13 | 14 | suspend fun getProvinceList() = withContext(Dispatchers.IO) { 15 | var list = placeDao.getProvinceList() 16 | if (list.isEmpty()) { 17 | list = network.fetchProvinceList() 18 | placeDao.saveProvinceList(list) 19 | } 20 | list 21 | } 22 | 23 | suspend fun getCityList(provinceId: Int) = withContext(Dispatchers.IO) { 24 | var list = placeDao.getCityList(provinceId) 25 | if (list.isEmpty()) { 26 | list = network.fetchCityList(provinceId) 27 | list.forEach { it.provinceId = provinceId } 28 | placeDao.saveCityList(list) 29 | } 30 | list 31 | } 32 | 33 | suspend fun getCountyList(provinceId: Int, cityId: Int) = withContext(Dispatchers.IO) { 34 | var list = placeDao.getCountyList(cityId) 35 | if (list.isEmpty()) { 36 | list = network.fetchCountyList(provinceId, cityId) 37 | list.forEach { it.cityId = cityId } 38 | placeDao.saveCountyList(list) 39 | } 40 | list 41 | } 42 | 43 | companion object { 44 | 45 | private var instance: PlaceRepository? = null 46 | 47 | fun getInstance(placeDao: PlaceDao, network: ExampleNetwork): PlaceRepository { 48 | if (instance == null) { 49 | synchronized(PlaceRepository::class.java) { 50 | if (instance == null) { 51 | instance = PlaceRepository(placeDao, network) 52 | } 53 | } 54 | } 55 | return instance!! 56 | } 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/Resource.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 The Android Open Source Project 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 | * http://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.aranandroid.mvvm.data 18 | 19 | /** 20 | * A generic class that holds a value with its loading status. 21 | * @param 22 | */ 23 | data class Resource(val status: Int, val data: T?, val message: String?) { 24 | // 伴生类 静态变量使用 25 | companion object { 26 | const val SUCCESS = 0 27 | const val ERROR = 1 28 | const val LOADING = 2 29 | 30 | fun success(data: T?) = Resource(SUCCESS, data, null) 31 | 32 | fun error(msg: String, data: T?) = Resource(ERROR, data, msg) 33 | 34 | fun loading(data: T?) = Resource(LOADING, data, null) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/WeatherRepository.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data 2 | 3 | import com.aranandroid.mvvm.data.db.WeatherDao 4 | import com.aranandroid.mvvm.data.model.weather.Weather 5 | import com.aranandroid.mvvm.data.network.ExampleNetwork 6 | import com.aranandroid.mvvm.base.factory.BaseRepository 7 | import com.aranandroid.mvvm.ui.weather.WeatherViewModel 8 | import kotlinx.coroutines.Dispatchers 9 | import kotlinx.coroutines.withContext 10 | 11 | /** 12 | * 天气数据工厂类 13 | * 先获取数据库数据 获取不到获取网络数据 14 | */ 15 | class WeatherRepository private constructor(private val weatherDao: WeatherDao, private val network: ExampleNetwork): 16 | BaseRepository() { 17 | 18 | suspend fun getWeather(weatherId: String): Weather { 19 | var weather = weatherDao.getCachedWeatherInfo() 20 | if (weather == null) weather = requestWeather(weatherId) 21 | return weather 22 | } 23 | 24 | suspend fun refreshWeather(weatherId: String) = requestWeather(weatherId) 25 | 26 | suspend fun getBingPic(): String { 27 | var url = weatherDao.getCachedBingPic() 28 | if (url == null) url = requestBingPic() 29 | return url 30 | } 31 | // suspend 声明挂起函数 32 | suspend fun refreshBingPic() = requestBingPic() 33 | 34 | fun isWeatherCached() = weatherDao.getCachedWeatherInfo() != null 35 | 36 | fun getCachedWeather() = weatherDao.getCachedWeatherInfo()!! 37 | 38 | private suspend fun requestWeather(weatherId: String) = withContext(Dispatchers.IO) { 39 | val heWeather = network.fetchWeather(weatherId) 40 | val weather = heWeather.weather!![0] 41 | weatherDao.cacheWeatherInfo(weather) 42 | weather 43 | } 44 | 45 | 46 | /** 47 | * suspend 声明挂起函数 48 | * withContext(Dispatchers.IO) 切换耗时线程 49 | * 四类协程 50 | * runBlocking 阻塞 无返回值 51 | * launch 非阻塞 无返回值 52 | * withContext 串行 返回值 53 | * async 并行 返回值 54 | */ 55 | 56 | private suspend fun requestBingPic() = withContext(Dispatchers.IO) { 57 | val url = network.fetchBingPic() 58 | // 保存到数据库 59 | weatherDao.cacheBingPic(url) 60 | url 61 | } 62 | 63 | companion object { 64 | 65 | private lateinit var instance: WeatherRepository 66 | 67 | fun getInstance(weatherDao: WeatherDao, network: ExampleNetwork): WeatherRepository { 68 | if (!::instance.isInitialized) { 69 | synchronized(WeatherRepository::class.java) { 70 | if (!::instance.isInitialized) { 71 | instance = WeatherRepository(weatherDao, network) 72 | } 73 | } 74 | } 75 | return instance 76 | } 77 | 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/db/ExampleDatabase.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.db 2 | 3 | /** 4 | * 案例 5 | */ 6 | object ExampleDatabase { 7 | 8 | private var placeDao: PlaceDao? = null 9 | 10 | private var weatherDao: WeatherDao? = null 11 | 12 | fun getPlaceDao(): PlaceDao { 13 | if (placeDao == null) { 14 | placeDao = PlaceDao() 15 | } 16 | return placeDao!! 17 | } 18 | 19 | fun getWeatherDao(): WeatherDao { 20 | if (weatherDao == null) weatherDao = WeatherDao() 21 | return weatherDao!! 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/db/PlaceDao.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.db 2 | 3 | import com.aranandroid.mvvm.data.model.place.City 4 | import com.aranandroid.mvvm.data.model.place.County 5 | import com.aranandroid.mvvm.data.model.place.Province 6 | import org.litepal.LitePal 7 | 8 | class PlaceDao { 9 | 10 | fun getProvinceList(): MutableList = LitePal.findAll(Province::class.java) 11 | 12 | fun getCityList(provinceId: Int): MutableList = LitePal.where("provinceId = ?", provinceId.toString()).find(City::class.java) 13 | 14 | fun getCountyList(cityId: Int): MutableList = LitePal.where("cityId = ?", cityId.toString()).find(County::class.java) 15 | 16 | fun saveProvinceList(provinceList: List?) { 17 | if (provinceList != null && provinceList.isNotEmpty()) { 18 | LitePal.saveAll(provinceList) 19 | } 20 | } 21 | 22 | fun saveCityList(cityList: List?) { 23 | if (cityList != null && cityList.isNotEmpty()) { 24 | LitePal.saveAll(cityList) 25 | } 26 | } 27 | 28 | fun saveCountyList(countyList: List?) { 29 | if (countyList != null && countyList.isNotEmpty()) { 30 | LitePal.saveAll(countyList) 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/db/WeatherDao.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.db 2 | 3 | import android.content.SharedPreferences 4 | import android.preference.PreferenceManager 5 | import com.aranandroid.mvvm.BaseApplication 6 | import com.aranandroid.mvvm.data.model.weather.Weather 7 | import com.google.gson.Gson 8 | 9 | class WeatherDao { 10 | 11 | fun cacheWeatherInfo(weather: Weather?) { 12 | if (weather == null) return 13 | PreferenceManager.getDefaultSharedPreferences(BaseApplication.context).edit { 14 | val weatherInfo = Gson().toJson(weather) 15 | putString("weather", weatherInfo) 16 | } 17 | } 18 | 19 | fun getCachedWeatherInfo(): Weather? { 20 | val weatherInfo = PreferenceManager.getDefaultSharedPreferences(BaseApplication.context).getString("weather", null) 21 | if (weatherInfo != null) { 22 | return Gson().fromJson(weatherInfo, Weather::class.java) 23 | } 24 | return null 25 | } 26 | 27 | fun cacheBingPic(bingPic: String?) { 28 | if (bingPic == null) return 29 | // SharedPreferences 保存数据 30 | PreferenceManager.getDefaultSharedPreferences(BaseApplication.context).edit { 31 | putString("bing_pic", bingPic) 32 | } 33 | } 34 | 35 | fun getCachedBingPic(): String? = PreferenceManager.getDefaultSharedPreferences(BaseApplication.context).getString("bing_pic", null) 36 | 37 | private fun SharedPreferences.edit(action: SharedPreferences.Editor.() -> Unit) { 38 | val editor = edit() 39 | action(editor) 40 | editor.apply() 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/place/City.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.place 2 | 3 | import com.google.gson.annotations.SerializedName 4 | import org.litepal.crud.LitePalSupport 5 | 6 | class City ( @SerializedName("name") val cityName: String, @SerializedName("id") val cityCode: Int) : LitePalSupport() { 7 | @Transient val id = 0 8 | var provinceId = 0 9 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/place/County.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.place 2 | 3 | import com.google.gson.annotations.SerializedName 4 | import org.litepal.crud.LitePalSupport 5 | 6 | class County (@SerializedName("name") val countyName: String, @SerializedName("weather_id") val weatherId: String) : LitePalSupport() { 7 | @Transient val id = 0 8 | var cityId = 0 9 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/place/Province.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.place 2 | 3 | import com.google.gson.annotations.SerializedName 4 | import org.litepal.crud.LitePalSupport 5 | 6 | class Province (@SerializedName("name") val provinceName: String, @SerializedName("id") val provinceCode: Int) : LitePalSupport() { 7 | @Transient val id = 0 8 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/AQI.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | class AQI { 4 | lateinit var city: AQICity 5 | 6 | inner class AQICity { 7 | var aqi = "" 8 | var pm25 = "" 9 | } 10 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/Basic.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class Basic { 6 | @SerializedName("city") 7 | var cityName = "" 8 | @SerializedName("id") 9 | var weatherId = "" 10 | lateinit var update: Update 11 | 12 | inner class Update { 13 | @SerializedName("loc") 14 | var updateTime = "" 15 | 16 | fun time() = updateTime.split(" ")[1] 17 | } 18 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/Forecast.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class Forecast { 6 | var date: String = "" 7 | @SerializedName("tmp") 8 | lateinit var temperature: Temperature 9 | @SerializedName("cond") 10 | lateinit var more: More 11 | 12 | inner class Temperature { 13 | var max = "" 14 | var min = "" 15 | } 16 | 17 | inner class More { 18 | @SerializedName("txt_d") 19 | var info = "" 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/HeWeather.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class HeWeather { 6 | 7 | @SerializedName("HeWeather") 8 | var weather: List? = null 9 | 10 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/Now.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class Now { 6 | @SerializedName("tmp") 7 | var temperature = "" 8 | @SerializedName("cond") 9 | lateinit var more: More 10 | 11 | fun degree() = "$temperature℃" 12 | 13 | inner class More { 14 | @SerializedName("txt") 15 | var info = "" 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/Suggestion.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class Suggestion { 6 | @SerializedName("comf") 7 | lateinit var comfort: Comfort 8 | @SerializedName("cw") 9 | lateinit var carWash: CarWash 10 | lateinit var sport: Sport 11 | 12 | inner class Comfort { 13 | @SerializedName("txt") 14 | var info = "" 15 | } 16 | 17 | inner class CarWash { 18 | @SerializedName("txt") 19 | var info = "" 20 | } 21 | 22 | inner class Sport { 23 | @SerializedName("txt") 24 | var info = "" 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/model/weather/Weather.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.model.weather 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | class Weather { 6 | var status = "" 7 | lateinit var basic: Basic 8 | lateinit var aqi: AQI 9 | lateinit var now: Now 10 | lateinit var suggestion: Suggestion 11 | @SerializedName("daily_forecast") 12 | lateinit var forecastList: List 13 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/network/ExampleNetwork.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.network 2 | 3 | import com.aranandroid.mvvm.data.network.api.PlaceService 4 | import com.aranandroid.mvvm.data.network.api.WeatherService 5 | 6 | class ExampleNetwork : BaseNetwork() { 7 | 8 | private val placeService = ServiceCreator.create(PlaceService::class.java) 9 | 10 | private val weatherService = ServiceCreator.create(WeatherService::class.java) 11 | 12 | suspend fun fetchProvinceList() = placeService.getProvinces().await() 13 | 14 | suspend fun fetchCityList(provinceId: Int) = placeService.getCities(provinceId).await() 15 | 16 | suspend fun fetchCountyList(provinceId: Int, cityId: Int) = placeService.getCounties(provinceId, cityId).await() 17 | 18 | suspend fun fetchWeather(weatherId: String) = weatherService.getWeather(weatherId).await() 19 | 20 | suspend fun fetchBingPic() = weatherService.getBingPic().await() 21 | 22 | // 伴生类 静态方法 单列 23 | companion object { 24 | 25 | private var network: ExampleNetwork? = null 26 | 27 | fun getInstance(): ExampleNetwork { 28 | if (network == null) { 29 | synchronized(BaseNetwork::class.java) { 30 | if (network == null) { 31 | network = ExampleNetwork() 32 | } 33 | } 34 | } 35 | return network!! 36 | } 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/network/api/PlaceService.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.network.api 2 | 3 | import com.aranandroid.mvvm.data.model.place.City 4 | import com.aranandroid.mvvm.data.model.place.County 5 | import com.aranandroid.mvvm.data.model.place.Province 6 | import retrofit2.Call 7 | import retrofit2.http.GET 8 | import retrofit2.http.Path 9 | 10 | interface PlaceService { 11 | 12 | @GET("api/china") 13 | fun getProvinces(): Call> 14 | 15 | @GET("api/china/{provinceId}") 16 | fun getCities(@Path("provinceId") provinceId: Int): Call> 17 | 18 | @GET("api/china/{provinceId}/{cityId}") 19 | fun getCounties(@Path("provinceId") provinceId: Int, @Path("cityId") cityId: Int): Call> 20 | 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/data/network/api/WeatherService.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.data.network.api 2 | 3 | import com.aranandroid.mvvm.data.model.weather.HeWeather 4 | import retrofit2.Call 5 | import retrofit2.http.GET 6 | import retrofit2.http.Query 7 | 8 | interface WeatherService { 9 | 10 | @GET("api/weather") 11 | fun getWeather(@Query("cityid") weatherId: String): Call 12 | // 网络请求返回一个 Call对象 13 | @GET("api/bing_pic") 14 | fun getBingPic(): Call 15 | 16 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui 2 | 3 | import android.Manifest 4 | import android.os.Bundle 5 | import com.aranandroid.mvvm.R 6 | import com.aranandroid.mvvm.ui.weather.WeatherActivity 7 | import android.content.Intent 8 | import android.widget.Toast 9 | import com.aranandroid.mvvm.base.vm.BaseActivity 10 | import com.aranandroid.mvvm.databinding.ActivityMainBinding 11 | import com.aranandroid.mvvm.permission.PermissonDialog 12 | import com.aranandroid.mvvm.ui.area.ChooseAreaFragment 13 | import com.permissionx.guolindev.PermissionX 14 | 15 | 16 | class MainActivity : BaseActivity(R.layout.activity_main) { 17 | 18 | //测试 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | //请求权限 22 | registerPermissions() 23 | if (viewModel.isWeatherCached()) { 24 | val intent = Intent(this, WeatherActivity::class.java) 25 | startActivity(intent) 26 | finish() 27 | } else { 28 | supportFragmentManager.beginTransaction().replace(binding.container.id, ChooseAreaFragment()).commit() 29 | } 30 | 31 | } 32 | 33 | private fun registerPermissions() { 34 | PermissionX.init(this).permissions( 35 | Manifest.permission.WRITE_EXTERNAL_STORAGE, 36 | Manifest.permission.READ_EXTERNAL_STORAGE 37 | ) 38 | .onExplainRequestReason { scope, deniedList, beforeRequest -> 39 | val message = "PermissionX需要您同意以下权限才能正常使用" 40 | val dialog = PermissonDialog(this, message, deniedList) 41 | scope.showRequestReasonDialog(dialog) 42 | }.onForwardToSettings { scope, deniedList -> 43 | val message = "您需要去设置中手动开启以下权限" 44 | val dialog = PermissonDialog(this, message, deniedList) 45 | scope.showForwardToSettingsDialog(dialog) 46 | }.request { allGranted, grantedList, deniedList -> 47 | if (allGranted) { 48 | Toast.makeText(this, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show() 49 | } else { 50 | Toast.makeText(this, "您拒绝了如下权限:$deniedList", Toast.LENGTH_SHORT).show() 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui 2 | 3 | import com.aranandroid.mvvm.base.m.BaseViewModel 4 | import com.aranandroid.mvvm.data.WeatherRepository 5 | import com.aranandroid.mvvm.data.db.ExampleDatabase 6 | import com.aranandroid.mvvm.data.network.ExampleNetwork 7 | 8 | class MainViewModel() : BaseViewModel() { 9 | private val repository: WeatherRepository = WeatherRepository.getInstance(ExampleDatabase.getWeatherDao(), ExampleNetwork.getInstance()) 10 | 11 | fun isWeatherCached() = repository.isWeatherCached() 12 | 13 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/area/ChooseAreaAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui.area 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.ArrayAdapter 8 | import androidx.databinding.DataBindingUtil 9 | import com.aranandroid.mvvm.databinding.SimpleItemBinding 10 | 11 | class ChooseAreaAdapter(context: Context, private val resId: Int, private val dataList: List) : ArrayAdapter(context, resId, dataList) { 12 | 13 | override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 14 | val bind: SimpleItemBinding? 15 | val view = if (convertView == null) { 16 | val v = LayoutInflater.from(context).inflate(resId, parent, false) 17 | bind = DataBindingUtil.bind(v) 18 | v.tag = bind 19 | v 20 | } else { 21 | bind = convertView.tag as SimpleItemBinding 22 | convertView 23 | } 24 | bind?.data = dataList[position] 25 | return view 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/area/ChooseAreaFragment.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui.area 2 | 3 | import android.app.ProgressDialog 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.view.View 7 | import android.widget.* 8 | import androidx.lifecycle.Observer 9 | import com.aranandroid.mvvm.R 10 | import com.aranandroid.mvvm.databinding.ChooseAreaBindingImpl 11 | import com.aranandroid.mvvm.ui.MainActivity 12 | import com.aranandroid.mvvm.base.vm.BaseFragment 13 | import com.aranandroid.mvvm.ui.weather.WeatherActivity 14 | import kotlinx.android.synthetic.main.activity_weather.* 15 | import kotlinx.android.synthetic.main.choose_area.* 16 | 17 | class ChooseAreaFragment : 18 | BaseFragment(R.layout.choose_area) { 19 | private var progressDialog: ProgressDialog? = null 20 | private lateinit var adapter: ArrayAdapter 21 | 22 | 23 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 24 | super.onViewCreated(view, savedInstanceState) 25 | binding?.viewModel = viewModel 26 | binding?.lifecycleOwner = this 27 | } 28 | 29 | 30 | override fun onActivityCreated(savedInstanceState: Bundle?) { 31 | super.onActivityCreated(savedInstanceState) 32 | adapter = ChooseAreaAdapter(context!!, R.layout.simple_item, viewModel.dataList) 33 | listView.adapter = adapter 34 | observe() 35 | } 36 | 37 | private fun observe() { 38 | // 通过观察当前水平的值 决定title的样式 (livedata) 39 | viewModel.currentLevel.observe(this, Observer { level -> 40 | when (level) { 41 | LEVEL_PROVINCE -> { 42 | viewModel.title.value = "中国" 43 | backButton.visibility = View.GONE 44 | } 45 | LEVEL_CITY -> { 46 | viewModel.title.value = viewModel.selectedProvince?.provinceName 47 | backButton.visibility = View.VISIBLE 48 | } 49 | LEVEL_COUNTY -> { 50 | viewModel.title.value = viewModel.selectedCity?.cityName 51 | backButton.visibility = View.VISIBLE 52 | } 53 | } 54 | }) 55 | viewModel.dataChanged.observe(this, Observer { 56 | adapter.notifyDataSetChanged() 57 | listView.setSelection(0) 58 | closeProgressDialog() 59 | }) 60 | viewModel.isLoading.observe(this, Observer { isLoading -> 61 | if (isLoading) showProgressDialog() 62 | else closeProgressDialog() 63 | }) 64 | viewModel.areaSelected.observe(this, Observer { selected -> 65 | if (selected && viewModel.selectedCounty != null) { 66 | if (activity is MainActivity) { 67 | val intent = Intent(activity, WeatherActivity::class.java) 68 | intent.putExtra("weather_id", viewModel.selectedCounty!!.weatherId) 69 | startActivity(intent) 70 | activity?.finish() 71 | } else if (activity is WeatherActivity) { 72 | val weatherActivity = activity as WeatherActivity 73 | weatherActivity.drawerLayout.closeDrawers() 74 | weatherActivity.viewModel.weatherId = viewModel.selectedCounty!!.weatherId 75 | weatherActivity.viewModel.refreshWeather() 76 | } 77 | viewModel.areaSelected.value = false 78 | } 79 | }) 80 | if (viewModel.dataList.isEmpty()) { 81 | viewModel.getProvinces() 82 | } 83 | } 84 | 85 | /** 86 | * 显示进度对话框 87 | */ 88 | private fun showProgressDialog() { 89 | if (progressDialog == null) { 90 | progressDialog = ProgressDialog(activity) 91 | progressDialog?.setMessage("正在加载...") 92 | progressDialog?.setCanceledOnTouchOutside(false) 93 | } 94 | progressDialog?.show() 95 | } 96 | 97 | /** 98 | * 关闭进度对话框 99 | */ 100 | private fun closeProgressDialog() { 101 | progressDialog?.dismiss() 102 | } 103 | 104 | // 伴生对象 105 | companion object { 106 | // 省 市 县 107 | const val LEVEL_PROVINCE = 0 108 | const val LEVEL_CITY = 1 109 | const val LEVEL_COUNTY = 2 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/area/ChooseAreaViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui.area 2 | 3 | import android.view.View 4 | import android.widget.AdapterView 5 | import android.widget.Toast 6 | import androidx.lifecycle.MutableLiveData 7 | import androidx.lifecycle.viewModelScope 8 | import com.aranandroid.mvvm.BaseApplication 9 | import com.aranandroid.mvvm.data.PlaceRepository 10 | import com.aranandroid.mvvm.data.model.place.City 11 | import com.aranandroid.mvvm.data.model.place.County 12 | import com.aranandroid.mvvm.data.model.place.Province 13 | import com.aranandroid.mvvm.ui.area.ChooseAreaFragment.Companion.LEVEL_CITY 14 | import com.aranandroid.mvvm.ui.area.ChooseAreaFragment.Companion.LEVEL_COUNTY 15 | import com.aranandroid.mvvm.ui.area.ChooseAreaFragment.Companion.LEVEL_PROVINCE 16 | import com.aranandroid.mvvm.base.m.BaseViewModel 17 | import com.aranandroid.mvvm.data.db.ExampleDatabase 18 | import com.aranandroid.mvvm.data.network.ExampleNetwork 19 | import kotlinx.coroutines.launch 20 | import java.util.* 21 | 22 | class ChooseAreaViewModel() : BaseViewModel() { 23 | 24 | private val repository: PlaceRepository = PlaceRepository.getInstance(ExampleDatabase.getPlaceDao(), ExampleNetwork.getInstance()) 25 | 26 | var title = MutableLiveData() 27 | 28 | // 当前水平 29 | var currentLevel = MutableLiveData() 30 | 31 | var dataChanged = MutableLiveData() 32 | 33 | var isLoading = MutableLiveData() 34 | 35 | var areaSelected = MutableLiveData() 36 | 37 | var selectedProvince: Province? = null 38 | 39 | var selectedCity: City? = null 40 | 41 | var selectedCounty: County? = null 42 | 43 | lateinit var provinces: MutableList 44 | 45 | lateinit var cities: MutableList 46 | 47 | lateinit var counties: MutableList 48 | 49 | val dataList = ArrayList() 50 | 51 | fun getProvinces() { 52 | currentLevel.value = LEVEL_PROVINCE 53 | launch { 54 | provinces = repository.getProvinceList() 55 | dataList.addAll(provinces.map { it.provinceName }) 56 | } 57 | } 58 | 59 | private fun getCities() = selectedProvince?.let { 60 | currentLevel.value = LEVEL_CITY 61 | launch { 62 | cities = repository.getCityList(it.provinceCode) 63 | dataList.addAll(cities.map { it.cityName }) 64 | } 65 | } 66 | 67 | private fun getCounties() = selectedCity?.let { 68 | currentLevel.value = LEVEL_COUNTY 69 | launch { 70 | counties = repository.getCountyList(it.provinceId, it.cityCode) 71 | dataList.addAll(counties.map { it.countyName }) 72 | } 73 | } 74 | 75 | fun onListViewItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { 76 | when { 77 | currentLevel.value == LEVEL_PROVINCE -> { 78 | selectedProvince = provinces[position] 79 | getCities() 80 | } 81 | currentLevel.value == LEVEL_CITY -> { 82 | selectedCity = cities[position] 83 | getCounties() 84 | } 85 | currentLevel.value == LEVEL_COUNTY -> { 86 | selectedCounty = counties[position] 87 | areaSelected.value = true 88 | } 89 | } 90 | } 91 | 92 | fun onBack() { 93 | if (currentLevel.value == LEVEL_COUNTY) { 94 | getCities() 95 | } else if (currentLevel.value == LEVEL_CITY) { 96 | getProvinces() 97 | } 98 | } 99 | 100 | private fun launch(block: suspend () -> Unit) = viewModelScope.launch { 101 | try { 102 | isLoading.value = true 103 | dataList.clear() 104 | block() 105 | dataChanged.value = dataChanged.value?.plus(1) 106 | isLoading.value = false 107 | } catch (t: Throwable) { 108 | t.printStackTrace() 109 | Toast.makeText(BaseApplication.context, t.message, Toast.LENGTH_SHORT).show() 110 | dataChanged.value = dataChanged.value?.plus(1) 111 | isLoading.value = false 112 | } 113 | } 114 | 115 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/weather/WeatherActivity.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui.weather 2 | 3 | import android.graphics.Color 4 | import android.os.Build 5 | import android.os.Bundle 6 | import android.view.View 7 | import androidx.core.view.GravityCompat 8 | import com.aranandroid.mvvm.R 9 | import com.aranandroid.mvvm.databinding.ActivityWeatherBinding 10 | import com.aranandroid.mvvm.base.vm.BaseActivity 11 | import kotlinx.android.synthetic.main.activity_weather.* 12 | import kotlinx.android.synthetic.main.title.* 13 | 14 | class WeatherActivity : BaseActivity(R.layout.activity_weather) { 15 | override fun onCreate(savedInstanceState: Bundle?) { 16 | super.onCreate(savedInstanceState) 17 | if (Build.VERSION.SDK_INT >= 21) { 18 | val decorView = window.decorView 19 | decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE 20 | window.statusBarColor = Color.TRANSPARENT 21 | } 22 | binding.viewModel = viewModel 23 | binding.resId = R.color.colorPrimary 24 | binding.lifecycleOwner = this 25 | viewModel.weatherId = if (viewModel.isWeatherCached()) { 26 | viewModel.getCachedWeather().basic.weatherId 27 | } else { 28 | intent.getStringExtra("weather_id") 29 | } 30 | navButton.setOnClickListener { 31 | drawerLayout.openDrawer(GravityCompat.START) 32 | } 33 | viewModel.getWeather() 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/ui/weather/WeatherViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.ui.weather 2 | 3 | import android.widget.Toast 4 | import androidx.lifecycle.MutableLiveData 5 | import androidx.lifecycle.viewModelScope 6 | import com.aranandroid.mvvm.BaseApplication 7 | import com.aranandroid.mvvm.data.WeatherRepository 8 | import com.aranandroid.mvvm.data.model.weather.Weather 9 | import com.aranandroid.mvvm.base.m.BaseViewModel 10 | import com.aranandroid.mvvm.data.db.ExampleDatabase 11 | import com.aranandroid.mvvm.data.network.ExampleNetwork 12 | import kotlinx.coroutines.launch 13 | 14 | class WeatherViewModel() : BaseViewModel() { 15 | private val repository: WeatherRepository = WeatherRepository.getInstance(ExampleDatabase.getWeatherDao(), ExampleNetwork.getInstance()) 16 | var weather = MutableLiveData() 17 | 18 | var bingPicUrl = MutableLiveData() 19 | 20 | var refreshing = MutableLiveData() 21 | 22 | var weatherInitialized = MutableLiveData() 23 | 24 | var weatherId = "" 25 | 26 | fun getWeather() { 27 | 28 | launch ({ 29 | weather.value = repository.getWeather(weatherId) 30 | weatherInitialized.value = true 31 | }, { 32 | Toast.makeText(BaseApplication.context, it.message, Toast.LENGTH_SHORT).show() 33 | }) 34 | getBingPic(false) 35 | } 36 | 37 | fun refreshWeather() { 38 | refreshing.value = true 39 | launch ({ 40 | weather.value = repository.refreshWeather(weatherId) 41 | refreshing.value = false 42 | weatherInitialized.value = true 43 | }, { 44 | Toast.makeText(BaseApplication.context, it.message, Toast.LENGTH_SHORT).show() 45 | refreshing.value = false 46 | }) 47 | getBingPic(true) 48 | } 49 | 50 | fun isWeatherCached() = repository.isWeatherCached() 51 | 52 | fun getCachedWeather() = repository.getCachedWeather() 53 | 54 | fun onRefresh() { 55 | refreshWeather() 56 | } 57 | 58 | private fun getBingPic(refresh: Boolean) { 59 | // viewModelScope.launch 开启协程方法 60 | launch({ 61 | bingPicUrl.value = if (refresh) repository.refreshBingPic() else repository.getBingPic() 62 | }, { 63 | Toast.makeText(BaseApplication.context, it.message, Toast.LENGTH_SHORT).show() 64 | }) 65 | } 66 | 67 | /** 68 | * 自定义一个launch方法 69 | * 高阶函数:(参数或者返回值有方法的函数) 70 | */ 71 | private fun launch(block: suspend () -> Unit, error: suspend (Throwable) -> Unit) = viewModelScope.launch { 72 | try { 73 | block() 74 | } catch (e: Throwable) { 75 | error(e) 76 | } 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /app/src/main/java/com/aranandroid/mvvm/util/BindingAdapters.kt: -------------------------------------------------------------------------------- 1 | package com.aranandroid.mvvm.util 2 | 3 | import android.view.LayoutInflater 4 | import android.widget.ImageView 5 | import android.widget.LinearLayout 6 | import androidx.databinding.BindingAdapter 7 | import androidx.databinding.DataBindingUtil 8 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout 9 | import com.bumptech.glide.Glide 10 | import com.aranandroid.mvvm.R 11 | import com.aranandroid.mvvm.data.model.weather.Weather 12 | import com.aranandroid.mvvm.databinding.ForecastItemBinding 13 | 14 | @BindingAdapter("bind:loadBingPic") 15 | fun ImageView.loadBingPic(url: String?) { 16 | if (url != null) Glide.with(context).load(url).into(this) 17 | } 18 | 19 | @BindingAdapter("bind:colorSchemeResources") 20 | fun SwipeRefreshLayout.colorSchemeResources(resId: Int) { 21 | setColorSchemeResources(resId) 22 | } 23 | 24 | @BindingAdapter("bind:showForecast") 25 | fun LinearLayout.showForecast(weather: Weather?) = weather?.let { 26 | removeAllViews() 27 | for (forecast in it.forecastList) { 28 | val view = LayoutInflater.from(context).inflate(R.layout.forecast_item, this, false) 29 | DataBindingUtil.bind(view)?.forecast = forecast 30 | addView(view) 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AranAndroid009/Mvvm/94e887210b10d23982f5907cd96b3470f8425c9c/app/src/main/res/drawable-xxhdpi/ic_back.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AranAndroid009/Mvvm/94e887210b10d23982f5907cd96b3470f8425c9c/app/src/main/res/drawable-xxhdpi/ic_home.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_weather.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | 21 | 22 | 26 | 27 | 35 | 36 | 43 | 44 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /app/src/main/res/layout/aqi.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 20 | 21 | 25 | 26 | 30 | 31 | 36 | 37 | 46 | 47 | 53 | 54 | 55 | 56 | 57 | 58 | 62 | 67 | 76 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /app/src/main/res/layout/choose_area.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 11 | 15 | 16 | 24 | 25 |