Skip to content

Commit

Permalink
Added Project
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshAggarwal1 committed Jan 15, 2024
0 parents commit 16437fa
Show file tree
Hide file tree
Showing 55 changed files with 1,569 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
78 changes: 78 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
plugins {
id("com.android.application")
id("com.google.dagger.hilt.android")
}
android {
namespace = "com.harsh.newsapp2"
compileSdk = 34

defaultConfig {
applicationId = "com.harsh.newsapp2"
minSdk = 30
targetSdk = 33
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

// enable data-binding
buildFeatures {
dataBinding = true
viewBinding = true
}
}

dependencies {

implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
// RxJava3 with retrofit
implementation("com.squareup.retrofit2:adapter-rxjava3:2.9.0")

// Paging Library
val pagingVersion = "3.2.1"
implementation("androidx.paging:paging-runtime:$pagingVersion")
// optional - RxJava3 support
implementation("androidx.paging:paging-rxjava3:$pagingVersion")

// Dagger
implementation("com.google.dagger:hilt-android:2.50")
annotationProcessor("com.google.dagger:hilt-compiler:2.50")

// Glide
implementation("com.github.bumptech.glide:glide:4.16.0")
annotationProcessor("com.github.bumptech.glide:compiler:4.16.0")

// Lifecycle
val lifecycleVersion = "2.7.0"
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion")
// ViewModel utilities for Compose
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion")
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.harsh.newsapp2;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* 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.harsh.newsapp2", appContext.getPackageName());
}
}
29 changes: 29 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".NewsApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NewsApp2"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
69 changes: 69 additions & 0 deletions app/src/main/java/com/harsh/newsapp2/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.harsh.newsapp2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;

import com.bumptech.glide.RequestManager;
import com.harsh.newsapp2.adapter.NewsAdapter;
import com.harsh.newsapp2.adapter.NewsLoadStateAdapter;
import com.harsh.newsapp2.databinding.ActivityMainBinding;
import com.harsh.newsapp2.util.GridSpace;
import com.harsh.newsapp2.util.NewsComparator;
import com.harsh.newsapp2.viewmodel.NewsViewModel;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.rxjava3.disposables.Disposable;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {

NewsViewModel mainActivityViewModel;
ActivityMainBinding activityMainBinding;
NewsAdapter newsAdapter;

@Inject
RequestManager requestManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(activityMainBinding.getRoot());

newsAdapter = new NewsAdapter(new NewsComparator(), requestManager);
mainActivityViewModel = new ViewModelProvider(this).get(NewsViewModel.class);

initRecyclerViewAdapter();

// subscribe to paging
Disposable subscribeResult = mainActivityViewModel.newsPagingDataFlowable.subscribe(newsPagingData -> {
newsAdapter.submitData(getLifecycle(), newsPagingData);
}
);
}

private void initRecyclerViewAdapter() {
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
activityMainBinding.recyclerViewNews.setLayoutManager(gridLayoutManager);
activityMainBinding.recyclerViewNews.addItemDecoration(new GridSpace(2, 20, true));
activityMainBinding.recyclerViewNews.setAdapter(
newsAdapter.withLoadStateFooter(
new NewsLoadStateAdapter(view -> {
newsAdapter.retry();
}
)
)
);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return newsAdapter.getItemViewType(position) == NewsAdapter.LOADING_ITEM ? 1 : 2;
}
});
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/harsh/newsapp2/NewsApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.harsh.newsapp2;

import android.app.Application;

import dagger.hilt.android.HiltAndroidApp;

@HiltAndroidApp
public class NewsApp extends Application {
}
61 changes: 61 additions & 0 deletions app/src/main/java/com/harsh/newsapp2/adapter/NewsAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.harsh.newsapp2.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.RequestManager;
import com.harsh.newsapp2.databinding.SingleNewsItemBinding;
import com.harsh.newsapp2.model.News;

public class NewsAdapter extends PagingDataAdapter<News, NewsAdapter.NewsViewHolder> {

public static final int LOADING_ITEM = 0;
public static final int MOVIE_ITEM = 0;

RequestManager glide;

public NewsAdapter(@NonNull DiffUtil.ItemCallback<News> diffCallback, RequestManager glide) {
super(diffCallback);
this.glide = glide;
}

@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new NewsViewHolder(SingleNewsItemBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false
));
}

@Override
public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
News news = getItem(position);
if (news != null) {
Log.d("URL", news.getUrlToImage());
glide.load(news.getUrlToImage()).
into(holder.newsItemBinding.imageViewNews);
holder.newsItemBinding.textViewTitle.setText(news.getTitle().substring(0, 6));
}
}

@Override
public int getItemViewType(int position) {
return position == getItemCount() ? LOADING_ITEM : MOVIE_ITEM;
}

public static class NewsViewHolder extends RecyclerView.ViewHolder {
SingleNewsItemBinding newsItemBinding;
public NewsViewHolder(@NonNull SingleNewsItemBinding newsItemBinding) {
super(newsItemBinding.getRoot());
this.newsItemBinding = newsItemBinding;
}
}
}
Loading

0 comments on commit 16437fa

Please sign in to comment.