Skip to content

Commit

Permalink
Merge pull request #1 from andriawan24/refactor/project-migration
Browse files Browse the repository at this point in the history
Migrate to AndroidX and cleaned up some codes
  • Loading branch information
Raj-m01 authored Oct 2, 2022
2 parents f4c230f + 2dc5575 commit f7443f4
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 283 deletions.
38 changes: 18 additions & 20 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ plugins {
}
apply plugin: 'kotlin-android'


android {


defaultConfig {

multiDexEnabled true

applicationId "com.example.newsapp"
minSdk 21
targetSdk 31
targetSdk 33
versionCode 1
versionName "1.0"
compileSdk 32
compileSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand All @@ -30,56 +26,58 @@ android {
}

compileOptions {

coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8

}

buildTypes {
release {
debug {
minifyEnabled false
shrinkResources false
debuggable true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

release {
minifyEnabled true
shrinkResources true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

kotlinOptions {
jvmTarget = '1.8'
}

namespace 'com.example.newsapp'
}

dependencies {

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation "androidx.activity:activity-ktx:1.4.0"
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation "androidx.activity:activity-ktx:1.6.0"
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

def room_version = '2.4.2'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

implementation 'com.squareup.picasso:picasso:2.71828'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.facebook.shimmer:shimmer:0.5.0'

}
99 changes: 39 additions & 60 deletions app/src/main/java/com/example/newsapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,26 @@ import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.example.newsapp.adapters.FragmentAdapter
import com.example.newsapp.architecture.NewsViewModel
import com.example.newsapp.utils.Constants.BUSINESS
import com.example.newsapp.utils.Constants.ENTERTAINMENT
import com.example.newsapp.utils.Constants.GENERAL
import com.example.newsapp.utils.Constants.HEALTH
import com.example.newsapp.utils.Constants.HOME
import com.example.newsapp.utils.Constants.SCIENCE
import com.example.newsapp.utils.Constants.SPORTS
import com.example.newsapp.utils.Constants.TECHNOLOGY
import com.example.newsapp.utils.Constants.TOTAL_NEWS_TAB
import com.facebook.shimmer.ShimmerFrameLayout
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
//import com.example.newsapp.BuildConfig


class MainActivity : AppCompatActivity() {

companion object {

var APIRequestError = false
var errorMessage = "error"
const val API_KEY = BuildConfig.API_KEY
const val TOTAL_NEWS_TAB = 7
const val TOP_HEADLINES_COUNT = 5
const val GENERAL = "general"
const val SCIENCE = "science"
const val HEALTH = "health"
const val ENTERTAINMENT = "entertainment"
const val BUSINESS = "business"
const val TECHNOLOGY = "technology"
const val SPORTS = "sports"
const val NEWS_URL = "news url"
const val NEWS_TITLE = "news title"
const val NEWS_IMAGE_URL = "news image url"
const val NEWS_SOURCE = "news source"
const val NEWS_PUBLICATION_TIME = "news publication time"
const val NEWS_DESCRIPTION = "news description"
const val NEWS_CONTENT = "news content"
var generalNews: ArrayList<NewsModel> = ArrayList()
var entertainmentNews: MutableList<NewsModel> = mutableListOf()
var businessNews: MutableList<NewsModel> = mutableListOf()
var healthNews: MutableList<NewsModel> = mutableListOf()
var scienceNews: MutableList<NewsModel> = mutableListOf()
var sportsNews: MutableList<NewsModel> = mutableListOf()
var techNews: MutableList<NewsModel> = mutableListOf()

}

// Tabs Title
// Tabs Title
private val newsCategories = arrayOf(
"Home",
BUSINESS,
ENTERTAINMENT,
SCIENCE,
SPORTS,
TECHNOLOGY,
HEALTH
HOME, BUSINESS,
ENTERTAINMENT, SCIENCE,
SPORTS, TECHNOLOGY, HEALTH
)

private lateinit var viewModel: NewsViewModel
Expand All @@ -77,8 +49,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


//set actionbar
// Set Action Bar
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

Expand All @@ -88,14 +59,14 @@ class MainActivity : AppCompatActivity() {
viewModel = ViewModelProvider(this)[NewsViewModel::class.java]


if(!isNetworkAvailable(applicationContext)){
if (!isNetworkAvailable(applicationContext)) {
shimmerLayout.visibility = View.GONE
val showError: TextView = findViewById(R.id.display_error)
showError.text = getString(R.string.internet_warming)
showError.visibility = View.VISIBLE
}

// send request call for news data
// Send request call for news data
requestNews(GENERAL, generalNews)
requestNews(BUSINESS, businessNews)
requestNews(ENTERTAINMENT, entertainmentNews)
Expand All @@ -112,14 +83,12 @@ class MainActivity : AppCompatActivity() {


private fun requestNews(newsCategory: String, newsData: MutableList<NewsModel>) {

viewModel.getNews(category = newsCategory)?.observe(this) {

newsData.addAll(it)
totalRequestCount += 1

// If main fragment loaded then attach the fragment to viewPager
if (newsCategory == "general") {
if (newsCategory == GENERAL) {
shimmerLayout.stopShimmer()
shimmerLayout.hideShimmer()
shimmerLayout.visibility = View.GONE
Expand All @@ -129,13 +98,11 @@ class MainActivity : AppCompatActivity() {
if (totalRequestCount == TOTAL_NEWS_TAB) {
viewPager.offscreenPageLimit = 7
}

}

}

private fun setViewPager() {
if (!APIRequestError) {
if (!apiRequestError) {
viewPager.visibility = View.VISIBLE
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = newsCategories[position]
Expand All @@ -148,32 +115,33 @@ class MainActivity : AppCompatActivity() {
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {

menuInflater.inflate(R.menu.menu_item_mainactivity, menu)
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {

intent = Intent(applicationContext, SavedNewsActivity::class.java)
startActivity(intent)
return super.onOptionsItemSelected(item)
}

// Check internet connection
private fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

// For 29 api or above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) ?: return false
val capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
?: return false
return when {
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
}
}else {
} else {
// For below 29 api
if (connectivityManager.activeNetworkInfo != null && connectivityManager.activeNetworkInfo!!.isConnectedOrConnecting) {
return true
Expand All @@ -182,4 +150,15 @@ class MainActivity : AppCompatActivity() {
return false
}

companion object {
var generalNews: ArrayList<NewsModel> = ArrayList()
var entertainmentNews: MutableList<NewsModel> = mutableListOf()
var businessNews: MutableList<NewsModel> = mutableListOf()
var healthNews: MutableList<NewsModel> = mutableListOf()
var scienceNews: MutableList<NewsModel> = mutableListOf()
var sportsNews: MutableList<NewsModel> = mutableListOf()
var techNews: MutableList<NewsModel> = mutableListOf()
var apiRequestError = false
var errorMessage = "error"
}
}
38 changes: 24 additions & 14 deletions app/src/main/java/com/example/newsapp/ReadNewsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import com.example.newsapp.architecture.NewsViewModel
import com.example.newsapp.utils.Constants.NEWS_CONTENT
import com.example.newsapp.utils.Constants.NEWS_DESCRIPTION
import com.example.newsapp.utils.Constants.NEWS_IMAGE_URL
import com.example.newsapp.utils.Constants.NEWS_PUBLICATION_TIME
import com.example.newsapp.utils.Constants.NEWS_SOURCE
import com.example.newsapp.utils.Constants.NEWS_TITLE
import com.example.newsapp.utils.Constants.NEWS_URL
import java.util.*


Expand All @@ -40,30 +47,33 @@ class ReadNewsActivity : AppCompatActivity(), TextToSpeech.OnInitListener {

//loading data into list
newsData = ArrayList(1)
val newsUrl = intent.getStringExtra(MainActivity.NEWS_URL)
val newsUrl = intent.getStringExtra(NEWS_URL)
val newsContent =
intent.getStringExtra(MainActivity.NEWS_CONTENT) + ". get paid version to hear full news. "
intent.getStringExtra(NEWS_CONTENT) + ". get paid version to hear full news. "
newsData.add(
NewsModel(
intent.getStringExtra(MainActivity.NEWS_TITLE)!!,
intent.getStringExtra(MainActivity.NEWS_IMAGE_URL),
intent.getStringExtra(MainActivity.NEWS_DESCRIPTION),
intent.getStringExtra(NEWS_TITLE)!!,
intent.getStringExtra(NEWS_IMAGE_URL),
intent.getStringExtra(NEWS_DESCRIPTION),
newsUrl,
intent.getStringExtra(MainActivity.NEWS_SOURCE),
intent.getStringExtra(MainActivity.NEWS_PUBLICATION_TIME),
intent.getStringExtra(NEWS_SOURCE),
intent.getStringExtra(NEWS_PUBLICATION_TIME),
newsContent
)
)

// Webview
newsWebView.settings.domStorageEnabled = true
newsWebView.settings.loadsImagesAutomatically = true
newsWebView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
newsWebView.settings.javaScriptEnabled = true
newsWebView.webViewClient = WebViewClient()

newsWebView.apply {
settings.apply {
domStorageEnabled = true
loadsImagesAutomatically = true
mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
javaScriptEnabled = true
}
webViewClient = WebViewClient()
webChromeClient = WebChromeClient()
}

newsWebView.webChromeClient = WebChromeClient()

if (newsUrl != null) {
newsWebView.loadUrl(newsUrl)
Expand Down
Loading

0 comments on commit f7443f4

Please sign in to comment.