Browse code

Initial commit (losing old history)

Dario Rodriguez authored on 08/04/2026 17:25:38
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,114 @@
1
+/*
2
+ * Copyright (c) 2023 Samson Achiaga
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+package com.certified.audionote.ui
18
+
19
+import android.os.Bundle
20
+import android.view.LayoutInflater
21
+import android.view.View
22
+import android.view.ViewGroup
23
+import androidx.fragment.app.Fragment
24
+import androidx.fragment.app.viewModels
25
+import androidx.navigation.NavController
26
+import androidx.navigation.Navigation
27
+import androidx.recyclerview.widget.GridLayoutManager
28
+import com.certified.audionote.R
29
+import com.certified.audionote.adapter.NoteRecyclerAdapter
30
+import com.certified.audionote.databinding.FragmentHomeBinding
31
+import com.certified.audionote.model.Note
32
+import com.certified.audionote.repository.Repository
33
+import com.certified.audionote.utils.Extensions.flags
34
+import com.certified.audionote.utils.Extensions.safeNavigate
35
+import com.google.android.material.bottomsheet.BottomSheetBehavior
36
+import dagger.hilt.android.AndroidEntryPoint
37
+import javax.inject.Inject
38
+
39
+@AndroidEntryPoint
40
+class HomeFragment : Fragment() {
41
+
42
+    private var _binding: FragmentHomeBinding? = null
43
+    private val binding get() = _binding!!
44
+
45
+    @Inject
46
+    lateinit var repository: Repository
47
+    private val viewModel: NotesViewModel by viewModels()
48
+    private lateinit var navController: NavController
49
+
50
+    override fun onCreateView(
51
+        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
52
+    ): View {
53
+        // Inflate the layout for this fragment
54
+        _binding = FragmentHomeBinding.inflate(inflater, container, false)
55
+        return binding.root
56
+    }
57
+
58
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
59
+        super.onViewCreated(view, savedInstanceState)
60
+        navController = Navigation.findNavController(view)
61
+
62
+        binding.viewModel = viewModel
63
+        binding.lifecycleOwner = viewLifecycleOwner
64
+
65
+        NoteRecyclerAdapter { note ->
66
+            val action = HomeFragmentDirections.actionHomeFragmentToEditNoteFragment(note)
67
+            navController.safeNavigate(action)
68
+        }.apply {
69
+            val layoutManager = GridLayoutManager(requireContext(), 2)
70
+            binding.recyclerViewNotes.also {
71
+                it.layoutManager = layoutManager
72
+                it.adapter = this
73
+            }
74
+        }
75
+
76
+        binding.apply {
77
+            btnSettings.setOnClickListener { navController.safeNavigate(HomeFragmentDirections.actionHomeFragmentToSettingsFragment()) }
78
+            fabAddNote.setOnClickListener {
79
+                val action =
80
+                    HomeFragmentDirections.actionHomeFragmentToAddNoteFragment(Note(audioLength = 0L))
81
+                navController.safeNavigate(action)
82
+            }
83
+
84
+            val bottomSheetBehavior =
85
+                BottomSheetBehavior.from(bottomSheetDialogLayout.bottomSheetDialog)
86
+            bottomSheetBehavior.addBottomSheetCallback(object :
87
+                BottomSheetBehavior.BottomSheetCallback() {
88
+                override fun onStateChanged(bottomSheet: View, newState: Int) {
89
+                    if (newState == BottomSheetBehavior.STATE_HIDDEN) bottomSheetBehavior.state =
90
+                        BottomSheetBehavior.STATE_COLLAPSED
91
+                }
92
+
93
+                override fun onSlide(bottomSheet: View, slideOffset: Float) {
94
+//                    Unused
95
+                }
96
+            })
97
+            bottomSheetDialogLayout.linearLayoutCompat.setOnClickListener {
98
+                if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) bottomSheetBehavior.state =
99
+                    BottomSheetBehavior.STATE_EXPANDED
100
+                else bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
101
+            }
102
+        }
103
+    }
104
+
105
+    override fun onResume() {
106
+        super.onResume()
107
+        flags(R.color.fragment_background)
108
+    }
109
+
110
+    override fun onDestroyView() {
111
+        super.onDestroyView()
112
+        _binding = null
113
+    }
114
+}
0 115
\ No newline at end of file