Skip to content

Commit b3d2ce2

Browse files
data binding done
1 parent 6919b0c commit b3d2ce2

File tree

9 files changed

+335
-249
lines changed

9 files changed

+335
-249
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010

1111
defaultConfig {
1212
applicationId "com.decimalab.todokotlin"
13-
minSdkVersion 21
13+
minSdkVersion 23
1414
targetSdkVersion 30
1515
versionCode 1
1616
versionName "1.0"

app/src/main/java/com/decimalab/todokotlin/fragments/BaseViewModel.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,5 @@ class BaseViewModel(application: Application) : AndroidViewModel(application) {
6262
}
6363
}
6464

65-
fun parsePriorityToInt(priority: Priority): Int {
66-
return when (priority) {
67-
Priority.HIGH -> 0
68-
Priority.MEDIUM -> 1
69-
Priority.LOW -> 2
70-
}
71-
72-
}
73-
74-
75-
7665

7766
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.decimalab.todokotlin.fragments
2+
3+
4+
import android.view.View
5+
import android.widget.Spinner
6+
import androidx.cardview.widget.CardView
7+
import androidx.constraintlayout.widget.ConstraintLayout
8+
import androidx.databinding.BindingAdapter
9+
import androidx.lifecycle.MutableLiveData
10+
import androidx.navigation.findNavController
11+
import com.decimalab.todokotlin.R
12+
import com.decimalab.todokotlin.data.models.Priority
13+
import com.decimalab.todokotlin.data.models.ToDoData
14+
import com.decimalab.todokotlin.fragments.list.ListFragmentDirections
15+
import com.google.android.material.floatingactionbutton.FloatingActionButton
16+
17+
/**
18+
* Created by Shakil Ahmed Shaj on 31,August,2020.
19+
* shakilahmedshaj@gmail.com
20+
*/
21+
class BindingAdapters {
22+
companion object {
23+
@BindingAdapter("android:navigateToAddFragment")
24+
@JvmStatic
25+
fun navigateToAddFragment(view: FloatingActionButton, navigate: Boolean) {
26+
view.setOnClickListener {
27+
if (navigate) {
28+
view.findNavController().navigate(R.id.action_listFragment_to_addFragment)
29+
}
30+
}
31+
}
32+
33+
@BindingAdapter("android:emptyDatabase")
34+
@JvmStatic
35+
fun emptyDatabase(view: View, emptyDatabase: MutableLiveData<Boolean>) {
36+
when (emptyDatabase.value) {
37+
true -> view.visibility = View.VISIBLE
38+
false -> view.visibility = View.INVISIBLE
39+
}
40+
}
41+
42+
@BindingAdapter("android:parsePriorityToInt")
43+
@JvmStatic
44+
fun parsePriorityToInt(view: Spinner, priority: Priority) {
45+
46+
when (priority) {
47+
Priority.HIGH -> { view.setSelection(0) }
48+
Priority.MEDIUM -> { view.setSelection(1) }
49+
Priority.LOW -> { view.setSelection(2) }
50+
}
51+
52+
}
53+
@BindingAdapter("android:parsePriorityColor")
54+
@JvmStatic
55+
fun parsePriorityColor(cardView: CardView, priority: Priority){
56+
when(priority){
57+
Priority.HIGH -> { cardView.setCardBackgroundColor(cardView.context.getColor(R.color.red)) }
58+
Priority.MEDIUM -> { cardView.setCardBackgroundColor(cardView.context.getColor(R.color.yellow)) }
59+
Priority.LOW -> { cardView.setCardBackgroundColor(cardView.context.getColor(R.color.green)) }
60+
}
61+
}
62+
63+
@BindingAdapter("android:sendDataToUpdateFragment")
64+
@JvmStatic
65+
fun sendDataToUpdateFragment(view: ConstraintLayout, currentItem: ToDoData){
66+
view.setOnClickListener {
67+
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
68+
view.findNavController().navigate(action)
69+
}
70+
}
71+
}
72+
}

app/src/main/java/com/decimalab/todokotlin/fragments/list/ListAdapter.kt

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package com.decimalab.todokotlin.fragments.list
22

33
import android.view.LayoutInflater
4-
import android.view.View
54
import android.view.ViewGroup
6-
import androidx.core.content.ContextCompat
7-
import androidx.navigation.findNavController
85
import androidx.recyclerview.widget.RecyclerView
9-
import com.decimalab.todokotlin.R
10-
import com.decimalab.todokotlin.data.models.Priority
116
import com.decimalab.todokotlin.data.models.ToDoData
12-
import kotlinx.android.synthetic.main.row_layout.view.*
7+
import com.decimalab.todokotlin.databinding.RowLayoutBinding
138

149
/**
1510
* Created by Shakil Ahmed Shaj on 31,August,2020.
@@ -18,48 +13,39 @@ import kotlinx.android.synthetic.main.row_layout.view.*
1813
class ListAdapter : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
1914

2015

21-
var dataList = emptyList<ToDoData>()
16+
private var dataList = emptyList<ToDoData>()
2217

2318

24-
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
19+
class MyViewHolder(private val binding: RowLayoutBinding) :
20+
RecyclerView.ViewHolder(binding.root) {
21+
22+
fun bind(toDoData: ToDoData) {
23+
binding.toDoData = toDoData
24+
binding.executePendingBindings()
25+
}
26+
27+
companion object {
28+
fun from(parent: ViewGroup): MyViewHolder {
29+
val layoutInflater = LayoutInflater.from(parent.context)
30+
val binding = RowLayoutBinding.inflate(layoutInflater, parent, false)
31+
return MyViewHolder(
32+
binding
33+
)
34+
}
35+
}
2536

2637
}
2738

2839
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
29-
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false)
30-
return MyViewHolder(view)
40+
return MyViewHolder.from(parent)
3141
}
3242

3343
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
34-
holder.itemView.title_txt.text = dataList[position].title
35-
holder.itemView.description_txt.text = dataList[position].description
36-
37-
holder.itemView.row_background.setOnClickListener {
3844

39-
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(dataList[position])
40-
holder.itemView.findNavController().navigate(action)
41-
}
42-
43-
val priority = dataList[position].priority
44-
when (priority) {
45-
Priority.HIGH -> holder.itemView.priority_indicator.setBackgroundColor(
46-
ContextCompat.getColor(
47-
holder.itemView.context, R.color.red
48-
)
49-
)
45+
val currentItem = dataList[position]
46+
holder.bind(currentItem)
5047

51-
Priority.LOW -> holder.itemView.priority_indicator.setBackgroundColor(
52-
ContextCompat.getColor(
53-
holder.itemView.context, R.color.yellow
54-
)
55-
)
5648

57-
Priority.MEDIUM -> holder.itemView.priority_indicator.setBackgroundColor(
58-
ContextCompat.getColor(
59-
holder.itemView.context, R.color.green
60-
)
61-
)
62-
}
6349
}
6450

6551
override fun getItemCount(): Int {

app/src/main/java/com/decimalab/todokotlin/fragments/list/ListFragment.kt

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,52 @@ import android.widget.Toast
77
import androidx.fragment.app.Fragment
88
import androidx.fragment.app.viewModels
99
import androidx.lifecycle.Observer
10-
import androidx.navigation.fragment.findNavController
1110
import androidx.recyclerview.widget.LinearLayoutManager
12-
import androidx.recyclerview.widget.StaggeredGridLayoutManager
1311
import com.decimalab.todokotlin.R
1412
import com.decimalab.todokotlin.data.viewmodel.ToDoViewModel
13+
import com.decimalab.todokotlin.databinding.FragmentListBinding
1514
import com.decimalab.todokotlin.fragments.BaseViewModel
16-
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator
17-
import kotlinx.android.synthetic.main.fragment_list.*
18-
import kotlinx.android.synthetic.main.fragment_list.view.*
1915

2016
class ListFragment : Fragment() {
2117

2218
private val mToDoViewModel: ToDoViewModel by viewModels()
2319
private val mBaseViewModel: BaseViewModel by viewModels()
20+
21+
private var _binding: FragmentListBinding? = null
22+
private val binding get() = _binding!!
23+
24+
2425
private val adapter: ListAdapter by lazy { ListAdapter() }
2526

2627
override fun onCreateView(
2728
inflater: LayoutInflater, container: ViewGroup?,
2829
savedInstanceState: Bundle?
2930
): View? {
30-
// Inflate the layout for this fragment
31-
val view = inflater.inflate(R.layout.fragment_list, container, false)
3231

33-
val recyclerView = view.recycler_view
34-
recyclerView.adapter = adapter
35-
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
32+
//Data binding
33+
_binding = FragmentListBinding.inflate(inflater, container, false)
34+
binding.lifecycleOwner = this
35+
binding.mBaseViewModel = mBaseViewModel
36+
37+
//Setup RecyclerView
38+
setupRecyclerView()
3639

3740
mToDoViewModel.getAllData.observe(viewLifecycleOwner, Observer { data ->
3841
mBaseViewModel.checkIfDatabaseEmpty(data)
3942
adapter.setData(data)
4043
})
4144

42-
mBaseViewModel.emptyDatabase.observe(viewLifecycleOwner, Observer {
43-
showEmptyDatabase(it)
44-
})
45-
46-
view.floating_action_button.setOnClickListener {
47-
findNavController().navigate(R.id.action_listFragment_to_addFragment)
48-
}
49-
50-
5145

5246
setHasOptionsMenu(true)
5347

54-
return view
48+
return binding.root
5549
}
5650

57-
private fun showEmptyDatabase(emptyDatabase: Boolean) {
58-
if (emptyDatabase) {
59-
view?.no_data_imageView?.visibility = View.VISIBLE
60-
view?.no_data_textView?.visibility = View.VISIBLE
61-
} else {
62-
view?.no_data_imageView?.visibility = View.INVISIBLE
63-
view?.no_data_textView?.visibility = View.INVISIBLE
64-
}
51+
private fun setupRecyclerView() {
52+
53+
val recyclerView = binding.recyclerView
54+
recyclerView.adapter = adapter
55+
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
6556
}
6657

6758
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@@ -91,5 +82,10 @@ class ListFragment : Fragment() {
9182
builder.create().show()
9283
}
9384

85+
override fun onDestroyView() {
86+
super.onDestroyView()
87+
_binding = null
88+
}
89+
9490

9591
}

app/src/main/java/com/decimalab/todokotlin/fragments/update/UpdateFragment.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.decimalab.todokotlin.R
1212
import com.decimalab.todokotlin.data.models.Priority
1313
import com.decimalab.todokotlin.data.models.ToDoData
1414
import com.decimalab.todokotlin.data.viewmodel.ToDoViewModel
15+
import com.decimalab.todokotlin.databinding.FragmentUpdateBinding
1516
import com.decimalab.todokotlin.fragments.BaseViewModel
1617
import kotlinx.android.synthetic.main.fragment_update.*
1718
import kotlinx.android.synthetic.main.fragment_update.view.*
@@ -22,23 +23,25 @@ class UpdateFragment : Fragment() {
2223
private val mBaseViewModel: BaseViewModel by viewModels()
2324
private val mToDoViewModel: ToDoViewModel by viewModels()
2425

26+
private var _binding: FragmentUpdateBinding? = null
27+
private val binding get() = _binding!!
28+
2529
override fun onCreateView(
2630
inflater: LayoutInflater, container: ViewGroup?,
2731
savedInstanceState: Bundle?
2832
): View? {
2933

3034
// Inflate the layout for this fragment
31-
val view = inflater.inflate(R.layout.fragment_update, container, false)
35+
_binding = FragmentUpdateBinding.inflate(inflater, container, false)
36+
binding.args = args
3237

3338
setHasOptionsMenu(true)
3439

35-
view.current_title_et.setText(args.currentItem.title)
36-
view.current_description_et.setText(args.currentItem.description)
37-
view.current_priorities_spinner.setSelection(mBaseViewModel.parsePriorityToInt(args.currentItem.priority))
38-
view.current_priorities_spinner.onItemSelectedListener = mBaseViewModel.listener
40+
41+
binding.currentPrioritiesSpinner.onItemSelectedListener = mBaseViewModel.listener
3942

4043

41-
return view
44+
return binding.root
4245
}
4346

4447
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@@ -94,5 +97,9 @@ class UpdateFragment : Fragment() {
9497
}
9598
}
9699

100+
override fun onDestroy() {
101+
super.onDestroy()
102+
_binding = null
103+
}
97104

98105
}

0 commit comments

Comments
 (0)