Skip to content

Commit

Permalink
- EntryEditorFragment now saves its own entry instead of having HomeF…
Browse files Browse the repository at this point in the history
…ragment do it

-- Did this by scoping the ViewModel to the activity and using it in both fragments as mentioned in the docs: https://developer.android.com/topic/libraries/architecture/viewmodel#sharing_data_between_fragments
  • Loading branch information
randroid88 committed Jan 1, 2019
1 parent 20b498b commit e307bd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
18 changes: 15 additions & 3 deletions app/src/main/java/io/winf/todayilearned/EntryEditorFragment.kt
Expand Up @@ -8,7 +8,10 @@ import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import io.winf.todayilearned.utils.EntryCreator
import java.lang.Exception

class EntryEditorFragment : Fragment() {
override fun onCreateView(
Expand All @@ -18,26 +21,35 @@ class EntryEditorFragment : Fragment() {
): View? {
setHasOptionsMenu(false)

entryViewModel = activity?.run {
ViewModelProviders.of(this, EntryViewModelFactory(this.application, EntryRepository(this.application))).get(EntryViewModel::class.java)
} ?: throw Exception("Invalid Activity")

return inflater.inflate(R.layout.entry_editor_fragment, container, false)
}

private lateinit var entryViewModel: EntryViewModel

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<Button>(R.id.button_save).setOnClickListener {
closeKeyboard(view)
saveNewEntry()
navigate()
}
}

private fun saveNewEntry() {
entryViewModel.insert(EntryCreator().create(getEntryText()))
}

private fun closeKeyboard(view: View) {
view.findViewById<EditText>(R.id.edit_entry).onEditorAction(EditorInfo.IME_ACTION_DONE)
}

private fun navigate() {
val nextAction = EntryEditorFragmentDirections.nextAction()
nextAction.entryText = getEntryText()
findNavController().navigate(nextAction)
findNavController().navigate(EntryEditorFragmentDirections.nextAction())
}

private fun getEntryText(): String {
Expand Down
19 changes: 7 additions & 12 deletions app/src/main/java/io/winf/todayilearned/HomeFragment.kt
Expand Up @@ -11,7 +11,7 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import io.winf.todayilearned.utils.EntryCreator
import java.lang.Exception

class HomeFragment : Fragment() {
override fun onCreateView(
Expand All @@ -24,13 +24,10 @@ class HomeFragment : Fragment() {
val rootView = inflater.inflate(R.layout.home_fragment, container, false)
initRecyclerView(rootView)

val safeArgs = HomeFragmentArgs.fromBundle(arguments!!)
savePossibleNewEntry(safeArgs.entryText)

return rootView
}

private var entryViewModel: EntryViewModel? = null
private lateinit var entryViewModel: EntryViewModel

private fun initRecyclerView(rootView: View) {
val recyclerView = rootView.findViewById(R.id.recyclerview) as RecyclerView
Expand All @@ -39,10 +36,12 @@ class HomeFragment : Fragment() {
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(context!!)

entryViewModel = ViewModelProviders.of(this, EntryViewModelFactory(activity!!.application, EntryRepository(activity!!.application))).get(EntryViewModel::class.java)
entryViewModel = activity?.run {
ViewModelProviders.of(this, EntryViewModelFactory(this.application, EntryRepository(this.application))).get(EntryViewModel::class.java)
} ?: throw Exception("Invalid Activity")

entryViewModel!!.allEntries.observe(this, Observer { entries ->
adapter.updateCachedEntries(entries!!)
entryViewModel.allEntries.observe(this, Observer { entries ->
adapter.updateCachedEntries(entries)
})
}

Expand All @@ -55,8 +54,4 @@ class HomeFragment : Fragment() {
}
}

private fun savePossibleNewEntry(entryText: String) {
entryViewModel!!.insert(EntryCreator().create(entryText))
}

}

0 comments on commit e307bd3

Please sign in to comment.