Initial commit
1
app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
91
app/build.gradle
Normal file
@@ -0,0 +1,91 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "29.0.3"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.h_mal.flavourednewsapp"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 30
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
buildConfigField "String", "ParamOne", "${paramOne}"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
// Specifies one flavor dimension.
|
||||
flavorDimensions "version"
|
||||
productFlavors {
|
||||
purple {
|
||||
// Assigns this product flavor to the "version" flavor dimension.
|
||||
// If you are using only one dimension, this property is optional,
|
||||
// and the plugin automatically assigns all the module's flavors to
|
||||
// that dimension.
|
||||
applicationId "com.example.h_mal.flavourednewsapp.purple"
|
||||
// versionNameSuffix "-purple"
|
||||
}
|
||||
stock {
|
||||
applicationId "com.example.h_mal.flavourednewsapp.stock"
|
||||
// versionNameSuffix "-stock"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
||||
implementation 'androidx.cardview:cardview:1.0.0'
|
||||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
|
||||
// android unit testing and espresso
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
implementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
//mock websever for testing retrofit responses
|
||||
testImplementation "com.squareup.okhttp3:mockwebserver:4.6.0"
|
||||
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
|
||||
|
||||
//mockito and livedata testing
|
||||
testImplementation 'org.mockito:mockito-inline:2.13.0'
|
||||
implementation 'android.arch.core:core-testing'
|
||||
androidTestImplementation 'androidx.test:rules:1.3.0-rc01'
|
||||
|
||||
//Retrofit and GSON
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:4.0.0'
|
||||
|
||||
// Shared prefs
|
||||
implementation "androidx.preference:preference-ktx:1.1.1"
|
||||
|
||||
// Picasso image display
|
||||
implementation 'com.squareup.picasso:picasso:2.71828'
|
||||
|
||||
//Android Room
|
||||
def room_version = "2.2.5"
|
||||
implementation "androidx.room:room-runtime:$room_version"
|
||||
annotationProcessor "androidx.room:room-compiler:$room_version"
|
||||
|
||||
// Dagger 2 dependency injection
|
||||
implementation 'com.google.dagger:dagger:2.27'
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
|
||||
}
|
||||
21
app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.h_mal.flavourednewsapp;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
assertEquals("com.example.h_mal.flavourednewsapp", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
25
app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.h_mal.flavourednewsapp">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
android:name=".app.FlavouredNewsAppClass">
|
||||
<activity android:name=".ui.main.MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.h_mal.flavourednewsapp.app;
|
||||
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainActivity;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Component;
|
||||
|
||||
/**
|
||||
* create dagger2 interface for dependency injection
|
||||
* define Context module to be used later in dependency injection
|
||||
*/
|
||||
@Singleton
|
||||
@Component(modules = {ContextModule.class, RetrofitModule.class, RoomModule.class})
|
||||
public interface ApplicationComponent {
|
||||
|
||||
void inject(MainActivity mainActivity);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.h_mal.flavourednewsapp.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
/*
|
||||
* Module used for injecting context in ResourcesFile class
|
||||
*/
|
||||
@Module
|
||||
class ContextModule {
|
||||
|
||||
private Context context;
|
||||
|
||||
public ContextModule(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Provides
|
||||
Context provideContext() {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.h_mal.flavourednewsapp.app;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
public class FlavouredNewsAppClass extends Application {
|
||||
|
||||
public ApplicationComponent appComponent;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
// Create dagger2 component to be used in the application
|
||||
ContextModule contextModule = new ContextModule(getApplicationContext());
|
||||
|
||||
appComponent = DaggerApplicationComponent.builder()
|
||||
.contextModule(contextModule)
|
||||
.retrofitModule(new RetrofitModule())
|
||||
.roomModule(new RoomModule(contextModule.provideContext()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example.h_mal.flavourednewsapp.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.api.NewsApi;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.api.interceptors.NetworkConnectionInterceptor;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.api.interceptors.QueryInterceptor;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
@Module
|
||||
public class RetrofitModule {
|
||||
|
||||
@Provides
|
||||
NewsApi getApiInterface(Retrofit retroFit) {
|
||||
return retroFit.create(NewsApi.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
Retrofit getRetrofit(OkHttpClient okHttpClient) {
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl("https://newsapi.org/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(okHttpClient)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides
|
||||
OkHttpClient getOkHttpClient(
|
||||
HttpLoggingInterceptor httpLoggingInterceptor,
|
||||
NetworkConnectionInterceptor networkConnectionInterceptor,
|
||||
QueryInterceptor queryInterceptor
|
||||
) {
|
||||
return new OkHttpClient.Builder()
|
||||
.addNetworkInterceptor(networkConnectionInterceptor)
|
||||
.addInterceptor(queryInterceptor)
|
||||
.addInterceptor(httpLoggingInterceptor)
|
||||
.readTimeout(5 * 60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides
|
||||
NetworkConnectionInterceptor getNetworkInterceptor(Context context) {
|
||||
return new NetworkConnectionInterceptor(context);
|
||||
}
|
||||
|
||||
@Provides
|
||||
QueryInterceptor getQueryInterceptor() {
|
||||
return new QueryInterceptor();
|
||||
}
|
||||
|
||||
@Provides
|
||||
HttpLoggingInterceptor getHttpLoggingInterceptor() {
|
||||
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
|
||||
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
return httpLoggingInterceptor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.example.h_mal.flavourednewsapp.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.room.AppDatabase;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class RoomModule {
|
||||
|
||||
AppDatabase appDatabase;
|
||||
|
||||
public RoomModule(Context context) {
|
||||
appDatabase = AppDatabase.getInstance(context);
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
AppDatabase providesAppDatabase() {
|
||||
return appDatabase;
|
||||
}
|
||||
|
||||
// @Singleton
|
||||
// @Provides
|
||||
// ProductDao providesProductDao(DemoDatabase demoDatabase) {
|
||||
// return demoDatabase.getProductDao();
|
||||
// }
|
||||
//
|
||||
// @Singleton
|
||||
// @Provides
|
||||
// ProductRepository productRepository(ProductDao productDao) {
|
||||
// return new ProductDataSource(productDao);
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.network;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import retrofit2.Response;
|
||||
|
||||
public abstract class ResponseHandler {
|
||||
|
||||
public ResponseHandler() {
|
||||
}
|
||||
|
||||
public <T> T unwrapResponse(Response<T> response) throws IOException {
|
||||
if (response.isSuccessful()) {
|
||||
return response.body();
|
||||
} else {
|
||||
String error = response.errorBody().string();
|
||||
int code = response.code();
|
||||
String errorMessage;
|
||||
try {
|
||||
errorMessage = new JSONObject(error).getString("error_message");
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
errorMessage = "Error Code " + code;
|
||||
}
|
||||
|
||||
throw new IOException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.network.api;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
public interface NewsApi {
|
||||
|
||||
@GET("v2/everything")
|
||||
Call<NewsResponse> getNewsFromApi(@Query("q") String query);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.network.api.interceptors;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class NetworkConnectionInterceptor implements Interceptor {
|
||||
Context context;
|
||||
|
||||
@Inject
|
||||
public NetworkConnectionInterceptor(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
if (!isInternetAvailable()){
|
||||
throw new IOException("Make sure you have an active data connection");
|
||||
}
|
||||
return chain.proceed(chain.request());
|
||||
}
|
||||
|
||||
private Boolean isInternetAvailable(){
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivityManager == null){
|
||||
return false;
|
||||
}
|
||||
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
||||
if (networkInfo == null){
|
||||
return false;
|
||||
}
|
||||
return networkInfo.isConnected();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.network.api.interceptors;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.BuildConfig;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class QueryInterceptor implements Interceptor {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request original = chain.request();
|
||||
HttpUrl originalHttpUrl = original.url();
|
||||
|
||||
HttpUrl url = originalHttpUrl.newBuilder()
|
||||
.addQueryParameter("apiKey", BuildConfig.ParamOne)
|
||||
.build();
|
||||
|
||||
// // Request customization: add request headers
|
||||
Request.Builder requestBuilder = original.newBuilder().url(url);
|
||||
Request request= requestBuilder.build();
|
||||
return chain.proceed(request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
package com.example.h_mal.flavourednewsapp.data.network.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class Article {
|
||||
|
||||
@SerializedName("source")
|
||||
@Expose
|
||||
public Source source;
|
||||
@SerializedName("author")
|
||||
@Expose
|
||||
public String author;
|
||||
@SerializedName("title")
|
||||
@Expose
|
||||
public String title;
|
||||
@SerializedName("description")
|
||||
@Expose
|
||||
public String description;
|
||||
@SerializedName("url")
|
||||
@Expose
|
||||
public String url;
|
||||
@SerializedName("urlToImage")
|
||||
@Expose
|
||||
public String urlToImage;
|
||||
@SerializedName("publishedAt")
|
||||
@Expose
|
||||
public String publishedAt;
|
||||
@SerializedName("content")
|
||||
@Expose
|
||||
public String content;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
package com.example.h_mal.flavourednewsapp.data.network.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NewsResponse {
|
||||
|
||||
@SerializedName("status")
|
||||
@Expose
|
||||
public String status;
|
||||
@SerializedName("totalResults")
|
||||
@Expose
|
||||
public Integer totalResults;
|
||||
@SerializedName("articles")
|
||||
@Expose
|
||||
public List<Article> articles = null;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
package com.example.h_mal.flavourednewsapp.data.network.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class Source {
|
||||
|
||||
@SerializedName("id")
|
||||
@Expose
|
||||
public String id;
|
||||
@SerializedName("name")
|
||||
@Expose
|
||||
public String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
public class PreferenceProvider {
|
||||
private final static String LAST_SAVED = "late_saved";
|
||||
private final static String NEWS_SAVED = "news_saved";
|
||||
SharedPreferences preference;
|
||||
|
||||
@Inject
|
||||
public PreferenceProvider(Context context) {
|
||||
preference = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
public void saveLastSavedAt(String user, Long savedAt) {
|
||||
preference.edit().putString(
|
||||
NEWS_SAVED,
|
||||
user
|
||||
).putLong(
|
||||
LAST_SAVED,
|
||||
savedAt
|
||||
).apply();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Long getLastSavedAt(String news){
|
||||
|
||||
String savedUser = preference.getString(NEWS_SAVED, null);
|
||||
if (savedUser == null){
|
||||
return null;
|
||||
}
|
||||
if (savedUser.equals(news)){
|
||||
return preference.getLong(LAST_SAVED, 1595076034403L);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.repositories;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.Article;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Repository {
|
||||
|
||||
LiveData<List<NewsEntity>> getNewsFromDatabase();
|
||||
LiveData<NewsEntity> getSingleNewsFromDatabase(String url);
|
||||
void saveNewsToDatabase(List<Article> news);
|
||||
void getNewsFromApi(String searchTerm, RepositoryImpl.AsyncTaskResultListener<NewsResponse> asyncTaskResultListener);
|
||||
// NewsResponse getNewsFromApi(String searchTerm) throws IOException;
|
||||
void saveCurrentSearchToPrefs(String searchTerm);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.repositories;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.ResponseHandler;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.api.NewsApi;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.Article;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
import com.example.h_mal.flavourednewsapp.data.preferences.PreferenceProvider;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.AppDatabase;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class RepositoryImpl extends ResponseHandler implements Repository{
|
||||
private static final int MILLISECONDS_ONE_MIN = 60000;
|
||||
|
||||
private NewsApi api;
|
||||
private AppDatabase database;
|
||||
private PreferenceProvider preference;
|
||||
|
||||
@Inject
|
||||
public RepositoryImpl(NewsApi api, AppDatabase database, PreferenceProvider preference) {
|
||||
this.api = api;
|
||||
this.database = database;
|
||||
this.preference = preference;
|
||||
}
|
||||
|
||||
// Current list of news in the database
|
||||
@Override
|
||||
public LiveData<List<NewsEntity>> getNewsFromDatabase() {
|
||||
return database.getNewsDao().getAllUsers();
|
||||
}
|
||||
|
||||
// retrieving a single news article from an unique url
|
||||
@Override
|
||||
public LiveData<NewsEntity> getSingleNewsFromDatabase(String url) {
|
||||
return database.getNewsDao().getUser(url);
|
||||
}
|
||||
|
||||
// save a list of news to the room database
|
||||
@Override
|
||||
public void saveNewsToDatabase(List<Article> news) {
|
||||
List<NewsEntity> newsEntities = new ArrayList<>();
|
||||
for (Article article : news){
|
||||
newsEntities.add(new NewsEntity(article));
|
||||
}
|
||||
database.getNewsDao().upsertNewUsers(newsEntities);
|
||||
}
|
||||
|
||||
// fetch news from an api call
|
||||
@Override
|
||||
public void getNewsFromApi(String searchTerm, AsyncTaskResultListener<NewsResponse> asyncTaskResultListener){
|
||||
if (isSearchValid(searchTerm)) {
|
||||
api.getNewsFromApi(searchTerm).enqueue(new Callback<NewsResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
|
||||
Gson gson = new Gson();
|
||||
|
||||
Log.i("ApiResponse", gson.toJson(response.body()));
|
||||
asyncTaskResultListener.onSuccess(response.body());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<NewsResponse> call, Throwable t) {
|
||||
asyncTaskResultListener.onFailed(t.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveCurrentSearchToPrefs(String searchTerm) {
|
||||
Long time = System.currentTimeMillis();
|
||||
preference.saveLastSavedAt(searchTerm, time);
|
||||
}
|
||||
|
||||
// boolean response of validity of search
|
||||
// if the same search is taking place again with a minute return false
|
||||
private Boolean isSearchValid(String searchTerm){
|
||||
Long time = preference.getLastSavedAt(searchTerm);
|
||||
if (time == null){
|
||||
return true;
|
||||
}
|
||||
Long currentTime = System.currentTimeMillis();
|
||||
long difference = currentTime - time;
|
||||
|
||||
return difference > MILLISECONDS_ONE_MIN;
|
||||
}
|
||||
|
||||
public interface AsyncTaskResultListener <T>{
|
||||
void onSuccess(T value);
|
||||
void onFailed(String error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.room;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
|
||||
@Database(entities = NewsEntity.class, version = 1)
|
||||
public abstract class AppDatabase extends RoomDatabase {
|
||||
|
||||
private static AppDatabase instance;
|
||||
|
||||
public static synchronized AppDatabase getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
instance = createDatabase(context);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static AppDatabase createDatabase(Context context) {
|
||||
return Room.databaseBuilder(
|
||||
context.getApplicationContext(),
|
||||
AppDatabase.class,
|
||||
"MyDatabase.db"
|
||||
).build();
|
||||
}
|
||||
|
||||
public abstract NewsDao getNewsDao();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.room;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Transaction;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public abstract class NewsDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
public abstract void saveAllUsers(List<NewsEntity> news);
|
||||
|
||||
@Query("SELECT * FROM News")
|
||||
public abstract LiveData<List<NewsEntity>> getAllUsers();
|
||||
|
||||
// clear database and add new entries
|
||||
@Transaction
|
||||
public void upsertNewUsers(List<NewsEntity> news){
|
||||
deleteEntries();
|
||||
saveAllUsers(news);
|
||||
}
|
||||
|
||||
@Query("DELETE FROM News")
|
||||
public abstract void deleteEntries();
|
||||
|
||||
@Query("SELECT * FROM News WHERE url = :url")
|
||||
public abstract LiveData<NewsEntity> getUser(String url);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.example.h_mal.flavourednewsapp.data.room.entities;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.Article;
|
||||
|
||||
|
||||
@Entity(tableName = "News")
|
||||
public class NewsEntity {
|
||||
|
||||
public String author;
|
||||
public String title;
|
||||
public String description;
|
||||
@NonNull
|
||||
@PrimaryKey(autoGenerate = false)
|
||||
public String url;
|
||||
public String urlToImage;
|
||||
public String publishedAt;
|
||||
public String content;
|
||||
|
||||
public NewsEntity(String author, String title, String description, @NonNull String url, String urlToImage, String publishedAt, String content) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.url = url;
|
||||
this.urlToImage = urlToImage;
|
||||
this.publishedAt = publishedAt;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public NewsEntity(Article article) {
|
||||
this.author = article.author;
|
||||
this.title = article.title;
|
||||
this.description = article.description;
|
||||
this.url = article.url;
|
||||
this.urlToImage = article.urlToImage;
|
||||
this.publishedAt = article.publishedAt;
|
||||
this.content = article.content;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUrlToImage() {
|
||||
return urlToImage;
|
||||
}
|
||||
|
||||
public void setUrlToImage(String urlToImage) {
|
||||
this.urlToImage = urlToImage;
|
||||
}
|
||||
|
||||
public String getPublishedAt() {
|
||||
return publishedAt;
|
||||
}
|
||||
|
||||
public void setPublishedAt(String publishedAt) {
|
||||
this.publishedAt = publishedAt;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.R;
|
||||
import com.example.h_mal.flavourednewsapp.app.FlavouredNewsAppClass;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.home.MainFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static com.example.h_mal.flavourednewsapp.utils.ViewUtils.displayToast;
|
||||
import static com.example.h_mal.flavourednewsapp.utils.ViewUtils.hide;
|
||||
import static com.example.h_mal.flavourednewsapp.utils.ViewUtils.show;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
@Inject
|
||||
MainViewModelFactory mainViewModelFactory;
|
||||
|
||||
public MainViewModel mViewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main_activity);
|
||||
ProgressBar progressBar = findViewById(R.id.progress_circular);
|
||||
|
||||
// Retrieve Dagger2 component from Application class
|
||||
((FlavouredNewsAppClass) getApplication()).appComponent.inject(this);
|
||||
// Create viewmodel
|
||||
mViewModel = new ViewModelProvider(this, mainViewModelFactory).get(MainViewModel.class);
|
||||
|
||||
// Observe operation state to display progress bar
|
||||
mViewModel.operationState.observe(this, aBoolean -> {
|
||||
if (aBoolean){
|
||||
show(progressBar);
|
||||
}else {
|
||||
hide(progressBar);
|
||||
}
|
||||
});
|
||||
|
||||
// Display a toast error if no internet
|
||||
mViewModel.operationError.observe(this, stringEvent -> {
|
||||
String mes = stringEvent.getContentIfNotHandled();
|
||||
if (mes != null){
|
||||
displayToast(this, mes);
|
||||
}
|
||||
});
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.container, MainFragment.newInstance())
|
||||
.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.Article;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
import com.example.h_mal.flavourednewsapp.data.repositories.RepositoryImpl;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
import com.example.h_mal.flavourednewsapp.utils.Event;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
|
||||
public class MainViewModel extends ViewModel {
|
||||
@NonNull
|
||||
private RepositoryImpl repository;
|
||||
|
||||
public MainViewModel(@NonNull RepositoryImpl repository) {
|
||||
this.repository = repository;
|
||||
|
||||
}
|
||||
|
||||
// livedata for user items in room database
|
||||
public LiveData<List<NewsEntity>> getNewsLiveData(){
|
||||
return repository.getNewsFromDatabase();
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> operationState = new MutableLiveData<>();
|
||||
MutableLiveData<Event<String>> operationError = new MutableLiveData<>();
|
||||
|
||||
public void getNews(String searchTerm){
|
||||
// validate that search term is not empty
|
||||
if (searchTerm.isEmpty()){
|
||||
operationError.postValue(new Event<String>("Enter a valid username"));
|
||||
return;
|
||||
}
|
||||
|
||||
repository.getNewsFromApi(searchTerm, new RepositoryImpl.AsyncTaskResultListener<NewsResponse>() {
|
||||
@Override
|
||||
public void onSuccess(NewsResponse value) {
|
||||
List<Article> articles = value.articles;
|
||||
if (!articles.isEmpty()) {
|
||||
// save news articles to database
|
||||
saveResultsToDatabase(articles);
|
||||
// save last search term
|
||||
repository.saveCurrentSearchToPrefs(searchTerm);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed(String error) {
|
||||
operationError.postValue(new Event<String>(error));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void saveResultsToDatabase(List<Article> articles){
|
||||
Executors.newSingleThreadScheduledExecutor().submit(() -> repository.saveNewsToDatabase(articles));
|
||||
}
|
||||
|
||||
|
||||
public LiveData<NewsEntity> getSingleNews(String url){
|
||||
return repository.getSingleNewsFromDatabase(url);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.repositories.RepositoryImpl;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
class MainViewModelFactory implements ViewModelProvider.Factory {
|
||||
RepositoryImpl repository;
|
||||
|
||||
@Inject
|
||||
public MainViewModelFactory(RepositoryImpl repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
@SuppressWarnings("UNCHECKED_CAST")
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
return (T) new MainViewModel(repository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main.home;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.SearchView;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.R;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainActivity;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainViewModel;
|
||||
|
||||
public class MainFragment extends Fragment implements SearchView.OnQueryTextListener {
|
||||
|
||||
private MainViewModel mViewModel;
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
|
||||
public static MainFragment newInstance() {
|
||||
return new MainFragment();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.main_fragment, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
// setup menu for searching
|
||||
setHasOptionsMenu(true);
|
||||
recyclerView = view.findViewById(R.id.recycler_view);
|
||||
mViewModel = ((MainActivity) requireActivity()).mViewModel;
|
||||
|
||||
// observe livedata to populate recycler view of articles
|
||||
mViewModel.getNewsLiveData().observe(getViewLifecycleOwner(), newsEntities ->
|
||||
recyclerView.setAdapter(
|
||||
new NewsRecyclerAdapter((MainActivity) requireActivity(), newsEntities)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
inflater.inflate(R.menu.menu, menu);
|
||||
|
||||
// Setup search bar in fragment
|
||||
MenuItem filter = menu.findItem(R.id.app_bar_search);
|
||||
SearchView searchView = (SearchView) filter.getActionView();
|
||||
searchView.setOnQueryTextListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(String newText) {
|
||||
if (newText.length() >= 3){
|
||||
mViewModel.getNews(newText);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main.home;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.cardview.widget.CardView;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.R;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainActivity;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.overview.OverviewFragment;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.example.h_mal.flavourednewsapp.utils.DateUtils.dateStringOnly;
|
||||
|
||||
public class NewsRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
MainActivity mainActivity;
|
||||
List<NewsEntity> newsList;
|
||||
|
||||
public NewsRecyclerAdapter(MainActivity mainActivity, List<NewsEntity> newsList) {
|
||||
this.mainActivity = mainActivity;
|
||||
this.newsList = newsList;
|
||||
}
|
||||
|
||||
public void updateList(List<NewsEntity> newsList) {
|
||||
this.newsList.clear();
|
||||
this.newsList.addAll(newsList);
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
if (newsList.isEmpty()) {
|
||||
View EmptyView = LayoutInflater.from(mainActivity).inflate(R.layout.empty_list_cell, parent, false);
|
||||
return new RecyclerView.ViewHolder(EmptyView) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
};
|
||||
} else {
|
||||
View itemOne = LayoutInflater.from(mainActivity).inflate(R.layout.news_item_cell, parent, false);
|
||||
return new ItemOne(itemOne);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
||||
if (holder instanceof ItemOne) {
|
||||
NewsEntity currentNews = newsList.get(position);
|
||||
((ItemOne) holder).bindView(currentNews);
|
||||
((ItemOne) holder).buttonOpen.setOnClickListener(v -> {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentNews.url));
|
||||
mainActivity.startActivity(intent);
|
||||
});
|
||||
((ItemOne) holder).buttonShare.setOnClickListener(v -> {
|
||||
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
||||
shareIntent.setType("text/plain");
|
||||
shareIntent.putExtra(Intent.EXTRA_SUBJECT, currentNews.title);
|
||||
mainActivity.startActivity(Intent.createChooser(shareIntent, "choose one"));
|
||||
});
|
||||
holder.itemView.setOnClickListener(view ->
|
||||
mainActivity.getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.container, OverviewFragment.newInstance(currentNews.url))
|
||||
.addToBackStack("details")
|
||||
.commit()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
if (newsList.isEmpty()) {
|
||||
return 1;
|
||||
} else {
|
||||
return newsList.size();
|
||||
}
|
||||
}
|
||||
|
||||
static class ItemOne extends RecyclerView.ViewHolder {
|
||||
TextView dateTv;
|
||||
TextView titleTv;
|
||||
TextView authorTv;
|
||||
ImageView cellImg;
|
||||
CardView buttonShare;
|
||||
CardView buttonOpen;
|
||||
|
||||
public ItemOne(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
dateTv = itemView.findViewById(R.id.card_date);
|
||||
titleTv = itemView.findViewById(R.id.card_title);
|
||||
authorTv = itemView.findViewById(R.id.card_author);
|
||||
buttonShare = itemView.findViewById(R.id.button_share);
|
||||
buttonOpen = itemView.findViewById(R.id.button_open);
|
||||
cellImg = itemView.findViewById(R.id.cell_image_view);
|
||||
}
|
||||
|
||||
public void bindView(NewsEntity news) {
|
||||
String date = dateStringOnly(news.publishedAt);
|
||||
dateTv.setText(date);
|
||||
titleTv.setText(news.title);
|
||||
authorTv.setText(news.author);
|
||||
if (news.urlToImage != null && !news.urlToImage.isEmpty()) {
|
||||
Picasso.get().load(news.urlToImage).into(cellImg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main.overview;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.R;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainActivity;
|
||||
import com.example.h_mal.flavourednewsapp.ui.main.MainViewModel;
|
||||
|
||||
|
||||
public class OverviewFragment extends Fragment {
|
||||
private static final String ARG_PARAM1 = "param1";
|
||||
|
||||
private MainViewModel mViewModel;
|
||||
|
||||
private String mUrl;
|
||||
|
||||
public OverviewFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this factory method to create a new instance of
|
||||
* this fragment using the provided parameters.
|
||||
*
|
||||
* @param url Parameter 1.
|
||||
* @return A new instance of fragment OverviewFragment.
|
||||
*/
|
||||
public static OverviewFragment newInstance(String url) {
|
||||
OverviewFragment fragment = new OverviewFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putString(ARG_PARAM1, url);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (getArguments() != null) {
|
||||
mUrl = getArguments().getString(ARG_PARAM1);
|
||||
}
|
||||
mViewModel = ((MainActivity) requireActivity()).mViewModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_overview, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
RecyclerView overviewRecycler = view.findViewById(R.id.overview_recycler_view);
|
||||
|
||||
mViewModel.getSingleNews(mUrl).observe(getViewLifecycleOwner(), newsEntity ->
|
||||
overviewRecycler.setAdapter( new OverviewRecyclerAdapter(requireContext(), newsEntity))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.example.h_mal.flavourednewsapp.ui.main.overview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Pair;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.R;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.entities.NewsEntity;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
class OverviewRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
Context context;
|
||||
NewsEntity news;
|
||||
|
||||
|
||||
public OverviewRecyclerAdapter(Context context, NewsEntity news) {
|
||||
this.context = context;
|
||||
this.news = news;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
if (viewType == 1) {
|
||||
View itemOne = LayoutInflater.from(context).inflate(R.layout.item_one_layout, parent, false);
|
||||
return new ItemOne(itemOne);
|
||||
}else {
|
||||
View itemTwo = LayoutInflater.from(context).inflate(R.layout.item_two_layout, parent, false);
|
||||
return new ItemTwo(itemTwo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
||||
if (holder instanceof ItemOne){
|
||||
((ItemOne) holder).bindView(news);
|
||||
}else {
|
||||
((ItemTwo) holder).bindView(getItemDetails(position));
|
||||
}
|
||||
}
|
||||
|
||||
private Pair<String, String> getItemDetails(int position){
|
||||
switch (position){
|
||||
case 1:
|
||||
return new Pair("Title", news.getTitle());
|
||||
case 2:
|
||||
return new Pair("Content", news.getContent());
|
||||
case 3:
|
||||
return new Pair("Description", news.getDescription());
|
||||
case 4:
|
||||
return new Pair("Author", news.getAuthor());
|
||||
case 5:
|
||||
return new Pair("Url", news.getUrl());
|
||||
default:
|
||||
return new Pair("", "");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 6;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
if (position == 0) {
|
||||
return 1;
|
||||
}else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
static class ItemOne extends RecyclerView.ViewHolder {
|
||||
ImageView cellImageView;
|
||||
|
||||
public ItemOne(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
cellImageView = itemView.findViewById(R.id.image_header);
|
||||
}
|
||||
|
||||
public void bindView(NewsEntity news){
|
||||
if (news.urlToImage != null && !news.urlToImage.isEmpty()){
|
||||
Picasso.get().load(news.urlToImage).into(cellImageView);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class ItemTwo extends RecyclerView.ViewHolder {
|
||||
TextView top;
|
||||
TextView bottom;
|
||||
|
||||
public ItemTwo(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
top = itemView.findViewById(android.R.id.text1);
|
||||
bottom = itemView.findViewById(android.R.id.text2);
|
||||
}
|
||||
|
||||
public void bindView(Pair<String, String> entry){
|
||||
top.setText(entry.first);
|
||||
bottom.setText(entry.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.h_mal.flavourednewsapp.utils;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static String dateStringOnly(String date){
|
||||
try{
|
||||
return date.split("T")[0];
|
||||
}catch (Exception e){
|
||||
return date;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.h_mal.flavourednewsapp.utils;
|
||||
|
||||
|
||||
public class Event<T> {
|
||||
private T content;
|
||||
private Boolean hasBeenHandled = false;
|
||||
|
||||
public Event(T content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public T getContentIfNotHandled(){
|
||||
if (hasBeenHandled) {
|
||||
return null;
|
||||
} else {
|
||||
hasBeenHandled = true;
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.h_mal.flavourednewsapp.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class ViewUtils {
|
||||
|
||||
public static void hide(View view){
|
||||
view.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
public static void show(View view){
|
||||
view.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
public static void displayToast(Context context, String message){
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
30
app/src/main/res/drawable-v24/ic_launcher_foreground.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
BIN
app/src/main/res/drawable/dims.jpg
Normal file
|
After Width: | Height: | Size: 362 KiB |
170
app/src/main/res/drawable/ic_launcher_background.xml
Normal file
@@ -0,0 +1,170 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
</vector>
|
||||
14
app/src/main/res/layout/fragment_overview.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
tools:context="com.example.h_mal.flavourednewsapp.ui.main.overview.OverviewFragment">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/overview_recycler_view"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</FrameLayout>
|
||||
14
app/src/main/res/layout/item_one_layout.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<ImageView
|
||||
android:id="@+id/image_header"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="240dp"
|
||||
tools:src="@drawable/dims"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_marginBottom="8dp"/>
|
||||
|
||||
</FrameLayout>
|
||||
33
app/src/main/res/layout/item_two_layout.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
|
||||
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
|
||||
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
|
||||
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:baselineAligned="false"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView android:id="@android:id/text1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:singleLine="true"
|
||||
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
|
||||
android:ellipsize="marquee"
|
||||
tools:text="asdasdas"/>
|
||||
|
||||
<TextView android:id="@android:id/text2"
|
||||
android:layout_alignLeft="@android:id/title"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
android:autoLink="web"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="asdasdas"/>
|
||||
|
||||
</LinearLayout>
|
||||
21
app/src/main/res/layout/main_activity.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.main.MainActivity">
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_circular"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
17
app/src/main/res/layout/main_fragment.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.main.home.MainFragment">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/empty_list_cell"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
14
app/src/main/res/menu/menu.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/app_bar_search"
|
||||
android:icon="@android:drawable/ic_menu_search"
|
||||
app:actionViewClass="androidx.appcompat.widget.SearchView"
|
||||
android:title="Filter"
|
||||
android:focusable="true"
|
||||
android:focusableInTouchMode="true"
|
||||
app:showAsAction="always" />
|
||||
|
||||
</menu>
|
||||
5
app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
5
app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
BIN
app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
BIN
app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
BIN
app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 7.3 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 16 KiB |
5
app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<string name="app_name">Flavoured News App</string>
|
||||
<!-- TODO: Remove or change this placeholder text -->
|
||||
<string name="hello_blank_fragment">Hello blank fragment</string>
|
||||
</resources>
|
||||
22
app/src/main/res/values/styles.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<resources>
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
<style name="small_header_text" parent="TextAppearance.AppCompat">
|
||||
<item name="android:gravity">center</item>
|
||||
<item name="android:textSize">14sp</item>
|
||||
<item name="android:textStyle">bold</item>
|
||||
<item name="android:layout_margin">4dp</item>
|
||||
</style>
|
||||
|
||||
<style name="test_item_title" parent="TextAppearance.AppCompat">
|
||||
<item name="android:textSize">22sp</item>
|
||||
<item name="android:textStyle">bold</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
48
app/src/purple/res/layout/empty_list_cell.xml
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="24dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_marginRight="24dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
app:cardBackgroundColor="#FFEEBA"
|
||||
app:cardCornerRadius="24dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:text="No news articles"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:text="Check connection and search to display results"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
147
app/src/purple/res/layout/news_item_cell.xml
Normal file
@@ -0,0 +1,147 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="270dp">
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/card_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="16dp"
|
||||
app:cardBackgroundColor="@color/colorPrimary"
|
||||
app:cardCornerRadius="12dp">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/cell_image_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="270dp"
|
||||
android:layout_gravity="center"
|
||||
android:adjustViewBounds="false"
|
||||
android:scaleType="centerCrop"
|
||||
tools:src="@drawable/dims"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/cell_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="90dp"
|
||||
android:background="@color/cell_bottom"
|
||||
android:orientation="horizontal"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent">
|
||||
<LinearLayout
|
||||
android:id="@+id/buttons_container"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="16dp"
|
||||
app:layout_constraintBottom_toBottomOf="@id/cell_layout"
|
||||
app:layout_constraintTop_toTopOf="@id/cell_layout"
|
||||
app:layout_constraintRight_toRightOf="@id/cell_layout"
|
||||
android:orientation="vertical">
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/button_share"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:cardBackgroundColor="@android:color/white"
|
||||
app:cardCornerRadius="20dp"
|
||||
app:contentPaddingLeft="12dp"
|
||||
app:contentPaddingRight="12dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:cardPreventCornerOverlap="false"
|
||||
android:layout_margin="3dp"
|
||||
app:cardElevation="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="@id/cell_layout"
|
||||
app:layout_constraintTop_toTopOf="@id/cell_layout"
|
||||
app:layout_constraintRight_toRightOf="@id/cell_layout">
|
||||
<TextView
|
||||
android:id="@+id/submission_button_label"
|
||||
style="@style/small_header_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Share"
|
||||
android:textColor="@color/cell_bottom" />
|
||||
</androidx.cardview.widget.CardView>
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/button_open"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:cardBackgroundColor="@android:color/white"
|
||||
app:cardCornerRadius="20dp"
|
||||
android:layout_margin="3dp"
|
||||
app:contentPaddingLeft="12dp"
|
||||
app:contentPaddingRight="12dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:cardPreventCornerOverlap="false"
|
||||
app:cardElevation="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="@id/cell_layout"
|
||||
app:layout_constraintTop_toTopOf="@id/cell_layout"
|
||||
app:layout_constraintRight_toRightOf="@id/cell_layout">
|
||||
<TextView
|
||||
android:id="@+id/submission_button_label_2"
|
||||
style="@style/small_header_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="Open"
|
||||
android:textColor="@color/cell_bottom" />
|
||||
</androidx.cardview.widget.CardView>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="8dp"
|
||||
app:layout_constraintTop_toTopOf="@id/cell_layout"
|
||||
app:layout_constraintBottom_toBottomOf="@id/cell_layout"
|
||||
app:layout_constraintLeft_toLeftOf="@id/cell_layout"
|
||||
app:layout_constraintRight_toLeftOf="@id/buttons_container">
|
||||
<TextView
|
||||
android:id="@+id/card_date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@android:color/white"
|
||||
android:alpha="0.6"
|
||||
tools:text="THURSDAY 23 JUL 2020"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/card_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textStyle="bold"
|
||||
android:textSize="15sp"
|
||||
style="@style/test_item_title"
|
||||
android:textColor="@android:color/white"
|
||||
tools:text="Lone Florida Teen Charged in the Single Worst Hack in Twitter's History"/>
|
||||
<TextView
|
||||
android:id="@+id/card_author"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textStyle="bold"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@android:color/white"
|
||||
android:alpha="0.6"
|
||||
tools:text="feedback@businessinsider.com (Emily Graffeo), Emily Graffeo"/>
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
</FrameLayout>
|
||||
7
app/src/purple/res/values/colors.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#6200EE</color>
|
||||
<color name="colorPrimaryDark">#3700B3</color>
|
||||
<color name="colorAccent">#03DAC5</color>
|
||||
<color name="cell_bottom">#8C5DFF</color>
|
||||
</resources>
|
||||
4
app/src/purple/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Purple News</string>
|
||||
</resources>
|
||||
37
app/src/stock/res/layout/empty_list_cell.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="24dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_marginRight="24dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="No news articles"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:text="Check connection and search to display results" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
104
app/src/stock/res/layout/news_item_cell.xml
Normal file
@@ -0,0 +1,104 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/cell_image_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="180dp"
|
||||
android:layout_gravity="center"
|
||||
android:adjustViewBounds="false"
|
||||
android:scaleType="centerCrop"
|
||||
|
||||
tools:src="@drawable/dims" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/card_date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:alpha="0.6"
|
||||
android:textSize="12sp"
|
||||
tools:text="THURSDAY 23 JUL 2020" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/card_title"
|
||||
style="@style/test_item_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:textSize="15sp"
|
||||
android:textStyle="bold"
|
||||
tools:text="Lone Florida Teen Charged in the Single Worst Hack in Twitter's History" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/card_author"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:alpha="0.6"
|
||||
android:textSize="12sp"
|
||||
android:textStyle="bold"
|
||||
tools:text="feedback@businessinsider.com (Emily Graffeo), Emily Graffeo" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/buttons_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/button_share"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="3dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_weight="1"
|
||||
app:cardBackgroundColor="@android:color/white"
|
||||
app:cardCornerRadius="20dp"
|
||||
app:cardElevation="0dp"
|
||||
app:cardPreventCornerOverlap="false"
|
||||
app:contentPaddingLeft="12dp"
|
||||
app:contentPaddingRight="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/submission_button_label"
|
||||
style="@style/small_header_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Share" />
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@+id/button_open"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="3dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_weight="1"
|
||||
app:cardBackgroundColor="@android:color/white"
|
||||
app:cardCornerRadius="20dp"
|
||||
app:cardElevation="0dp"
|
||||
app:cardPreventCornerOverlap="false"
|
||||
app:contentPaddingLeft="12dp"
|
||||
app:contentPaddingRight="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/submission_button_label_2"
|
||||
style="@style/small_header_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="Open" />
|
||||
</androidx.cardview.widget.CardView>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
7
app/src/stock/res/values/colors.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#7e8287</color>
|
||||
<color name="colorPrimaryDark">#54494b</color>
|
||||
<color name="colorAccent">#9da39a</color>
|
||||
|
||||
</resources>
|
||||
4
app/src/stock/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Stock News</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.h_mal.flavourednewsapp;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.h_mal.flavourednewsapp.repositories;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Request;
|
||||
import okio.Timeout;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class MockCallable <T> implements Call<T> {
|
||||
final Class<T> typeParameterClass;
|
||||
|
||||
public MockCallable(Class<T> typeParameterClass) {
|
||||
this.typeParameterClass = typeParameterClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<T> execute() throws IOException {
|
||||
T mockResponse = mock(typeParameterClass);
|
||||
return Response.success(mockResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(Callback<T> callback) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExecuted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCanceled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Call<T> clone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Request request() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timeout timeout() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.example.h_mal.flavourednewsapp.repositories;
|
||||
|
||||
import com.example.h_mal.flavourednewsapp.data.network.api.NewsApi;
|
||||
import com.example.h_mal.flavourednewsapp.data.network.model.NewsResponse;
|
||||
import com.example.h_mal.flavourednewsapp.data.preferences.PreferenceProvider;
|
||||
import com.example.h_mal.flavourednewsapp.data.repositories.RepositoryImpl;
|
||||
import com.example.h_mal.flavourednewsapp.data.room.AppDatabase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Request;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Timeout;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class RepositoryImplTest {
|
||||
|
||||
@Mock
|
||||
NewsApi api;
|
||||
@Mock
|
||||
AppDatabase db;
|
||||
@Mock
|
||||
PreferenceProvider prefs;
|
||||
|
||||
RepositoryImpl repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
repository = new RepositoryImpl(api, db, prefs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchUserFromApi_positiveResponse() {
|
||||
// GIVEN
|
||||
String input = "12345";
|
||||
NewsResponse mockApiResponse = mock(NewsResponse.class);
|
||||
Call<NewsResponse> mockResponse = new MockCallable<>(NewsResponse.class);
|
||||
|
||||
// WHEN
|
||||
Mockito.when(api.getNewsFromApi(input)).thenReturn(mockResponse);
|
||||
Mockito.when(prefs.getLastSavedAt(input)).thenReturn(null);
|
||||
|
||||
// THEN
|
||||
repository.getNewsFromApi(input, new RepositoryImpl.AsyncTaskResultListener<NewsResponse>() {
|
||||
@Override
|
||||
public void onSuccess(NewsResponse value) {
|
||||
assertEquals(mockApiResponse, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed(String error) { }
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchUserFromApi_negativeResponse() throws IOException {
|
||||
// GIVEN
|
||||
String input = "12345";
|
||||
String errorString = "error";
|
||||
Call<NewsResponse> mockResponse = new Call<NewsResponse>(){
|
||||
@Override
|
||||
public Response<NewsResponse> execute() throws IOException {
|
||||
throw new IOException(errorString);
|
||||
}
|
||||
@Override
|
||||
public void enqueue(Callback<NewsResponse> callback) { }
|
||||
|
||||
@Override
|
||||
public boolean isExecuted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() { }
|
||||
|
||||
@Override
|
||||
public boolean isCanceled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Call<NewsResponse> clone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Request request() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timeout timeout() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// WHEN
|
||||
Mockito.when(api.getNewsFromApi(input)).thenReturn(mockResponse);
|
||||
Mockito.when(prefs.getLastSavedAt(input)).thenReturn(null);
|
||||
|
||||
// THEN
|
||||
repository.getNewsFromApi(input, new RepositoryImpl.AsyncTaskResultListener<NewsResponse>() {
|
||||
@Override
|
||||
public void onSuccess(NewsResponse value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed(String error) {
|
||||
assertEquals(error, errorString);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||