@@ -39,38 +39,18 @@ class ProfAtdReportFragment : Fragment() {
3939 private val TAG = ProfAtdReportFragment ::class .java.simpleName
4040 private lateinit var binding: FragmentProfAtdReportBinding
4141
42- private lateinit var downloadFolderFile: File
43-
4442 private val atdReportViewModel by activityViewModels<ProfAtdReportViewModel >()
4543 private val sharedProfViewModel by activityViewModels<SharedProfViewModel >()
4644
4745 private lateinit var viewPagerAdapter: AtdReportViewPagerAdapter
4846 private val tabLayoutTitles = arrayListOf (" Lectures" , " Students" )
4947
50- private lateinit var confirmDialogDownload: AlertDialog
51- private lateinit var confirmDialogOpenUpload: AlertDialog
52-
53- private val permissionRequestLauncher =
54- registerForActivityResult(ActivityResultContracts .RequestMultiplePermissions ()) { permissions ->
55- val granted = permissions.entries.all {
56- it.value == true
57- }
58- if (granted) {
59- openExcelFile()
60- } else {
61- Toast .makeText(context, " not GRANTED" , Toast .LENGTH_SHORT ).show()
62- }
63- }
64-
6548 override fun onCreateView (
6649 inflater : LayoutInflater , container : ViewGroup ? ,
6750 savedInstanceState : Bundle ?
6851 ): View {
6952 binding = FragmentProfAtdReportBinding .inflate(layoutInflater)
7053
71- downloadFolderFile = requireContext().getExternalFilesDir(Environment .DIRECTORY_DOWNLOADS )!!
72- // downloadFolderFile = requireContext().filesDir
73-
7454 return binding.root
7555 }
7656
@@ -86,9 +66,7 @@ class ProfAtdReportFragment : Fragment() {
8666 }
8767
8868 setupOptionsMenu()
89- createConfirmDownloadDialog()
90- createConfirmDialogOpenUpload()
91- setupObservers()
69+ // setupObservers()
9270
9371
9472 viewPagerAdapter = AtdReportViewPagerAdapter (this )
@@ -113,115 +91,15 @@ class ProfAtdReportFragment : Fragment() {
11391 when (it.itemId) {
11492 R .id.action_download_report -> {
11593 if (atdReportViewModel.detailsLoaded) {
116- checkPermissionAndShowDialog()
94+ findNavController().navigate(R .id.action_profAtdReportFragment_to_profExcelSheetFragment)
95+ // checkPermissionAndShowDialog()
11796 } else {
118- Toast .makeText(context, " Data not available " , Toast .LENGTH_SHORT ).show()
97+ Toast .makeText(context, " Fetching data, please wait... " , Toast .LENGTH_SHORT ).show()
11998 }
12099 true
121100 }
122101 else -> false
123102 }
124103 }
125104 }
126-
127- private fun setupObservers () {
128-
129- atdReportViewModel.atdReportSaveStatus.observe(viewLifecycleOwner) {
130- when (it) {
131- is Response .Loading -> { showSaving() }
132- is Response .Error -> {
133- stopSaving()
134- Toast .makeText(context, it.errorMessage, Toast .LENGTH_SHORT ).show()
135- }
136- is Response .Success -> {
137- stopSaving()
138- confirmDialogOpenUpload.show()
139- binding.layoutFileLocation.visibility = View .VISIBLE
140- binding.tvFileLocation.text = downloadFolderFile.path.toString()
141- }
142- }
143- }
144- }
145-
146- private fun checkPermissionAndShowDialog () {
147- if (Permissions .hasReadStoragePermissions(activity as Context )
148- && Permissions .hasWriteStoragePermissions(activity as Context )) {
149-
150- confirmDialogDownload.show()
151- } else {
152- permissionRequestLauncher.launch(Permissions .getPendingStoragePermissions(activity as Activity ))
153- }
154- }
155-
156- private fun createConfirmDownloadDialog () {
157- confirmDialogDownload = AlertDialog .Builder (context)
158- .setTitle(" Convert to excel?" )
159- .setMessage(" Attendance report will be saved as excel sheet" )
160- .setPositiveButton(Constants .YES ) { _, _ ->
161-
162- lifecycleScope.launch(Dispatchers .IO ) {
163- atdReportViewModel.downloadExcelSheet(downloadFolderFile.path.toString())
164- }
165- }
166- .setNegativeButton(Constants .NO , null )
167- .create()
168- }
169-
170- private fun createConfirmDialogOpenUpload () {
171- confirmDialogOpenUpload = AlertDialog .Builder (context)
172- .setTitle(" File Saved" )
173- // .setMessage("Do you want to open it or upload it to google drive?")
174- .setMessage(" Do you want to open it?" )
175- .setPositiveButton(" Open" ) { _, _ ->
176-
177- openExcelFile()
178- }
179- // .setNegativeButton("Upload") {_,_ -> }
180- // .setNeutralButton("Cancel", null)
181- .setNegativeButton(" Cancel" , null )
182- .create()
183- }
184-
185- private fun showSaving () {
186- binding.viewPager.alpha = 0.5F
187- binding.cvSaving.visibility = View .VISIBLE
188- }
189-
190- private fun stopSaving () {
191- binding.viewPager.alpha = 1F
192- binding.cvSaving.visibility = View .GONE
193- }
194-
195- private fun openExcelFile () {
196- try {
197- if (atdReportViewModel.sheetName != null ) {
198- val filePath = File (downloadFolderFile, " " )
199- val sheet = File (filePath, atdReportViewModel.sheetName!! )
200- val contentUri = FileProvider .getUriForFile(
201- requireContext(),
202- " com.example.teachjr.fileProvider" ,
203- sheet
204- )
205- Log .i(TAG , " TESTING, URI: ${contentUri.toString()} " )
206-
207- val intent = Intent (Intent .ACTION_VIEW )
208- if (contentUri.toString().contains(" .xls" ) || contentUri.toString().contains(" .xlsx" )) {
209- intent.setDataAndType(contentUri, " application/vnd.ms-excel" )
210- }
211- intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
212- startActivity(intent)
213-
214- // val file = File(atdReportViewModel.filePath)
215- // OpenExcel.openFile(requireContext(), file)
216- }
217- } catch (e: ActivityNotFoundException ) {
218- // binding.tvNoActivityToHandleIntent.text = binding.tvNoActivityToHandleIntent.text.toString() + downloadFolderFile.path.toString()
219- // binding.tvNoActivityToHandleIntent.visibility = View.VISIBLE
220- Toast .makeText(context, " You don't have any app that can open an EXCEL file" , Toast .LENGTH_LONG ).show()
221- }
222- catch (e: Exception ) {
223- e.printStackTrace()
224- Toast .makeText(context, " Something went wrong" , Toast .LENGTH_SHORT ).show()
225- }
226- }
227105}
0 commit comments