├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── org │ │ └── attentiveness │ │ └── news │ │ ├── base │ │ ├── BaseActivity.java │ │ ├── BaseFragment.java │ │ ├── BasePresenter.java │ │ └── BaseView.java │ │ ├── data │ │ ├── News.java │ │ ├── Story.java │ │ ├── StoryDetail.java │ │ └── source │ │ │ ├── StoriesDataRepository.java │ │ │ ├── StoriesDataSource.java │ │ │ ├── local │ │ │ ├── LocalStoriesDataSource.java │ │ │ ├── StoriesDbHelper.java │ │ │ └── StoriesPersistenceContract.java │ │ │ └── remote │ │ │ └── RemoteStoriesDataSource.java │ │ ├── detail │ │ ├── StoryDetailActivity.java │ │ ├── StoryDetailContract.java │ │ ├── StoryDetailFragment.java │ │ └── StoryDetailPresenter.java │ │ ├── exception │ │ └── NetworkException.java │ │ ├── list │ │ ├── LoadMoreListener.java │ │ ├── ScrollChildSwipeRefreshLayout.java │ │ ├── StoryListActivity.java │ │ ├── StoryListAdapter.java │ │ ├── StoryListContract.java │ │ ├── StoryListFragment.java │ │ └── StoryListPresenter.java │ │ ├── net │ │ ├── HttpManager.java │ │ └── StoryService.java │ │ ├── splash │ │ └── SplashActivity.java │ │ └── util │ │ ├── DateUtil.java │ │ ├── LogUtil.java │ │ └── schedulers │ │ ├── BaseSchedulerProvider.java │ │ └── SchedulerProvider.java │ └── res │ ├── drawable │ ├── ic_about.xml │ ├── ic_close.xml │ ├── ic_feedback.xml │ ├── ic_menu.xml │ ├── ic_network_error.xml │ ├── ic_settings.xml │ ├── ic_story_list.xml │ └── side_nav_bar.xml │ ├── layout │ ├── activity_splash.xml │ ├── activity_story_detail.xml │ ├── activity_story_list.xml │ ├── app_bar.xml │ ├── app_bar_story_list.xml │ ├── content_story_detail.xml │ ├── content_story_list.xml │ ├── fragment_story_detail.xml │ ├── fragment_story_list.xml │ ├── item_story.xml │ └── nav_header_story_list.xml │ ├── menu │ └── activity_story_list_drawer.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-v21 │ └── styles.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── documents ├── News_Android.vp ├── event_flow.png └── requirements_analysis.md ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .gradle/ 3 | 4 | build/ 5 | */build/ 6 | 7 | local.properties 8 | 9 | *.iml 10 | */*.iml 11 | 12 | # Crashlytics configuations 13 | com_crashlytics_export_strings.xml 14 | crashlytics-build.properties 15 | 16 | # Eclipse project files 17 | .classpath 18 | .project 19 | 20 | # OSX files 21 | .DS_Store 22 | .DS_Store? 23 | 24 | # Windows thumbnail db 25 | ehthumbs.db 26 | Thumbs.db 27 | 28 | # OS-specific files 29 | ._* 30 | .Spotlight-V100 31 | .Trashes 32 | 33 | # Log Files 34 | *.log -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # News 2 | The News app is a simple android app, which uses the MVP architecture. 3 | 4 | # Features 5 | * Use MVP architecture 6 | * Use popular third party libraries, such as: RxJava, OkHttp, Retrofit, Picasso and so on 7 | * Use different test methods to test different layers 8 | * Use Repository Design Pattern 9 | 10 | # Contributors 11 | [@WolfHan](https://github.com/wolfhan) 12 | [@Richard-Cao](https://github.com/Richard-Cao) 13 | 14 | # License 15 | Apache License 2.0 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion '25.0.2' 6 | 7 | defaultConfig { 8 | applicationId "org.attentiveness.news" 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | 28 | // // Dependencies for local unit tests 29 | // testCompile "junit:junit:4.12" 30 | // testCompile "org.mockito:mockito-all:1.10.19" 31 | // testCompile "org.hamcrest:hamcrest-all:1.3" 32 | // 33 | // // Android Testing Support Library's runner and rules 34 | // androidTestCompile "com.android.support.test:runner:0.4.1" 35 | // androidTestCompile "com.android.support.test:rules:0.4.1" 36 | // 37 | // // Dependencies for Android unit tests 38 | // androidTestCompile "junit:junit:4.12" 39 | // androidTestCompile "org.mockito:mockito-core:1.10.19" 40 | // androidTestCompile "com.google.dexmaker:dexmaker:1.2" 41 | // androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" 42 | // 43 | // // Espresso UI Testing 44 | // androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.1" 45 | // androidTestCompile "com.android.support.test.espresso:espresso-contrib:2.2.1" 46 | // androidTestCompile "com.android.support.test.espresso:espresso-intents:2.2.1" 47 | // 48 | // // Resolve conflicts between main and test APK 49 | // androidTestCompile "com.android.support:support-annotations:23.4.0" 50 | // androidTestCompile "com.android.support:support-v4:23.4.0" 51 | // androidTestCompile "com.android.support:recyclerview-v7:23.4.0" 52 | 53 | compile 'com.android.support:appcompat-v7:25.3.1' 54 | compile 'com.android.support:support-v4:25.3.1' 55 | compile 'com.android.support:design:25.3.1' 56 | compile 'com.android.support:recyclerview-v7:25.3.1' 57 | compile 'com.android.support:cardview-v7:25.3.1' 58 | compile 'com.android.support:gridlayout-v7:25.3.1' 59 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8' 60 | compile 'com.google.code.gson:gson:2.8.0' 61 | compile 'com.squareup.okhttp3:okhttp:3.6.0' 62 | compile 'com.squareup.picasso:picasso:2.5.2' 63 | compile 'io.reactivex.rxjava2:rxjava:2.0.8' 64 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 65 | compile 'com.squareup.retrofit2:retrofit:2.2.0' 66 | compile 'com.squareup.retrofit2:converter-gson:2.2.0' 67 | compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' 68 | compile 'com.squareup.retrofit2:retrofit-mock:2.2.0' 69 | compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2' 70 | compile 'com.jakewharton:butterknife:8.5.1' 71 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' 72 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/richardcao/developer/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 | 32 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.base; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.design.widget.NavigationView; 7 | import android.support.design.widget.Snackbar; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.app.FragmentManager; 10 | import android.support.v4.app.FragmentTransaction; 11 | import android.support.v4.widget.DrawerLayout; 12 | import android.support.v7.app.ActionBar; 13 | import android.support.v7.app.AppCompatActivity; 14 | import android.support.v7.widget.Toolbar; 15 | import android.view.View; 16 | 17 | import org.attentiveness.news.R; 18 | 19 | import butterknife.BindView; 20 | 21 | /** 22 | * Base Activity 23 | */ 24 | public abstract class BaseActivity extends AppCompatActivity { 25 | 26 | @Nullable 27 | @BindView(R.id.toolbar) 28 | Toolbar mToolbar; 29 | @Nullable 30 | @BindView(R.id.drawer_layout) 31 | protected DrawerLayout mDrawerLayout; 32 | @Nullable 33 | @BindView(R.id.nav_view) 34 | NavigationView mNavigationView; 35 | 36 | @Override 37 | protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | } 40 | 41 | protected void setup() { 42 | setup(-1); 43 | } 44 | 45 | protected void setup(int barIconId) { 46 | setSupportActionBar(this.mToolbar); 47 | ActionBar actionBar = getSupportActionBar(); 48 | if (actionBar != null) { 49 | if (barIconId > 0) { 50 | actionBar.setHomeAsUpIndicator(barIconId); 51 | } 52 | actionBar.setDisplayHomeAsUpEnabled(true); 53 | } 54 | 55 | if (this.mDrawerLayout != null) { 56 | this.mDrawerLayout.setStatusBarBackground(R.color.colorPrimaryDark); 57 | } 58 | if (this.mNavigationView != null) { 59 | setupDrawerContent(this.mNavigationView); 60 | } 61 | } 62 | 63 | protected void setupDrawerContent(NavigationView navigationView) { 64 | 65 | } 66 | 67 | protected void addFragment(FragmentManager fragmentManager, int containerId, Fragment fragment) { 68 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 69 | fragmentTransaction.add(containerId, fragment); 70 | fragmentTransaction.commit(); 71 | } 72 | 73 | protected void showMessage(View anchorView, String message) { 74 | Snackbar.make(anchorView, message, Snackbar.LENGTH_SHORT).show(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.base; 2 | 3 | 4 | import android.support.design.widget.Snackbar; 5 | import android.support.v4.app.Fragment; 6 | import android.view.View; 7 | 8 | /** 9 | * Base Fragment 10 | */ 11 | public class BaseFragment extends Fragment { 12 | 13 | public BaseFragment() { 14 | // Required empty public constructor 15 | } 16 | 17 | protected void showMessage(View anchorView, String message) { 18 | Snackbar.make(anchorView, message, Snackbar.LENGTH_SHORT).show(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/base/BasePresenter.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.base; 2 | 3 | 4 | public interface BasePresenter { 5 | 6 | void subscribe(); 7 | 8 | void unsubscribe(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/base/BaseView.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.base; 2 | 3 | 4 | import android.support.annotation.NonNull; 5 | 6 | public interface BaseView { 7 | 8 | void setPresenter(@NonNull T presenter); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/News.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class News { 8 | 9 | @SerializedName("date") 10 | private String date; 11 | 12 | @SerializedName("stories") 13 | private List storyList; 14 | 15 | @SerializedName("top_stories") 16 | private List topStoryList; 17 | 18 | public News() { 19 | 20 | } 21 | 22 | public String getDate() { 23 | return date; 24 | } 25 | 26 | public void setDate(String date) { 27 | this.date = date; 28 | } 29 | 30 | public List getStoryList() { 31 | return storyList; 32 | } 33 | 34 | public void setStoryList(List storyList) { 35 | this.storyList = storyList; 36 | } 37 | 38 | public List getTopStoryList() { 39 | return topStoryList; 40 | } 41 | 42 | public void setTopStoryList(List topStoryList) { 43 | this.topStoryList = topStoryList; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/Story.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class Story { 8 | 9 | private int id; 10 | private String title; 11 | 12 | @SerializedName("images") 13 | private List imageList; 14 | 15 | public Story() { 16 | 17 | } 18 | 19 | public Story(int id, String title) { 20 | this.id = id; 21 | this.title = title; 22 | } 23 | 24 | public int getId() { 25 | return id; 26 | } 27 | 28 | public void setId(int id) { 29 | this.id = id; 30 | } 31 | 32 | public String getTitle() { 33 | return title; 34 | } 35 | 36 | public void setTitle(String title) { 37 | this.title = title; 38 | } 39 | 40 | public List getImageList() { 41 | return imageList; 42 | } 43 | 44 | public void setImageList(List imageList) { 45 | this.imageList = imageList; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/StoryDetail.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class StoryDetail { 8 | 9 | @SerializedName("id") 10 | private int id; 11 | 12 | @SerializedName("body") 13 | private String content; 14 | 15 | @SerializedName("title") 16 | private String title; 17 | 18 | @SerializedName("image") 19 | private String imageUrl; 20 | 21 | @SerializedName("image_source") 22 | private String imageSource; 23 | 24 | @SerializedName("js") 25 | private List jsList; 26 | 27 | @SerializedName("css") 28 | private List cssList; 29 | 30 | public StoryDetail() { 31 | 32 | } 33 | 34 | public int getId() { 35 | return id; 36 | } 37 | 38 | public void setId(int id) { 39 | this.id = id; 40 | } 41 | 42 | public String getContent() { 43 | return content; 44 | } 45 | 46 | public void setContent(String content) { 47 | this.content = content; 48 | } 49 | 50 | public String getTitle() { 51 | return title; 52 | } 53 | 54 | public void setTitle(String title) { 55 | this.title = title; 56 | } 57 | 58 | public String getImageUrl() { 59 | return imageUrl; 60 | } 61 | 62 | public void setImageUrl(String imageUrl) { 63 | this.imageUrl = imageUrl; 64 | } 65 | 66 | public String getImageSource() { 67 | return imageSource; 68 | } 69 | 70 | public void setImageSource(String imageSource) { 71 | this.imageSource = imageSource; 72 | } 73 | 74 | public List getJsList() { 75 | return jsList; 76 | } 77 | 78 | public void setJsList(List jsList) { 79 | this.jsList = jsList; 80 | } 81 | 82 | public List getCssList() { 83 | return cssList; 84 | } 85 | 86 | public void setCssList(List cssList) { 87 | this.cssList = cssList; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/StoriesDataRepository.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import org.attentiveness.news.data.Story; 6 | import org.attentiveness.news.data.StoryDetail; 7 | 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import io.reactivex.Observable; 13 | import io.reactivex.functions.Action; 14 | import io.reactivex.functions.Consumer; 15 | import io.reactivex.functions.Predicate; 16 | 17 | public class StoriesDataRepository implements StoriesDataSource { 18 | 19 | private static StoriesDataRepository INSTANCE = null; 20 | 21 | private final StoriesDataSource mStoriesRemoteDataSource; 22 | 23 | private final StoriesDataSource mStoriesLocalDataSource; 24 | 25 | /** 26 | * This variable has package local visibility so it can be accessed from tests. 27 | */ 28 | private Map mCachedStories; 29 | 30 | /** 31 | * Marks the cache as invalid, to force an update the next time data is requested. This variable 32 | * has package local visibility so it can be accessed from tests. 33 | */ 34 | private boolean mCacheIsDirty = false; 35 | 36 | // Prevent direct instantiation. 37 | private StoriesDataRepository(@NonNull StoriesDataSource storiesRemoteDataSource, 38 | @NonNull StoriesDataSource storiesLocalDataSource) { 39 | this.mStoriesRemoteDataSource = storiesRemoteDataSource; 40 | this.mStoriesLocalDataSource = storiesLocalDataSource; 41 | this.mCachedStories = new HashMap<>(); 42 | } 43 | 44 | public static StoriesDataRepository getInstance(StoriesDataSource storiesRemoteDataSource, 45 | StoriesDataSource storiesLocalDataSource) { 46 | if (INSTANCE == null) { 47 | INSTANCE = new StoriesDataRepository(storiesRemoteDataSource, storiesLocalDataSource); 48 | } 49 | return INSTANCE; 50 | } 51 | 52 | @Override 53 | public Observable> getStories(String date) { 54 | if (this.mCachedStories != null && !this.mCacheIsDirty) { 55 | List storyList = (List) this.mCachedStories.values(); 56 | return Observable.just(storyList); 57 | } 58 | Observable> remoteStoryList = this.getAndSaveStoryListFromRemote(date); 59 | if (this.mCacheIsDirty) { 60 | return remoteStoryList; 61 | } else { 62 | Observable> localStoryList = this.getAndSaveStoryListFromLocal(date); 63 | return Observable.concat(localStoryList, remoteStoryList) 64 | .filter(new Predicate>() { 65 | @Override 66 | public boolean test(@io.reactivex.annotations.NonNull List storyList) throws Exception { 67 | return storyList != null && storyList.size() > 0; 68 | } 69 | }) 70 | .firstElement() 71 | .toObservable(); 72 | } 73 | } 74 | 75 | /** 76 | * Get story detail. Now it just gets data from remote source, not from local source and not 77 | * cache the story detail. 78 | * 79 | * @param storyId Each story has one unique id. 80 | * @return Observable wraps story detail. 81 | */ 82 | @Override 83 | public Observable getStoryDetail(int storyId) { 84 | return this.mStoriesRemoteDataSource.getStoryDetail(storyId); 85 | } 86 | 87 | @Override 88 | public void saveStories(@NonNull List storyList) { 89 | this.mStoriesLocalDataSource.saveStories(storyList); 90 | this.mStoriesRemoteDataSource.saveStories(storyList); 91 | if (this.mCachedStories == null) { 92 | this.mCachedStories = new HashMap<>(); 93 | } 94 | for (Story story : storyList) { 95 | this.mCachedStories.put(story.getId(), story); 96 | } 97 | } 98 | 99 | @Override 100 | public void refreshStories() { 101 | this.mCacheIsDirty = true; 102 | } 103 | 104 | @Override 105 | public void deleteAllStories() { 106 | this.mStoriesLocalDataSource.deleteAllStories(); 107 | this.mStoriesRemoteDataSource.deleteAllStories(); 108 | if (this.mCachedStories == null) { 109 | this.mCachedStories = new HashMap<>(); 110 | } 111 | this.mCachedStories.clear(); 112 | } 113 | 114 | private Observable> getAndSaveStoryListFromRemote(String date) { 115 | return this.mStoriesRemoteDataSource.getStories(date) 116 | .doOnNext(new Consumer>() { 117 | @Override 118 | public void accept(@io.reactivex.annotations.NonNull List storyList) throws Exception { 119 | mStoriesLocalDataSource.saveStories(storyList); 120 | for (Story story : storyList) { 121 | mCachedStories.put(story.getId(), story); 122 | } 123 | } 124 | }) 125 | .doOnComplete(new Action() { 126 | @Override 127 | public void run() throws Exception { 128 | mCacheIsDirty = true; 129 | } 130 | }); 131 | } 132 | 133 | private Observable> getAndSaveStoryListFromLocal(String date) { 134 | return this.mStoriesLocalDataSource.getStories(date).doOnNext(new Consumer>() { 135 | @Override 136 | public void accept(@io.reactivex.annotations.NonNull List storyList) throws Exception { 137 | for (Story story : storyList) { 138 | mCachedStories.put(story.getId(), story); 139 | } 140 | } 141 | }); 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/StoriesDataSource.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import org.attentiveness.news.data.Story; 6 | import org.attentiveness.news.data.StoryDetail; 7 | 8 | import java.util.List; 9 | 10 | import io.reactivex.Observable; 11 | 12 | public interface StoriesDataSource { 13 | 14 | Observable> getStories(String date); 15 | 16 | Observable getStoryDetail(int storyId); 17 | 18 | void saveStories(@NonNull List storyList); 19 | 20 | void refreshStories(); 21 | 22 | void deleteAllStories(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/local/LocalStoriesDataSource.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source.local; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.Cursor; 6 | import android.database.sqlite.SQLiteDatabase; 7 | import android.support.annotation.NonNull; 8 | 9 | import org.attentiveness.news.data.Story; 10 | import org.attentiveness.news.data.StoryDetail; 11 | import org.attentiveness.news.data.source.StoriesDataSource; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | import io.reactivex.Observable; 17 | 18 | public class LocalStoriesDataSource implements StoriesDataSource { 19 | 20 | private static LocalStoriesDataSource INSTANCE; 21 | 22 | private StoriesDbHelper mDbHelper; 23 | 24 | // Prevent direct instantiation. 25 | private LocalStoriesDataSource(@NonNull Context context) { 26 | this.mDbHelper = new StoriesDbHelper(context); 27 | } 28 | 29 | public static LocalStoriesDataSource getInstance(@NonNull Context context) { 30 | if (INSTANCE == null) { 31 | INSTANCE = new LocalStoriesDataSource(context); 32 | } 33 | return INSTANCE; 34 | } 35 | 36 | @Override 37 | public Observable> getStories(String date) { 38 | List storyList = new ArrayList<>(); 39 | SQLiteDatabase database = this.mDbHelper.getReadableDatabase(); 40 | String[] projections = { 41 | StoriesPersistenceContract.StoryEntry.COLUMN_NAME_STORY_ID, 42 | StoriesPersistenceContract.StoryEntry.COLUMN_NAME_TITLE}; 43 | Cursor cursor = database.query(StoriesPersistenceContract.StoryEntry.TABLE_NAME, projections, null, null, null, null, null); 44 | if (cursor != null && cursor.getCount() > 0) { 45 | while (cursor.moveToNext()) { 46 | int storyId = cursor.getInt(cursor.getColumnIndexOrThrow(StoriesPersistenceContract.StoryEntry.COLUMN_NAME_STORY_ID)); 47 | String title = cursor.getString(cursor.getColumnIndexOrThrow(StoriesPersistenceContract.StoryEntry.COLUMN_NAME_TITLE)); 48 | Story story = new Story(storyId, title); 49 | storyList.add(story); 50 | } 51 | } 52 | if (cursor != null) { 53 | cursor.close(); 54 | } 55 | database.close(); 56 | if (storyList.size() == 0) { 57 | return Observable.empty(); 58 | } else { 59 | return Observable.just(storyList); 60 | } 61 | } 62 | 63 | @Override 64 | public Observable getStoryDetail(int storyId) { 65 | //Do not save story detail in local storage, so do nothing 66 | return null; 67 | } 68 | 69 | @Override 70 | public void saveStories(@NonNull List storyList) { 71 | SQLiteDatabase database = this.mDbHelper.getWritableDatabase(); 72 | for (Story story : storyList) { 73 | ContentValues contentValues = new ContentValues(); 74 | contentValues.put(StoriesPersistenceContract.StoryEntry.COLUMN_NAME_STORY_ID, story.getId()); 75 | contentValues.put(StoriesPersistenceContract.StoryEntry.COLUMN_NAME_TITLE, story.getTitle()); 76 | database.insertOrThrow(StoriesPersistenceContract.StoryEntry.TABLE_NAME, null, contentValues); 77 | } 78 | database.close(); 79 | } 80 | 81 | @Override 82 | public void refreshStories() { 83 | //do nothing 84 | } 85 | 86 | @Override 87 | public void deleteAllStories() { 88 | SQLiteDatabase database = this.mDbHelper.getWritableDatabase(); 89 | database.delete(StoriesPersistenceContract.StoryEntry.TABLE_NAME, null, null); 90 | database.close(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/local/StoriesDbHelper.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source.local; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | 7 | public class StoriesDbHelper extends SQLiteOpenHelper { 8 | 9 | public static final int DATABASE_VERSION = 1; 10 | 11 | public static final String DATABASE_NAME = "Stories.db"; 12 | 13 | private static final String TEXT_TYPE = " TEXT"; 14 | 15 | private static final String BOOLEAN_TYPE = " INTEGER"; 16 | 17 | private static final String COMMA_SEP = ","; 18 | 19 | private static final String SQL_CREATE_ENTRIES = 20 | "CREATE TABLE " + StoriesPersistenceContract.StoryEntry.TABLE_NAME + " (" + 21 | StoriesPersistenceContract.StoryEntry._ID + TEXT_TYPE + " PRIMARY KEY," + 22 | StoriesPersistenceContract.StoryEntry.COLUMN_NAME_STORY_ID + BOOLEAN_TYPE + COMMA_SEP + 23 | StoriesPersistenceContract.StoryEntry.COLUMN_NAME_TITLE + TEXT_TYPE + 24 | " )"; 25 | 26 | public StoriesDbHelper(Context context) { 27 | super(context, DATABASE_NAME, null, DATABASE_VERSION); 28 | } 29 | 30 | public StoriesDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 31 | super(context, name, factory, version); 32 | } 33 | 34 | @Override 35 | public void onCreate(SQLiteDatabase db) { 36 | db.execSQL(SQL_CREATE_ENTRIES); 37 | } 38 | 39 | @Override 40 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 41 | 42 | } 43 | 44 | @Override 45 | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 46 | // Not required as at version 1 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/local/StoriesPersistenceContract.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source.local; 2 | 3 | import android.provider.BaseColumns; 4 | 5 | final class StoriesPersistenceContract { 6 | 7 | // To prevent someone from accidentally instantiating the contract class, 8 | // give it an empty constructor. 9 | private StoriesPersistenceContract() { 10 | } 11 | 12 | /* Inner class that defines the table contents */ 13 | static abstract class StoryEntry implements BaseColumns { 14 | static final String TABLE_NAME = "story"; 15 | static final String COLUMN_NAME_STORY_ID = "storyId"; 16 | static final String COLUMN_NAME_TITLE = "title"; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/data/source/remote/RemoteStoriesDataSource.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.data.source.remote; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | import org.attentiveness.news.data.Story; 7 | import org.attentiveness.news.data.StoryDetail; 8 | import org.attentiveness.news.data.source.StoriesDataSource; 9 | import org.attentiveness.news.net.HttpManager; 10 | 11 | import java.util.List; 12 | 13 | import io.reactivex.Observable; 14 | 15 | public class RemoteStoriesDataSource implements StoriesDataSource { 16 | 17 | private static RemoteStoriesDataSource INSTANCE; 18 | 19 | private Context mContext; 20 | 21 | public static RemoteStoriesDataSource getInstance(Context context) { 22 | if (INSTANCE == null) { 23 | INSTANCE = new RemoteStoriesDataSource(context); 24 | } 25 | return INSTANCE; 26 | } 27 | 28 | // Prevent direct instantiation. 29 | private RemoteStoriesDataSource(Context context) { 30 | this.mContext = context; 31 | } 32 | 33 | @Override 34 | public Observable> getStories(String date) { 35 | return HttpManager.getInstance(this.mContext).getStoryList(date); 36 | } 37 | 38 | @Override 39 | public Observable getStoryDetail(int storyId) { 40 | return HttpManager.getInstance(this.mContext).getStory(storyId); 41 | } 42 | 43 | @Override 44 | public void saveStories(@NonNull List storyList) { 45 | //do nothing 46 | } 47 | 48 | @Override 49 | public void refreshStories() { 50 | // do nothing 51 | } 52 | 53 | @Override 54 | public void deleteAllStories() { 55 | // do nothing 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/detail/StoryDetailActivity.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.detail; 2 | 3 | import android.os.Bundle; 4 | 5 | import org.attentiveness.news.R; 6 | import org.attentiveness.news.base.BaseActivity; 7 | import org.attentiveness.news.data.source.StoriesDataRepository; 8 | import org.attentiveness.news.data.source.local.LocalStoriesDataSource; 9 | import org.attentiveness.news.data.source.remote.RemoteStoriesDataSource; 10 | import org.attentiveness.news.list.StoryListFragment; 11 | 12 | import butterknife.ButterKnife; 13 | 14 | public class StoryDetailActivity extends BaseActivity { 15 | 16 | private static final String INSTANCE_STORY_ID = "story_id"; 17 | 18 | private int mStoryId; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_story_detail); 24 | ButterKnife.bind(this); 25 | setup(); 26 | 27 | if (savedInstanceState == null) { 28 | this.mStoryId = getIntent().getIntExtra(StoryListFragment.EXTRA_ID, 0); 29 | } else { 30 | this.mStoryId = savedInstanceState.getInt(INSTANCE_STORY_ID); 31 | } 32 | 33 | StoryDetailFragment storyDetailFragment = (StoryDetailFragment) getSupportFragmentManager().findFragmentById(R.id.fl_container); 34 | if (storyDetailFragment == null) { 35 | storyDetailFragment = StoryDetailFragment.newInstance(); 36 | addFragment(getSupportFragmentManager(), R.id.fl_container, storyDetailFragment); 37 | } 38 | StoriesDataRepository repository = StoriesDataRepository.getInstance( 39 | RemoteStoriesDataSource.getInstance(this), LocalStoriesDataSource.getInstance(this)); 40 | StoryDetailPresenter presenter = new StoryDetailPresenter(this.mStoryId, repository, storyDetailFragment); 41 | } 42 | 43 | @Override 44 | protected void onSaveInstanceState(Bundle outState) { 45 | super.onSaveInstanceState(outState); 46 | outState.putInt(INSTANCE_STORY_ID, this.mStoryId); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/detail/StoryDetailContract.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.detail; 2 | 3 | import org.attentiveness.news.base.BasePresenter; 4 | import org.attentiveness.news.base.BaseView; 5 | import org.attentiveness.news.data.StoryDetail; 6 | 7 | interface StoryDetailContract { 8 | 9 | interface View extends BaseView { 10 | 11 | void showStoryDetail(StoryDetail storyDetail); 12 | 13 | void showError(String message); 14 | 15 | boolean isActive(); 16 | 17 | } 18 | 19 | interface Presenter extends BasePresenter { 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/detail/StoryDetailFragment.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.detail; 2 | 3 | 4 | import android.annotation.SuppressLint; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.webkit.WebSettings; 12 | import android.webkit.WebView; 13 | import android.widget.FrameLayout; 14 | 15 | import org.attentiveness.news.R; 16 | import org.attentiveness.news.base.BaseFragment; 17 | import org.attentiveness.news.data.StoryDetail; 18 | 19 | import java.util.List; 20 | 21 | import butterknife.BindView; 22 | import butterknife.ButterKnife; 23 | 24 | /** 25 | * News Detail Fragment 26 | */ 27 | public class StoryDetailFragment extends BaseFragment implements StoryDetailContract.View { 28 | 29 | @BindView(R.id.fl_root_view) 30 | FrameLayout mRootView; 31 | @BindView(R.id.wv_view) 32 | WebView mWebView; 33 | 34 | private StoryDetailContract.Presenter mPresenter; 35 | 36 | public static StoryDetailFragment newInstance() { 37 | return new StoryDetailFragment(); 38 | } 39 | 40 | public StoryDetailFragment() { 41 | // Required empty public constructor 42 | } 43 | 44 | @Override 45 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 46 | super.onActivityCreated(savedInstanceState); 47 | } 48 | 49 | @SuppressLint("SetJavaScriptEnabled") 50 | @Override 51 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 52 | View view = inflater.inflate(R.layout.fragment_story_detail, container, false); 53 | ButterKnife.bind(this, view); 54 | 55 | WebSettings webSettings = this.mWebView.getSettings(); 56 | webSettings.setJavaScriptEnabled(true); 57 | setHasOptionsMenu(true); 58 | return view; 59 | } 60 | 61 | @Override 62 | public void setPresenter(@NonNull StoryDetailContract.Presenter presenter) { 63 | this.mPresenter = presenter; 64 | } 65 | 66 | @Override 67 | public void onResume() { 68 | super.onResume(); 69 | this.mPresenter.subscribe(); 70 | } 71 | 72 | @Override 73 | public void onPause() { 74 | super.onPause(); 75 | this.mPresenter.unsubscribe(); 76 | } 77 | 78 | @Override 79 | public void showError(String message) { 80 | this.showMessage(this.mRootView, message); 81 | } 82 | 83 | @Override 84 | public void showStoryDetail(StoryDetail storyDetail) { 85 | if (!this.isActive() || getView() == null) { 86 | return; 87 | } 88 | if (storyDetail == null) { 89 | return; 90 | } 91 | String body = storyDetail.getContent(); 92 | List jsArray = storyDetail.getJsList(); 93 | if (jsArray != null && jsArray.size() > 0) { 94 | StringBuilder stringBuilder = new StringBuilder(); 95 | for (int index = 0; index < jsArray.size(); index++) { 96 | String jsLink = ""; 97 | stringBuilder.append(jsLink); 98 | } 99 | body = stringBuilder.toString() + body; 100 | } 101 | List cssArray = storyDetail.getCssList(); 102 | if (cssArray != null && cssArray.size() > 0) { 103 | StringBuilder stringBuilder = new StringBuilder(); 104 | for (int index = 0; index < cssArray.size(); index++) { 105 | String cssLink = ""; 106 | stringBuilder.append(cssLink); 107 | } 108 | body = stringBuilder.toString() + body; 109 | } 110 | this.mWebView.loadData(body, "text/html; charset=UTF-8", null); 111 | } 112 | 113 | @Override 114 | public boolean isActive() { 115 | return isAdded(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/detail/StoryDetailPresenter.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.detail; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import org.attentiveness.news.data.StoryDetail; 6 | import org.attentiveness.news.data.source.StoriesDataRepository; 7 | import org.attentiveness.news.util.schedulers.BaseSchedulerProvider; 8 | import org.attentiveness.news.util.schedulers.SchedulerProvider; 9 | 10 | import io.reactivex.disposables.CompositeDisposable; 11 | import io.reactivex.functions.Consumer; 12 | 13 | class StoryDetailPresenter implements StoryDetailContract.Presenter { 14 | 15 | private int mStoryId; 16 | @NonNull 17 | private StoriesDataRepository mRepository; 18 | @NonNull 19 | private StoryDetailContract.View mView; 20 | private CompositeDisposable mDisposables; 21 | private BaseSchedulerProvider mSchedulerProvider; 22 | 23 | StoryDetailPresenter(int storyId, @NonNull StoriesDataRepository repository, @NonNull StoryDetailContract.View view) { 24 | this.mStoryId = storyId; 25 | this.mRepository = repository; 26 | this.mView = view; 27 | this.mDisposables = new CompositeDisposable(); 28 | this.mSchedulerProvider = SchedulerProvider.getInstance(); 29 | this.mView.setPresenter(this); 30 | } 31 | 32 | @Override 33 | public void subscribe() { 34 | this.requestStoryDetail(this.mStoryId); 35 | } 36 | 37 | @Override 38 | public void unsubscribe() { 39 | this.mDisposables.clear(); 40 | } 41 | 42 | private void requestStoryDetail(int storyId) { 43 | this.mRepository.getStoryDetail(storyId).subscribeOn(this.mSchedulerProvider.io()) 44 | .observeOn(this.mSchedulerProvider.ui()) 45 | .subscribe( 46 | new Consumer() { 47 | @Override 48 | public void accept(@io.reactivex.annotations.NonNull StoryDetail storyDetail) throws Exception { 49 | mView.showStoryDetail(storyDetail); 50 | } 51 | }, 52 | new Consumer() { 53 | @Override 54 | public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception { 55 | mView.showError(throwable.getMessage()); 56 | } 57 | } 58 | ); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/exception/NetworkException.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.exception; 2 | 3 | public class NetworkException extends Exception { 4 | 5 | public NetworkException() { 6 | super(); 7 | } 8 | 9 | public NetworkException(Throwable throwable) { 10 | super(throwable); 11 | } 12 | 13 | public NetworkException(String message) { 14 | super(message); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/LoadMoreListener.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | 4 | import android.support.v7.widget.LinearLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | 7 | abstract class LoadMoreListener extends RecyclerView.OnScrollListener { 8 | 9 | private int mPreviousTotal = 0; 10 | private boolean mLoading = true; 11 | private LinearLayoutManager mLinearLayoutManager; 12 | 13 | LoadMoreListener(LinearLayoutManager linearLayoutManager) { 14 | this.mLinearLayoutManager = linearLayoutManager; 15 | } 16 | 17 | @Override 18 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 19 | super.onScrolled(recyclerView, dx, dy); 20 | 21 | if (dy > 0 && this.mLinearLayoutManager != null) //check for scroll down 22 | { 23 | int visibleItemCount = this.mLinearLayoutManager.getChildCount(); 24 | int totalItemCount = this.mLinearLayoutManager.getItemCount(); 25 | int firstVisibleItem = this.mLinearLayoutManager.findFirstVisibleItemPosition(); 26 | 27 | if (this.mLoading) { 28 | if (totalItemCount > this.mPreviousTotal) { 29 | this.mLoading = false; 30 | this.mPreviousTotal = totalItemCount; 31 | } 32 | } 33 | int visibleThreshold = 1; 34 | if (!this.mLoading && totalItemCount > visibleItemCount 35 | && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 36 | this.onLoadMore(); 37 | this.mLoading = true; 38 | } 39 | } 40 | } 41 | 42 | abstract void onLoadMore(); 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/ScrollChildSwipeRefreshLayout.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | 4 | import android.content.Context; 5 | import android.support.v4.view.ViewCompat; 6 | import android.support.v4.widget.SwipeRefreshLayout; 7 | import android.util.AttributeSet; 8 | import android.view.View; 9 | 10 | public class ScrollChildSwipeRefreshLayout extends SwipeRefreshLayout { 11 | 12 | private View mChildView; 13 | 14 | public ScrollChildSwipeRefreshLayout(Context context) { 15 | super(context); 16 | } 17 | 18 | public ScrollChildSwipeRefreshLayout(Context context, AttributeSet attrs) { 19 | super(context, attrs); 20 | } 21 | 22 | @Override 23 | public boolean canChildScrollUp() { 24 | if (mChildView != null) { 25 | return ViewCompat.canScrollVertically(mChildView, -1); 26 | } 27 | return super.canChildScrollUp(); 28 | } 29 | 30 | public void setChildView(View childView) { 31 | this.mChildView = childView; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/StoryListActivity.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.design.widget.NavigationView; 6 | import android.support.v4.view.GravityCompat; 7 | import android.view.MenuItem; 8 | 9 | import org.attentiveness.news.R; 10 | import org.attentiveness.news.base.BaseActivity; 11 | import org.attentiveness.news.data.source.StoriesDataRepository; 12 | import org.attentiveness.news.data.source.local.LocalStoriesDataSource; 13 | import org.attentiveness.news.data.source.remote.RemoteStoriesDataSource; 14 | import org.attentiveness.news.util.DateUtil; 15 | 16 | import butterknife.ButterKnife; 17 | 18 | public class StoryListActivity extends BaseActivity { 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_story_list); 24 | ButterKnife.bind(this); 25 | setup(R.drawable.ic_menu); 26 | 27 | StoryListFragment newsListFragment = (StoryListFragment) getSupportFragmentManager().findFragmentById(R.id.fl_container); 28 | if (newsListFragment == null) { 29 | String today = DateUtil.getToday(); 30 | newsListFragment = StoryListFragment.newInstance(today); 31 | addFragment(getSupportFragmentManager(), R.id.fl_container, newsListFragment); 32 | } 33 | 34 | StoriesDataRepository repository = StoriesDataRepository.getInstance( 35 | RemoteStoriesDataSource.getInstance(this), LocalStoriesDataSource.getInstance(this)); 36 | StoryListPresenter presenter = new StoryListPresenter(repository, newsListFragment); 37 | } 38 | 39 | @Override 40 | public boolean onOptionsItemSelected(MenuItem item) { 41 | switch (item.getItemId()) { 42 | case android.R.id.home: 43 | // Open the navigation drawer when the home icon is selected from the toolbar. 44 | if (this.mDrawerLayout != null) { 45 | this.mDrawerLayout.openDrawer(GravityCompat.START); 46 | } 47 | return true; 48 | } 49 | return super.onOptionsItemSelected(item); 50 | } 51 | 52 | @Override 53 | protected void setupDrawerContent(NavigationView navigationView) { 54 | navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 55 | @Override 56 | public boolean onNavigationItemSelected(@NonNull MenuItem item) { 57 | int itemId = item.getItemId(); 58 | switch (itemId) { 59 | case R.id.nav_news_list: 60 | //do nothing 61 | break; 62 | case R.id.nav_feedback: 63 | break; 64 | case R.id.nav_about: 65 | break; 66 | case R.id.nav_settings: 67 | break; 68 | default: 69 | break; 70 | } 71 | // Close the navigation drawer when an item is selected. 72 | item.setChecked(true); 73 | if (mDrawerLayout != null) { 74 | mDrawerLayout.closeDrawers(); 75 | } 76 | return true; 77 | } 78 | }); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/StoryListAdapter.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | import android.widget.TextView; 9 | 10 | import com.squareup.picasso.Picasso; 11 | 12 | import org.attentiveness.news.R; 13 | import org.attentiveness.news.data.Story; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | import butterknife.BindView; 19 | import butterknife.ButterKnife; 20 | 21 | class StoryListAdapter extends RecyclerView.Adapter { 22 | 23 | interface OnItemClickListener { 24 | void onStoryClicked(Story story); 25 | } 26 | 27 | private List mStoryList; 28 | private OnItemClickListener mOnItemClickListener; 29 | 30 | StoryListAdapter() { 31 | this.mStoryList = new ArrayList<>(); 32 | } 33 | 34 | @Override 35 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 36 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_story, parent, false); 37 | return new ViewHolder(view); 38 | } 39 | 40 | @Override 41 | public void onBindViewHolder(ViewHolder holder, int position) { 42 | final Story story = this.mStoryList.get(position); 43 | List imageUrlList = story.getImageList(); 44 | String imageUrl = null; 45 | if (imageUrlList != null && imageUrlList.size() > 0) { 46 | imageUrl = imageUrlList.get(0); 47 | } 48 | ImageView imageView = holder.mImageView; 49 | TextView titleView = holder.mTitleView; 50 | titleView.setText(story.getTitle()); 51 | Picasso.with(holder.mImageView.getContext()).load(imageUrl).error(R.mipmap.ic_launcher).into(imageView); 52 | holder.itemView.setOnClickListener(new View.OnClickListener() { 53 | @Override 54 | public void onClick(View v) { 55 | if (mOnItemClickListener != null) { 56 | mOnItemClickListener.onStoryClicked(story); 57 | } 58 | } 59 | }); 60 | } 61 | 62 | @Override 63 | public int getItemCount() { 64 | return this.mStoryList.size(); 65 | } 66 | 67 | void setItemList(List list) { 68 | if (list == null) { 69 | throw new IllegalArgumentException("The arguments must not be null."); 70 | } 71 | this.mStoryList = list; 72 | notifyDataSetChanged(); 73 | } 74 | 75 | void addItemList(List list) { 76 | if (list == null || list.size() == 0) { 77 | return; 78 | } 79 | this.mStoryList.addAll(list); 80 | notifyDataSetChanged(); 81 | } 82 | 83 | void setOnItemClickListener(OnItemClickListener onItemClickListener) { 84 | this.mOnItemClickListener = onItemClickListener; 85 | } 86 | 87 | class ViewHolder extends RecyclerView.ViewHolder { 88 | 89 | @BindView(R.id.iv_img) 90 | ImageView mImageView; 91 | @BindView(R.id.tv_title) 92 | TextView mTitleView; 93 | 94 | ViewHolder(View itemView) { 95 | super(itemView); 96 | ButterKnife.bind(this, itemView); 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/StoryListContract.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import org.attentiveness.news.base.BasePresenter; 6 | import org.attentiveness.news.base.BaseView; 7 | import org.attentiveness.news.data.Story; 8 | 9 | import java.util.List; 10 | 11 | interface StoryListContract { 12 | 13 | interface View extends BaseView { 14 | 15 | void showStoryList(List storyList); 16 | 17 | void appendStoryList(List storyList); 18 | 19 | void hideStoryList(); 20 | 21 | void setLoadingIndicator(boolean active); 22 | 23 | void showRetry(); 24 | 25 | void hideRetry(); 26 | 27 | void showError(String message); 28 | 29 | boolean isActive(); 30 | 31 | } 32 | 33 | interface Presenter extends BasePresenter { 34 | 35 | void loadNewsList(@NonNull String date, boolean forceUpdate, boolean append); 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/StoryListFragment.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.content.ContextCompat; 9 | import android.support.v4.widget.SwipeRefreshLayout; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.TextView; 16 | 17 | import org.attentiveness.news.R; 18 | import org.attentiveness.news.base.BaseFragment; 19 | import org.attentiveness.news.data.Story; 20 | import org.attentiveness.news.detail.StoryDetailActivity; 21 | import org.attentiveness.news.util.DateUtil; 22 | 23 | import java.util.List; 24 | 25 | import butterknife.BindView; 26 | import butterknife.ButterKnife; 27 | import butterknife.OnClick; 28 | 29 | /** 30 | * News list fragment. 31 | */ 32 | public class StoryListFragment extends BaseFragment implements StoryListContract.View, StoryListAdapter.OnItemClickListener { 33 | 34 | public static final String EXTRA_ID = "id"; 35 | public static final String EXTRA_DATE = "date"; 36 | 37 | @BindView(R.id.rv_story_list) 38 | RecyclerView mStoriesView; 39 | @BindView(R.id.rl_swipe_fresh_view) 40 | ScrollChildSwipeRefreshLayout mSwipeRefreshLayout; 41 | @BindView(R.id.tv_loading_error) 42 | TextView mLoadingErrorView; 43 | 44 | private StoryListContract.Presenter mPresenter; 45 | private StoryListAdapter mStoriesAdapter; 46 | private LoadMoreListener mLoadMoreListener; 47 | private String mOriginalDate; 48 | private String mDate; 49 | private int mLoadingCount; 50 | 51 | public static StoryListFragment newInstance(String date) { 52 | StoryListFragment storyListFragment = new StoryListFragment(); 53 | Bundle bundle = new Bundle(); 54 | bundle.putString(EXTRA_DATE, date); 55 | storyListFragment.setArguments(bundle); 56 | return storyListFragment; 57 | } 58 | 59 | public StoryListFragment() { 60 | // Required empty public constructor 61 | } 62 | 63 | @Override 64 | public void onCreate(@Nullable Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | this.mStoriesAdapter = new StoryListAdapter(); 67 | this.mStoriesAdapter.setOnItemClickListener(this); 68 | Bundle bundle = getArguments(); 69 | if (bundle != null && bundle.containsKey(EXTRA_DATE)) { 70 | this.mOriginalDate = bundle.getString(EXTRA_DATE); 71 | } 72 | } 73 | 74 | @Override 75 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 76 | View rootView = inflater.inflate(R.layout.fragment_story_list, container, false); 77 | ButterKnife.bind(this, rootView); 78 | 79 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); 80 | this.mStoriesView.setLayoutManager(linearLayoutManager); 81 | this.mStoriesView.setAdapter(this.mStoriesAdapter); 82 | 83 | this.mLoadingCount = 0; 84 | this.mDate = this.mOriginalDate; 85 | // LogUtil.e(mDate); 86 | this.mLoadMoreListener = new LoadMoreListener(linearLayoutManager) { 87 | 88 | @Override 89 | void onLoadMore() { 90 | mLoadingCount++; 91 | mDate = DateUtil.getDate(mLoadingCount); 92 | mPresenter.loadNewsList(mDate, false, true); 93 | // LogUtil.e(mDate); 94 | } 95 | }; 96 | 97 | this.mSwipeRefreshLayout.setColorSchemeColors( 98 | ContextCompat.getColor(getActivity(), R.color.colorPrimary), 99 | ContextCompat.getColor(getActivity(), R.color.colorAccent), 100 | ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark) 101 | ); 102 | this.mSwipeRefreshLayout.setChildView(this.mStoriesView); 103 | this.mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 104 | @Override 105 | public void onRefresh() { 106 | mLoadingCount = 0; 107 | mDate = mOriginalDate; 108 | mPresenter.loadNewsList(mDate, false, false); 109 | } 110 | }); 111 | 112 | setHasOptionsMenu(true); 113 | return rootView; 114 | } 115 | 116 | @Override 117 | public void setPresenter(@NonNull StoryListContract.Presenter presenter) { 118 | this.mPresenter = presenter; 119 | } 120 | 121 | @Override 122 | public void onResume() { 123 | super.onResume(); 124 | this.mPresenter.subscribe(); 125 | this.mStoriesView.addOnScrollListener(this.mLoadMoreListener); 126 | } 127 | 128 | @Override 129 | public void onPause() { 130 | super.onPause(); 131 | this.mPresenter.unsubscribe(); 132 | this.mStoriesView.removeOnScrollListener(this.mLoadMoreListener); 133 | } 134 | 135 | @Override 136 | public void showRetry() { 137 | this.mLoadingErrorView.setVisibility(View.VISIBLE); 138 | } 139 | 140 | @Override 141 | public void hideRetry() { 142 | this.mLoadingErrorView.setVisibility(View.GONE); 143 | } 144 | 145 | @Override 146 | public void showError(String message) { 147 | showMessage(this.mSwipeRefreshLayout, message); 148 | } 149 | 150 | @Override 151 | public void setLoadingIndicator(final boolean active) { 152 | if (!this.isActive() || getView() == null) { 153 | return; 154 | } 155 | 156 | // Make sure setRefreshing() is called after the layout is done with everything else. 157 | this.mSwipeRefreshLayout.post(new Runnable() { 158 | @Override 159 | public void run() { 160 | mSwipeRefreshLayout.setRefreshing(active); 161 | } 162 | }); 163 | } 164 | 165 | @Override 166 | public void showStoryList(List storyList) { 167 | this.mStoriesView.setVisibility(View.VISIBLE); 168 | this.mStoriesAdapter.setItemList(storyList); 169 | } 170 | 171 | @Override 172 | public void appendStoryList(List storyList) { 173 | this.mStoriesView.setVisibility(View.VISIBLE); 174 | this.mStoriesAdapter.addItemList(storyList); 175 | } 176 | 177 | @Override 178 | public void hideStoryList() { 179 | this.mStoriesView.setVisibility(View.GONE); 180 | } 181 | 182 | @Override 183 | public boolean isActive() { 184 | return isAdded(); 185 | } 186 | 187 | @Override 188 | public void onStoryClicked(Story story) { 189 | if (story != null && story.getId() > 0) { 190 | Intent intent = new Intent(this.getActivity(), StoryDetailActivity.class); 191 | intent.putExtra(EXTRA_ID, story.getId()); 192 | startActivity(intent); 193 | } 194 | } 195 | 196 | @OnClick(R.id.tv_loading_error) 197 | void reload() { 198 | this.mDate = this.mOriginalDate; 199 | this.mLoadingCount = 0; 200 | this.mPresenter.loadNewsList(this.mDate, true, false); 201 | } 202 | 203 | } 204 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/list/StoryListPresenter.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.list; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import org.attentiveness.news.data.Story; 6 | import org.attentiveness.news.data.source.StoriesDataRepository; 7 | import org.attentiveness.news.util.DateUtil; 8 | import org.attentiveness.news.util.schedulers.BaseSchedulerProvider; 9 | import org.attentiveness.news.util.schedulers.SchedulerProvider; 10 | 11 | import java.util.List; 12 | 13 | import io.reactivex.disposables.CompositeDisposable; 14 | import io.reactivex.disposables.Disposable; 15 | import io.reactivex.functions.Action; 16 | import io.reactivex.functions.Consumer; 17 | 18 | class StoryListPresenter implements StoryListContract.Presenter { 19 | 20 | @NonNull 21 | private StoriesDataRepository mRepository; 22 | @NonNull 23 | private StoryListContract.View mNewsListView; 24 | @NonNull 25 | private final BaseSchedulerProvider mSchedulerProvider; 26 | @NonNull 27 | private CompositeDisposable mDisposables; 28 | private boolean mFirstLoad = true; 29 | 30 | StoryListPresenter(@NonNull StoriesDataRepository repository, @NonNull StoryListContract.View view) { 31 | this.mRepository = repository; 32 | this.mNewsListView = view; 33 | this.mSchedulerProvider = SchedulerProvider.getInstance(); 34 | this.mDisposables = new CompositeDisposable(); 35 | this.mNewsListView.setPresenter(this); 36 | } 37 | 38 | @Override 39 | public void subscribe() { 40 | String today = DateUtil.getToday(); 41 | this.loadNewsList(today, false, false); 42 | } 43 | 44 | @Override 45 | public void unsubscribe() { 46 | this.mDisposables.clear(); 47 | } 48 | 49 | @Override 50 | public void loadNewsList(@NonNull String date, boolean forceUpdate, boolean append) { 51 | this.loadNewsList(date, forceUpdate || this.mFirstLoad, true, append); 52 | this.mFirstLoad = false; 53 | } 54 | 55 | private void loadNewsList(@NonNull String date, boolean forceUpdate, final boolean showLoadingUI, final boolean append) { 56 | if (showLoadingUI) { 57 | this.mNewsListView.setLoadingIndicator(true); 58 | } 59 | if (forceUpdate) { 60 | this.mRepository.refreshStories(); 61 | } 62 | Disposable disposable = this.mRepository.getStories(date) 63 | .subscribeOn(this.mSchedulerProvider.computation()) 64 | .observeOn(this.mSchedulerProvider.ui()) 65 | .subscribe( 66 | new Consumer>() { 67 | @Override 68 | public void accept(@io.reactivex.annotations.NonNull List storyList) throws Exception { 69 | if (append) { 70 | mNewsListView.appendStoryList(storyList); 71 | } else { 72 | mNewsListView.showStoryList(storyList); 73 | } 74 | mNewsListView.hideRetry(); 75 | } 76 | }, 77 | new Consumer() { 78 | @Override 79 | public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception { 80 | mNewsListView.showError(throwable.getMessage()); 81 | mNewsListView.showRetry(); 82 | mNewsListView.hideStoryList(); 83 | mNewsListView.setLoadingIndicator(false); 84 | } 85 | }, 86 | new Action() { 87 | @Override 88 | public void run() throws Exception { 89 | mNewsListView.setLoadingIndicator(false); 90 | } 91 | }); 92 | this.mDisposables.add(disposable); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/net/HttpManager.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.net; 2 | 3 | import android.content.Context; 4 | import android.net.ConnectivityManager; 5 | import android.net.NetworkInfo; 6 | 7 | import org.attentiveness.news.R; 8 | import org.attentiveness.news.data.News; 9 | import org.attentiveness.news.data.Story; 10 | import org.attentiveness.news.data.StoryDetail; 11 | import org.attentiveness.news.exception.NetworkException; 12 | 13 | import java.util.List; 14 | import java.util.concurrent.Callable; 15 | 16 | import io.reactivex.Observable; 17 | import io.reactivex.annotations.NonNull; 18 | import io.reactivex.functions.Function; 19 | import okhttp3.OkHttpClient; 20 | import retrofit2.Retrofit; 21 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 22 | import retrofit2.converter.gson.GsonConverterFactory; 23 | 24 | /** 25 | * Http Manager that responds to url request. 26 | */ 27 | public class HttpManager { 28 | 29 | private static final String BASE_URL = "http://news-at.zhihu.com/api/4/news/"; 30 | 31 | private static HttpManager INSTANCE = null; 32 | 33 | private Context mContext; 34 | private StoryService mStoryService; 35 | 36 | public static HttpManager getInstance(Context context) { 37 | if (INSTANCE == null) { 38 | INSTANCE = new HttpManager(context); 39 | } 40 | return INSTANCE; 41 | } 42 | 43 | /** 44 | * Both connection time and read time are 10,000ms by default. 45 | */ 46 | private HttpManager(Context context) { 47 | this.mContext = context; 48 | OkHttpClient client = new OkHttpClient.Builder().build(); 49 | Retrofit retrofit = new Retrofit.Builder() 50 | .client(client) 51 | .baseUrl(BASE_URL) 52 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 53 | .addConverterFactory(GsonConverterFactory.create()) 54 | .build(); 55 | this.mStoryService = retrofit.create(StoryService.class); 56 | } 57 | 58 | public Observable> getStoryList(String date) { 59 | if (!this.isConnected()) { 60 | return Observable.error(new Callable() { 61 | @Override 62 | public Throwable call() { 63 | return new NetworkException(mContext.getResources().getString(R.string.error_no_network_connection)); 64 | } 65 | }); 66 | } 67 | return this.mStoryService.getStoryList(date).map(new Function>() { 68 | @Override 69 | public List apply(@NonNull News news) throws Exception { 70 | return news.getStoryList(); 71 | } 72 | }); 73 | } 74 | 75 | public Observable getStory(int storyId) { 76 | if (!this.isConnected()) { 77 | return Observable.error(new Callable() { 78 | @Override 79 | public Throwable call() { 80 | return new NetworkException(mContext.getResources().getString(R.string.error_no_network_connection)); 81 | } 82 | }); 83 | } 84 | return this.mStoryService.getStoryDetail(storyId); 85 | } 86 | 87 | private boolean isConnected() { 88 | ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 89 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 90 | return networkInfo != null && networkInfo.isConnectedOrConnecting(); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/net/StoryService.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.net; 2 | 3 | import org.attentiveness.news.data.News; 4 | import org.attentiveness.news.data.StoryDetail; 5 | 6 | import io.reactivex.Observable; 7 | import retrofit2.http.GET; 8 | import retrofit2.http.Path; 9 | 10 | public interface StoryService { 11 | 12 | @GET("before/{date}") 13 | Observable getStoryList(@Path("date") String date); 14 | 15 | @GET("{id}") 16 | Observable getStoryDetail(@Path("id") int storyId); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/splash/SplashActivity.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.splash; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.os.CountDownTimer; 6 | import android.support.design.widget.NavigationView; 7 | import android.view.Window; 8 | import android.view.WindowManager; 9 | 10 | import org.attentiveness.news.R; 11 | import org.attentiveness.news.base.BaseActivity; 12 | import org.attentiveness.news.list.StoryListActivity; 13 | 14 | import butterknife.ButterKnife; 15 | 16 | public class SplashActivity extends BaseActivity { 17 | 18 | private CountDownTimer mTimer; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | requestWindowFeature(Window.FEATURE_NO_TITLE); 24 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 25 | setContentView(R.layout.activity_splash); 26 | ButterKnife.bind(this); 27 | 28 | init(); 29 | } 30 | 31 | @Override 32 | protected void setupDrawerContent(NavigationView navigationView) { 33 | //do nothing 34 | } 35 | 36 | private void init() { 37 | mTimer = new CountDownTimer(3000, 1000) { 38 | @Override 39 | public void onTick(long millisUntilFinished) { 40 | 41 | } 42 | 43 | @Override 44 | public void onFinish() { 45 | navigate(); 46 | finish(); 47 | } 48 | }; 49 | mTimer.start(); 50 | } 51 | 52 | private void navigate() { 53 | Intent intent = new Intent(this, StoryListActivity.class); 54 | startActivity(intent); 55 | } 56 | 57 | @Override 58 | protected void onDestroy() { 59 | super.onDestroy(); 60 | if (mTimer != null) { 61 | mTimer.cancel(); 62 | mTimer = null; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/util/DateUtil.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.util; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Calendar; 5 | import java.util.Locale; 6 | 7 | public class DateUtil { 8 | 9 | public static String getToday() { 10 | Calendar calendar = Calendar.getInstance(); 11 | calendar.add(Calendar.DAY_OF_YEAR, 1); 12 | SimpleDateFormat simpleDateFormat = getDataFormat(); 13 | return simpleDateFormat.format(calendar.getTime()); 14 | } 15 | 16 | public static String getDate(int before) { 17 | Calendar calendar = Calendar.getInstance(); 18 | calendar.add(Calendar.DAY_OF_YEAR, 1 - before); 19 | SimpleDateFormat simpleDateFormat = getDataFormat(); 20 | return simpleDateFormat.format(calendar.getTime()); 21 | } 22 | 23 | private static SimpleDateFormat getDataFormat() { 24 | return new SimpleDateFormat("yyyyMMdd", Locale.US); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/util/LogUtil.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.util; 2 | 3 | import android.util.Log; 4 | 5 | public class LogUtil { 6 | 7 | private static boolean mDebug = true; 8 | 9 | public static void d(String message) { 10 | d("News", message); 11 | } 12 | 13 | public static void d(String tag, String message) { 14 | if (mDebug) { 15 | Log.d(tag, message); 16 | } 17 | } 18 | 19 | public static void e(String message) { 20 | e("News", message); 21 | } 22 | 23 | public static void e(String tag, String message) { 24 | if (mDebug) { 25 | Log.e(tag, message); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/util/schedulers/BaseSchedulerProvider.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.util.schedulers; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import io.reactivex.Scheduler; 6 | 7 | 8 | public interface BaseSchedulerProvider { 9 | 10 | @NonNull 11 | Scheduler computation(); 12 | 13 | @NonNull 14 | Scheduler io(); 15 | 16 | @NonNull 17 | Scheduler ui(); 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/org/attentiveness/news/util/schedulers/SchedulerProvider.java: -------------------------------------------------------------------------------- 1 | package org.attentiveness.news.util.schedulers; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.annotation.Nullable; 5 | 6 | import io.reactivex.Scheduler; 7 | import io.reactivex.android.schedulers.AndroidSchedulers; 8 | import io.reactivex.schedulers.Schedulers; 9 | 10 | /** 11 | * Provides different types of schedulers. 12 | */ 13 | public class SchedulerProvider implements BaseSchedulerProvider { 14 | 15 | @Nullable 16 | private static SchedulerProvider INSTANCE; 17 | 18 | // Prevent direct instantiation. 19 | private SchedulerProvider() { 20 | } 21 | 22 | public static synchronized SchedulerProvider getInstance() { 23 | if (INSTANCE == null) { 24 | INSTANCE = new SchedulerProvider(); 25 | } 26 | return INSTANCE; 27 | } 28 | 29 | @Override 30 | @NonNull 31 | public Scheduler computation() { 32 | return Schedulers.computation(); 33 | } 34 | 35 | @Override 36 | @NonNull 37 | public Scheduler io() { 38 | return Schedulers.io(); 39 | } 40 | 41 | @Override 42 | @NonNull 43 | public Scheduler ui() { 44 | return AndroidSchedulers.mainThread(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_about.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_close.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_feedback.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_menu.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_network_error.xml: -------------------------------------------------------------------------------- 1 | 6 | 10 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_settings.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_story_list.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/side_nav_bar.xml: -------------------------------------------------------------------------------- 1 | 3 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_splash.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_story_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_story_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/app_bar.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/app_bar_story_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_story_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_story_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_story_detail.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_story_list.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | 14 | 18 | 19 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_story.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 22 | 23 | 30 | 31 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/src/main/res/layout/nav_header_story_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/menu/activity_story_list_drawer.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 13 | 18 | 23 | 24 | 25 | 28 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/attentiveness/News/d08909814918f0a3609a4f6690ff380e80e13506/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/attentiveness/News/d08909814918f0a3609a4f6690ff380e80e13506/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/attentiveness/News/d08909814918f0a3609a4f6690ff380e80e13506/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/attentiveness/News/d08909814918f0a3609a4f6690ff380e80e13506/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/attentiveness/News/d08909814918f0a3609a4f6690ff380e80e13506/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | > 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #4CAF50 4 | #326917 5 | #FF4081 6 | 7 | #FFFFFF 8 | #E0E0E0 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 160dp 5 | 6 | 16dp 7 | 16dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | News 3 | 4 | Splash 5 | News 6 | Detail 7 | Feedback 8 | About 9 | SettingsActivity 10 | 11 | News 12 | Feedback 13 | About 14 | Settings 15 | 16 | There is no network connection. 17 | 18 | Img 19 | 20 | News 21 | Loading Error 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |