Skip to content

Commit

Permalink
Fix: NewsFragment navArgs NPE
Browse files Browse the repository at this point in the history
Cleanup: Renamed package
  • Loading branch information
pk-218 committed Jan 10, 2022
1 parent 20b1a11 commit da3a0d6
Show file tree
Hide file tree
Showing 24 changed files with 407 additions and 414 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

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

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
compileSdk 31

defaultConfig {
applicationId "gdsc.stydyjams.newsapp"
applicationId "gdsc.studyjams.newsapp"
minSdk 21
targetSdk 31
versionCode 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package gdsc.stydyjams.newsapp

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

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

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("gdsc.stydyjams.newsapp", appContext.packageName)
}
package gdsc.studyjams.newsapp

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

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

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("gdsc.stydyjams.newsapp", appContext.packageName)
}
}
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gdsc.stydyjams.newsapp">
package="gdsc.studyjams.newsapp">

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

Expand All @@ -12,15 +12,15 @@
android:supportsRtl="true"
android:theme="@style/Theme.App.Starting">
<activity
android:name=".RegisterActivity"
android:name="gdsc.studyjams.newsapp.RegisterActivity"
android:exported="false"
android:theme="@style/Theme.App.Starting" />
<activity
android:name=".MainActivity"
android:name="gdsc.studyjams.newsapp.MainActivity"
android:exported="false"
android:theme="@style/Theme.App.Starting" />
<activity
android:name=".LoginActivity"
android:name="gdsc.studyjams.newsapp.LoginActivity"
android:exported="true"
android:theme="@style/Theme.App.Starting">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gdsc.stydyjams.newsapp
package gdsc.studyjams.newsapp

import android.content.Context
import android.graphics.Color
Expand All @@ -10,13 +10,13 @@ import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import gdsc.stydyjams.newsapp.database.bookmark.Bookmark
import gdsc.stydyjams.newsapp.databinding.RecyclerViewItemBinding
import gdsc.studyjams.newsapp.database.bookmark.Bookmark
import gdsc.studyjams.newsapp.databinding.RecyclerViewItemBinding

class BookmarksRecyclerViewAdapter(
class BookmarksAdapter(
private val context: Context,
private val bookmarks:List<Bookmark>
) : RecyclerView.Adapter<BookmarksRecyclerViewAdapter.ItemViewHolder>() {
) : RecyclerView.Adapter<BookmarksAdapter.ItemViewHolder>() {

private lateinit var binding: RecyclerViewItemBinding

Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
package gdsc.stydyjams.newsapp
package gdsc.studyjams.newsapp

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import gdsc.stydyjams.newsapp.database.bookmark.Bookmark
import gdsc.stydyjams.newsapp.databinding.FragmentBookmarksBinding
import gdsc.stydyjams.newsapp.viewmodels.ListViewModel
import gdsc.studyjams.newsapp.databinding.FragmentBookmarksBinding
import gdsc.studyjams.newsapp.model.Article
import gdsc.studyjams.newsapp.viewmodels.ListViewModel

class BookmarksFragment : Fragment(R.layout.fragment_bookmarks) {

private var _binding: FragmentBookmarksBinding? = null
private val binding get() = _binding!!
lateinit var viewModel: ListViewModel
lateinit var recyclerViewAdapter: BookmarksRecyclerViewAdapter
lateinit var adapter: BookmarksAdapter

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {

_binding = FragmentBookmarksBinding.inflate(inflater, container, false)
// obtaining the instance of view model from the ViewModelProviderFactory present in MainActivity
viewModel = (activity as MainActivity).viewModel

val bookmarks = viewModel.getBookmarks()

// val bookmarks = MutableLiveData(listOf(Bookmark("Hi","google.com"),
// Bookmark("Bye","apple.com")))

bookmarks.observe(viewLifecycleOwner, Observer {bookmarks ->
recyclerViewAdapter =
activity?.let { activityContext -> BookmarksRecyclerViewAdapter(activityContext, bookmarks) }!!
recyclerViewAdapter.diff.submitList(bookmarks)
bookmarks.observe(viewLifecycleOwner, { bookmark ->
adapter =
activity?.let { activityContext -> BookmarksAdapter(activityContext, bookmark) }!!
adapter.diff.submitList(bookmark)
val layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
binding.articleHeadline.adapter = recyclerViewAdapter
binding.articleHeadline.adapter = adapter
binding.articleHeadline.layoutManager = layoutManager

binding.articleHeadline.addItemDecoration(
DividerItemDecoration(
DividerItemDecoration(
activity,
layoutManager.orientation
)
)

recyclerViewAdapter.setOnItemClickListener { bookmark ->
adapter.setOnItemClickListener {
val bundle = Bundle().apply {
putSerializable("url", bookmark.url)
putSerializable("article", Article(it.headline, it.url))
}
findNavController().navigate(R.id.action_bookmarks_fragment_to_newsFragment2, bundle)
findNavController().navigate(
R.id.action_bookmarks_fragment_to_newsFragment2,
bundle
)
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gdsc.stydyjams.newsapp
package gdsc.studyjams.newsapp

import android.os.Bundle
import android.view.*
Expand All @@ -7,8 +7,8 @@ import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import gdsc.stydyjams.newsapp.databinding.FragmentListBinding
import gdsc.stydyjams.newsapp.viewmodels.ListViewModel
import gdsc.studyjams.newsapp.databinding.FragmentListBinding
import gdsc.studyjams.newsapp.viewmodels.ListViewModel


class ListFragment : Fragment(R.layout.fragment_list) {
Expand Down Expand Up @@ -61,10 +61,12 @@ class ListFragment : Fragment(R.layout.fragment_list) {
binding.recyclerView.layoutManager = layoutManager

// add default divider
binding.recyclerView.addItemDecoration(DividerItemDecoration(
activity,
layoutManager.orientation
))
binding.recyclerView.addItemDecoration(
DividerItemDecoration(
activity,
layoutManager.orientation
)
)
Toast.makeText(context, "News Updated!", Toast.LENGTH_SHORT).show()

adapter?.setOnItemClickListener { article ->
Expand Down
Loading

0 comments on commit da3a0d6

Please sign in to comment.