diff --git a/app/build.gradle b/app/build.gradle index 93904bb..16f2ce4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" @@ -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' - } diff --git a/app/src/main/java/com/example/newsapp/MainActivity.kt b/app/src/main/java/com/example/newsapp/MainActivity.kt index 405b9f2..66da6ad 100644 --- a/app/src/main/java/com/example/newsapp/MainActivity.kt +++ b/app/src/main/java/com/example/newsapp/MainActivity.kt @@ -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 = ArrayList() - var entertainmentNews: MutableList = mutableListOf() - var businessNews: MutableList = mutableListOf() - var healthNews: MutableList = mutableListOf() - var scienceNews: MutableList = mutableListOf() - var sportsNews: MutableList = mutableListOf() - var techNews: MutableList = 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 @@ -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) @@ -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) @@ -112,14 +83,12 @@ class MainActivity : AppCompatActivity() { private fun requestNews(newsCategory: String, newsData: MutableList) { - 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 @@ -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] @@ -148,13 +115,11 @@ 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) @@ -162,18 +127,21 @@ class MainActivity : AppCompatActivity() { // 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 @@ -182,4 +150,15 @@ class MainActivity : AppCompatActivity() { return false } + companion object { + var generalNews: ArrayList = ArrayList() + var entertainmentNews: MutableList = mutableListOf() + var businessNews: MutableList = mutableListOf() + var healthNews: MutableList = mutableListOf() + var scienceNews: MutableList = mutableListOf() + var sportsNews: MutableList = mutableListOf() + var techNews: MutableList = mutableListOf() + var apiRequestError = false + var errorMessage = "error" + } } diff --git a/app/src/main/java/com/example/newsapp/ReadNewsActivity.kt b/app/src/main/java/com/example/newsapp/ReadNewsActivity.kt index 93f920e..1ba6448 100644 --- a/app/src/main/java/com/example/newsapp/ReadNewsActivity.kt +++ b/app/src/main/java/com/example/newsapp/ReadNewsActivity.kt @@ -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.* @@ -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) diff --git a/app/src/main/java/com/example/newsapp/SavedNewsActivity.kt b/app/src/main/java/com/example/newsapp/SavedNewsActivity.kt index 490db38..f20c157 100644 --- a/app/src/main/java/com/example/newsapp/SavedNewsActivity.kt +++ b/app/src/main/java/com/example/newsapp/SavedNewsActivity.kt @@ -15,6 +15,13 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.newsapp.adapters.CustomAdapter 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 class SavedNewsActivity : AppCompatActivity() { @@ -40,7 +47,7 @@ class SavedNewsActivity : AppCompatActivity() { viewModel = ViewModelProvider(this)[NewsViewModel::class.java] -// Get Saved News + // Get Saved News viewModel.getNewsFromDB(context = applicationContext)?.observe(this) { newsData.clear() newsData.addAll(it) @@ -50,56 +57,53 @@ class SavedNewsActivity : AppCompatActivity() { adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { - val intent = Intent(this@SavedNewsActivity, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) - startActivity(intent) + val intent = Intent(this@SavedNewsActivity, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } + startActivity(intent) } }) adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { override fun onItemLongClick(position: Int) { -// delete news + // Delete saved news dialog recyclerView.findViewHolderForAdapterPosition(position)?.itemView?.setBackgroundColor( getThemeColor(R.attr.colorPrimaryVariant) ) - val alertBuilder: AlertDialog.Builder = AlertDialog.Builder(this@SavedNewsActivity) - alertBuilder.setMessage("Delete this News?") - alertBuilder.setTitle("Alert!") - alertBuilder.setCancelable(false) - - alertBuilder.setPositiveButton( - "Yes" - ) { dialog, which -> - this@SavedNewsActivity.let { - viewModel.deleteNews( - it, - news = newsData[position] + val alertDialog = AlertDialog.Builder(this@SavedNewsActivity).apply { + setMessage("Delete this News?") + setTitle("Alert!") + setCancelable(false) + + setPositiveButton( + "Yes" + ) { _, _ -> + this@SavedNewsActivity.let { + viewModel.deleteNews( + it, + news = newsData[position] + ) + } + adapter.notifyItemRemoved(position) + Toast.makeText(this@SavedNewsActivity, "Deleted!", Toast.LENGTH_SHORT).show() + } + + setNegativeButton("No") { _, _ -> + recyclerView.findViewHolderForAdapterPosition(position)?.itemView?.setBackgroundColor( + getThemeColor(R.attr.colorPrimary) ) } - adapter.notifyItemRemoved(position) - Toast.makeText(this@SavedNewsActivity, "Deleted!", Toast.LENGTH_SHORT) - .show() - } - alertBuilder.setNegativeButton( - "No" - ) { dialog, which -> - recyclerView.findViewHolderForAdapterPosition(position)?.itemView?.setBackgroundColor( - getThemeColor(R.attr.colorPrimary) - ) - } + }.create() - val alertDialog = alertBuilder.create() alertDialog.show() - - } }) diff --git a/app/src/main/java/com/example/newsapp/adapters/CustomAdapter.kt b/app/src/main/java/com/example/newsapp/adapters/CustomAdapter.kt index b0d70fa..9bece9b 100644 --- a/app/src/main/java/com/example/newsapp/adapters/CustomAdapter.kt +++ b/app/src/main/java/com/example/newsapp/adapters/CustomAdapter.kt @@ -28,7 +28,6 @@ class CustomAdapter(private var newsList: List) : } interface OnItemClickListener { - fun onItemClick(position: Int) } @@ -44,16 +43,13 @@ class CustomAdapter(private var newsList: List) : mClickListener = listener } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false) context = parent.context return ViewHolder(view, mClickListener, mLongClickListener) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val newsData = newsList[holder.adapterPosition] holder.headLine.text = newsData.headLine @@ -62,7 +58,7 @@ class CustomAdapter(private var newsList: List) : if (imgUrl.isNullOrEmpty()) { Picasso.get() - .load(R.drawable.samplenews) + .load( R.drawable.samplenews) .fit() .centerCrop() .into(holder.image) @@ -75,10 +71,10 @@ class CustomAdapter(private var newsList: List) : .into(holder.image) } - if(context.toString().contains("SavedNews")){ - val date = " "+time?.substring(0,time.indexOf('T',0)) + if (context.toString().contains("SavedNews")) { + val date = " " + time?.substring(0, time.indexOf('T', 0)) holder.newsPublicationTime.text = date - }else { + } else { val currentTimeInHours = Instant.now().atZone(ZoneId.of("Asia/Kolkata")) val newsTimeInHours = Instant.parse(time).atZone(ZoneId.of("Asia/Kolkata")) val hoursDifference = Duration.between(currentTimeInHours, newsTimeInHours) @@ -96,11 +92,7 @@ class CustomAdapter(private var newsList: List) : ItemView: View, listener: OnItemClickListener, listener2: OnItemLongClickListener - ) : RecyclerView.ViewHolder( - ItemView - ) { - - + ) : RecyclerView.ViewHolder(ItemView) { val image: ImageView = itemView.findViewById(R.id.img) val headLine: TextView = itemView.findViewById(R.id.news_title) val newsPublicationTime: TextView = itemView.findViewById(R.id.news_publication_time) @@ -114,10 +106,8 @@ class CustomAdapter(private var newsList: List) : listener2.onItemLongClick(adapterPosition) return@setOnLongClickListener true } - - } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt b/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt index 607c21e..cafb06f 100644 --- a/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt +++ b/app/src/main/java/com/example/newsapp/adapters/CustomAdapterForTopHeadlines.kt @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView import com.example.newsapp.MainActivity import com.example.newsapp.NewsModel import com.example.newsapp.R +import com.example.newsapp.utils.Constants.TOP_HEADLINES_COUNT import com.squareup.picasso.Picasso @@ -51,7 +52,7 @@ class CustomAdapterForTopHeadlines(private var newsList: List) : override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val pos = position % MainActivity.TOP_HEADLINES_COUNT + val pos = position % TOP_HEADLINES_COUNT val newsData: NewsModel = newsList[pos] holder.headLine.text = newsData.headLine diff --git a/app/src/main/java/com/example/newsapp/adapters/FragmentAdapter.kt b/app/src/main/java/com/example/newsapp/adapters/FragmentAdapter.kt index 7767e46..aede78b 100644 --- a/app/src/main/java/com/example/newsapp/adapters/FragmentAdapter.kt +++ b/app/src/main/java/com/example/newsapp/adapters/FragmentAdapter.kt @@ -6,10 +6,11 @@ import androidx.lifecycle.Lifecycle import androidx.viewpager2.adapter.FragmentStateAdapter import com.example.newsapp.MainActivity import com.example.newsapp.fragmentClasses.* +import com.example.newsapp.utils.Constants.TOTAL_NEWS_TAB class FragmentAdapter(fm: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fm, lifecycle){ - override fun getItemCount(): Int = MainActivity.TOTAL_NEWS_TAB + override fun getItemCount(): Int = TOTAL_NEWS_TAB override fun createFragment(position: Int): Fragment { diff --git a/app/src/main/java/com/example/newsapp/architecture/NewsDao.kt b/app/src/main/java/com/example/newsapp/architecture/NewsDao.kt index 998d5de..f5e3b5f 100644 --- a/app/src/main/java/com/example/newsapp/architecture/NewsDao.kt +++ b/app/src/main/java/com/example/newsapp/architecture/NewsDao.kt @@ -4,7 +4,6 @@ import androidx.lifecycle.LiveData import androidx.room.* import com.example.newsapp.NewsModel - @Dao interface NewsDao { @@ -14,9 +13,6 @@ interface NewsDao { @Query("SELECT * FROM News_Table") fun getNewsFromDatabase(): LiveData> - @Delete fun deleteNews(news: NewsModel) - - -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/newsapp/architecture/NewsDatabase.kt b/app/src/main/java/com/example/newsapp/architecture/NewsDatabase.kt index a169d0d..727dbd0 100644 --- a/app/src/main/java/com/example/newsapp/architecture/NewsDatabase.kt +++ b/app/src/main/java/com/example/newsapp/architecture/NewsDatabase.kt @@ -5,6 +5,7 @@ import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase import com.example.newsapp.NewsModel +import com.example.newsapp.utils.Constants.DATABASE_NAME @Database(entities = [NewsModel::class], version = 3, exportSchema = false) abstract class NewsDatabase : RoomDatabase() { @@ -16,19 +17,18 @@ abstract class NewsDatabase : RoomDatabase() { @Volatile private var INSTANCE: NewsDatabase? = null - fun getDataseClient(context: Context): NewsDatabase { + fun getDatabaseClient(context: Context): NewsDatabase { if (INSTANCE != null) return INSTANCE!! synchronized(this) { INSTANCE = Room - .databaseBuilder(context, NewsDatabase::class.java, "LOGIN_DATABASE") + .databaseBuilder(context, NewsDatabase::class.java, DATABASE_NAME) .fallbackToDestructiveMigration() .build() return INSTANCE!! - } } diff --git a/app/src/main/java/com/example/newsapp/architecture/NewsRepository.kt b/app/src/main/java/com/example/newsapp/architecture/NewsRepository.kt index c844442..4f4f51b 100644 --- a/app/src/main/java/com/example/newsapp/architecture/NewsRepository.kt +++ b/app/src/main/java/com/example/newsapp/architecture/NewsRepository.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import com.example.newsapp.BuildConfig import com.example.newsapp.MainActivity import com.example.newsapp.NewsModel import com.example.newsapp.retrofit.NewsApi @@ -25,7 +26,7 @@ class NewsRepository { private var newsDatabase: NewsDatabase? = null private fun initializeDB(context: Context): NewsDatabase { - return NewsDatabase.getDataseClient(context) + return NewsDatabase.getDatabaseClient(context) } fun insertNews(context: Context, news: NewsModel) { @@ -58,7 +59,7 @@ class NewsRepository { val newsList = MutableLiveData>() val call = RetrofitHelper.getInstance().create(NewsApi::class.java) - .getNews("in", category, MainActivity.API_KEY) //put your api key here + .getNews("in", category, BuildConfig.API_KEY) //put your api key here call.enqueue(object : Callback { override fun onResponse( @@ -96,7 +97,7 @@ class NewsRepository { try { jsonObj = response.errorBody()?.string()?.let { JSONObject(it) } if (jsonObj != null) { - MainActivity.APIRequestError = true + MainActivity.apiRequestError = true MainActivity.errorMessage = jsonObj.getString("message") val tempNewsList = mutableListOf() newsList.value = tempNewsList @@ -110,7 +111,7 @@ class NewsRepository { override fun onFailure(call: Call, t: Throwable) { - MainActivity.APIRequestError = true + MainActivity.apiRequestError = true MainActivity.errorMessage = t.localizedMessage as String Log.d("err_msg", "msg" + t.localizedMessage) } diff --git a/app/src/main/java/com/example/newsapp/architecture/NewsViewModel.kt b/app/src/main/java/com/example/newsapp/architecture/NewsViewModel.kt index 9704d80..8c5cb5e 100644 --- a/app/src/main/java/com/example/newsapp/architecture/NewsViewModel.kt +++ b/app/src/main/java/com/example/newsapp/architecture/NewsViewModel.kt @@ -6,12 +6,10 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.example.newsapp.NewsModel - class NewsViewModel : ViewModel() { private var newsLiveData: MutableLiveData>? = null - //get news from API fun getNews(category: String?): MutableLiveData>? { @@ -20,7 +18,6 @@ class NewsViewModel : ViewModel() { return newsLiveData } - var newsData: LiveData>? = null fun insertNews(context: Context, news: NewsModel) { @@ -35,6 +32,4 @@ class NewsViewModel : ViewModel() { newsData = NewsRepository.getAllNews(context) return newsData } - - -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/BusinessFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/BusinessFragment.kt index 158d9b5..fa2e27e 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/BusinessFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/BusinessFragment.kt @@ -13,6 +13,13 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class BusinessFragment : Fragment() { @@ -30,25 +37,26 @@ class BusinessFragment : Fragment() { recyclerView.adapter = adapter adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { + override fun onItemClick(position: Int) { + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) startActivity(intent) } }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/EntertainmentFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/EntertainmentFragment.kt index 4d3b0a9..a479d4d 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/EntertainmentFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/EntertainmentFragment.kt @@ -13,11 +13,17 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class EntertainmentFragment : Fragment() { - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -34,22 +40,23 @@ class EntertainmentFragment : Fragment() { adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } + startActivity(intent) } }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt index 4c47ff5..e8ba344 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/GeneralFragment.kt @@ -16,6 +16,16 @@ import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter import com.example.newsapp.adapters.CustomAdapterForTopHeadlines +import com.example.newsapp.utils.Constants.DEFAULT_SWIPER_DELAY +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 com.example.newsapp.utils.Constants.TOP_HEADLINES_COUNT +import com.example.newsapp.utils.Constants.INITIAL_POSITION class GeneralFragment : Fragment() { @@ -27,15 +37,13 @@ class GeneralFragment : Fragment() { private lateinit var adapter: CustomAdapter private lateinit var newsDataForTopHeadlines: List private lateinit var newsDataForDown: List - var position = 0 + var position = INITIAL_POSITION override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - val view = inflater.inflate(R.layout.fragment_general, container, false) - val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) val layoutManagerTop = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) @@ -45,10 +53,8 @@ class GeneralFragment : Fragment() { recyclerViewTop.layoutManager = layoutManagerTop // Setting recyclerViews adapter - newsDataForTopHeadlines = - MainActivity.generalNews.slice(0 until MainActivity.TOP_HEADLINES_COUNT) - newsDataForDown = - MainActivity.generalNews.slice(MainActivity.TOP_HEADLINES_COUNT until MainActivity.generalNews.size - MainActivity.TOP_HEADLINES_COUNT) + newsDataForTopHeadlines = MainActivity.generalNews.slice(0 until TOP_HEADLINES_COUNT) + newsDataForDown = MainActivity.generalNews.slice(TOP_HEADLINES_COUNT until MainActivity.generalNews.size - TOP_HEADLINES_COUNT) topAdapter = CustomAdapterForTopHeadlines(newsDataForTopHeadlines) adapter = CustomAdapter(newsDataForDown) recyclerViewTop.adapter = topAdapter @@ -60,7 +66,7 @@ class GeneralFragment : Fragment() { override fun run() { recyclerViewTop.smoothScrollToPosition(position) position++ - mainHandler.postDelayed(this, 4000) + mainHandler.postDelayed(this, DEFAULT_SWIPER_DELAY) } } @@ -86,17 +92,16 @@ class GeneralFragment : Fragment() { // listitem onClick adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsDataForDown[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsDataForDown[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsDataForDown[position].image) - intent.putExtra( - MainActivity.NEWS_DESCRIPTION, - newsDataForDown[position].description - ) - intent.putExtra(MainActivity.NEWS_SOURCE, newsDataForDown[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsDataForDown[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsDataForDown[position].content) + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsDataForDown[position].url) + putExtra(NEWS_TITLE, newsDataForDown[position].headLine) + putExtra(NEWS_IMAGE_URL, newsDataForDown[position].image) + putExtra(NEWS_DESCRIPTION, newsDataForDown[position].description) + putExtra(NEWS_SOURCE, newsDataForDown[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsDataForDown[position].time) + putExtra(NEWS_CONTENT, newsDataForDown[position].content) + } + startActivity(intent) } }) @@ -105,49 +110,43 @@ class GeneralFragment : Fragment() { topAdapter.setOnItemClickListener(object : CustomAdapterForTopHeadlines.OnItemClickListener { override fun onItemClick(position: Int) { + val pos = position % TOP_HEADLINES_COUNT + + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsDataForTopHeadlines[pos].url) + putExtra(NEWS_TITLE, newsDataForTopHeadlines[pos].headLine) + putExtra(NEWS_IMAGE_URL, newsDataForTopHeadlines[pos].image) + putExtra(NEWS_DESCRIPTION, newsDataForTopHeadlines[pos].description) + putExtra(NEWS_SOURCE, newsDataForTopHeadlines[pos].source) + putExtra(NEWS_PUBLICATION_TIME, newsDataForTopHeadlines[pos].time) + putExtra(NEWS_CONTENT, newsDataForTopHeadlines[pos].content) + } - val intent = Intent(context, ReadNewsActivity::class.java) - val pos = position % MainActivity.TOP_HEADLINES_COUNT - - intent.putExtra(MainActivity.NEWS_URL, newsDataForTopHeadlines[pos].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsDataForTopHeadlines[pos].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsDataForTopHeadlines[pos].image) - intent.putExtra( - MainActivity.NEWS_DESCRIPTION, - newsDataForTopHeadlines[pos].description - ) - intent.putExtra(MainActivity.NEWS_SOURCE, newsDataForTopHeadlines[pos].source) - intent.putExtra( - MainActivity.NEWS_PUBLICATION_TIME, - newsDataForTopHeadlines[pos].time - ) - intent.putExtra(MainActivity.NEWS_CONTENT, newsDataForTopHeadlines[pos].content) startActivity(intent) } }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view } - private var currentPositonOfSlider = 0 + private var currentSliderPosition = INITIAL_POSITION // pause slider when fragment paused override fun onPause() { mainHandler.removeCallbacks(swiper) - currentPositonOfSlider = position + currentSliderPosition = position super.onPause() } // resume slider when fragment resumed override fun onResume() { mainHandler.post(swiper) - position = currentPositonOfSlider + position = currentSliderPosition super.onResume() } diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/HealthFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/HealthFragment.kt index d39d18e..2113de1 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/HealthFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/HealthFragment.kt @@ -13,10 +13,16 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class HealthFragment : Fragment() { - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -33,22 +39,23 @@ class HealthFragment : Fragment() { adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } + startActivity(intent) } }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/ScienceFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/ScienceFragment.kt index 3f3d3ea..8771a20 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/ScienceFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/ScienceFragment.kt @@ -13,6 +13,13 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class ScienceFragment : Fragment() { @@ -33,25 +40,23 @@ class ScienceFragment : Fragment() { override fun onItemClick(position: Int) { - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) - startActivity(intent) + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } + startActivity(intent) } - - }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/SportsFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/SportsFragment.kt index b350107..90d3337 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/SportsFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/SportsFragment.kt @@ -13,6 +13,13 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class SportsFragment : Fragment() { @@ -32,14 +39,15 @@ class SportsFragment : Fragment() { adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { override fun onItemClick(position: Int) { - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } startActivity(intent) } }) diff --git a/app/src/main/java/com/example/newsapp/fragmentClasses/TechFragment.kt b/app/src/main/java/com/example/newsapp/fragmentClasses/TechFragment.kt index d58ad63..ed1a117 100644 --- a/app/src/main/java/com/example/newsapp/fragmentClasses/TechFragment.kt +++ b/app/src/main/java/com/example/newsapp/fragmentClasses/TechFragment.kt @@ -13,6 +13,13 @@ import com.example.newsapp.NewsModel import com.example.newsapp.R import com.example.newsapp.ReadNewsActivity import com.example.newsapp.adapters.CustomAdapter +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 class TechFragment : Fragment() { @@ -30,24 +37,25 @@ class TechFragment : Fragment() { recyclerView.adapter = adapter adapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener { + override fun onItemClick(position: Int) { + val intent = Intent(context, ReadNewsActivity::class.java).apply { + putExtra(NEWS_URL, newsData[position].url) + putExtra(NEWS_TITLE, newsData[position].headLine) + putExtra(NEWS_IMAGE_URL, newsData[position].image) + putExtra(NEWS_DESCRIPTION, newsData[position].description) + putExtra(NEWS_SOURCE, newsData[position].source) + putExtra(NEWS_PUBLICATION_TIME, newsData[position].time) + putExtra(NEWS_CONTENT, newsData[position].content) + } - val intent = Intent(context, ReadNewsActivity::class.java) - intent.putExtra(MainActivity.NEWS_URL, newsData[position].url) - intent.putExtra(MainActivity.NEWS_TITLE, newsData[position].headLine) - intent.putExtra(MainActivity.NEWS_IMAGE_URL, newsData[position].image) - intent.putExtra(MainActivity.NEWS_DESCRIPTION, newsData[position].description) - intent.putExtra(MainActivity.NEWS_SOURCE, newsData[position].source) - intent.putExtra(MainActivity.NEWS_PUBLICATION_TIME, newsData[position].time) - intent.putExtra(MainActivity.NEWS_CONTENT, newsData[position].content) startActivity(intent) } }) - //ignore + // Ignore adapter.setOnItemLongClickListener(object : CustomAdapter.OnItemLongClickListener { - override fun onItemLongClick(position: Int) { - } + override fun onItemLongClick(position: Int) = Unit }) return view diff --git a/app/src/main/java/com/example/newsapp/retrofit/RetrofitHelper.kt b/app/src/main/java/com/example/newsapp/retrofit/RetrofitHelper.kt index 88da41e..e8f6c23 100644 --- a/app/src/main/java/com/example/newsapp/retrofit/RetrofitHelper.kt +++ b/app/src/main/java/com/example/newsapp/retrofit/RetrofitHelper.kt @@ -7,7 +7,7 @@ object RetrofitHelper { private const val BASE_URL = "https://newsapi.org/v2/" - fun getInstance() : Retrofit{ + fun getInstance(): Retrofit { return Retrofit.Builder() .baseUrl(BASE_URL) diff --git a/app/src/main/java/com/example/newsapp/utils/Constants.kt b/app/src/main/java/com/example/newsapp/utils/Constants.kt new file mode 100644 index 0000000..cccfee9 --- /dev/null +++ b/app/src/main/java/com/example/newsapp/utils/Constants.kt @@ -0,0 +1,27 @@ +package com.example.newsapp.utils + +object Constants { + const val HOME = "Home" + 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" + const val TOTAL_NEWS_TAB = 7 + const val TOP_HEADLINES_COUNT = 5 + + const val DATABASE_NAME = "LOGIN_DATABASE" + + const val DEFAULT_SWIPER_DELAY = 4000L + + const val INITIAL_POSITION = 0 +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 320eb43..5ba51b0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,17 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.6.20-RC' + ext { + room_version = '2.4.3' + kotlin_version = '1.7.10' + } + repositories { google() mavenCentral() - } + dependencies { - classpath 'com.android.tools.build:gradle:7.2.1' - classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21' + classpath 'com.android.tools.build:gradle:7.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 15f5fb0..22ab408 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 18 21:46:02 IST 2022 +#Sat Oct 01 23:09:56 WIB 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME