Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/misc.xml

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

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
applicationId "com.example.teachjr"
minSdk 21
targetSdk 32
targetSdk 33
versionCode 1
versionName "1.0"

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.example.teachjr">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- TODO: Implement permission stuff For android 11-->
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>-->

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

Expand All @@ -24,7 +23,13 @@
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_FINE_LOCATION" />
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="32"/>

<!-- If your app targets Android 13 (API level 33)
or higher, you must declare the NEARBY_WIFI_DEVICES permission. -->
<uses-permission
android:name="android.permission.NEARBY_WIFI_DEVICES" />

<application
android:name=".di.MyApplication"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/example/teachjr/ui/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SplashActivity : AppCompatActivity() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Permissions.requestPermission(this@SplashActivity)
Permissions.requestStoragePermissions(this@SplashActivity)

splashViewModel.userType.observe(this) {
when(it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,18 @@ class ProfAtdReportFragment : Fragment() {
private val TAG = ProfAtdReportFragment::class.java.simpleName
private lateinit var binding: FragmentProfAtdReportBinding

private lateinit var downloadFolderFile: File

private val atdReportViewModel by activityViewModels<ProfAtdReportViewModel>()
private val sharedProfViewModel by activityViewModels<SharedProfViewModel>()

private lateinit var viewPagerAdapter: AtdReportViewPagerAdapter
private val tabLayoutTitles = arrayListOf("Lectures", "Students")

private lateinit var confirmDialogDownload: AlertDialog
private lateinit var confirmDialogOpenUpload: AlertDialog

private val permissionRequestLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val granted = permissions.entries.all {
it.value == true
}
if(granted) {
openExcelFile()
} else {
Toast.makeText(context, "not GRANTED", Toast.LENGTH_SHORT).show()
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentProfAtdReportBinding.inflate(layoutInflater)

downloadFolderFile = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!
// downloadFolderFile = requireContext().filesDir

return binding.root
}

Expand All @@ -86,9 +66,7 @@ class ProfAtdReportFragment : Fragment() {
}

setupOptionsMenu()
createConfirmDownloadDialog()
createConfirmDialogOpenUpload()
setupObservers()
// setupObservers()


viewPagerAdapter = AtdReportViewPagerAdapter(this)
Expand All @@ -113,115 +91,15 @@ class ProfAtdReportFragment : Fragment() {
when(it.itemId) {
R.id.action_download_report -> {
if(atdReportViewModel.detailsLoaded) {
checkPermissionAndShowDialog()
findNavController().navigate(R.id.action_profAtdReportFragment_to_profExcelSheetFragment)
// checkPermissionAndShowDialog()
} else {
Toast.makeText(context, "Data not available", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "Fetching data, please wait...", Toast.LENGTH_SHORT).show()
}
true
}
else -> false
}
}
}

private fun setupObservers() {

atdReportViewModel.atdReportSaveStatus.observe(viewLifecycleOwner) {
when(it) {
is Response.Loading -> { showSaving() }
is Response.Error -> {
stopSaving()
Toast.makeText(context, it.errorMessage, Toast.LENGTH_SHORT).show()
}
is Response.Success -> {
stopSaving()
confirmDialogOpenUpload.show()
binding.layoutFileLocation.visibility = View.VISIBLE
binding.tvFileLocation.text = downloadFolderFile.path.toString()
}
}
}
}

private fun checkPermissionAndShowDialog() {
if(Permissions.hasReadStoragePermissions(activity as Context)
&& Permissions.hasWriteStoragePermissions(activity as Context)) {

confirmDialogDownload.show()
} else {
permissionRequestLauncher.launch(Permissions.getPendingStoragePermissions(activity as Activity))
}
}

private fun createConfirmDownloadDialog() {
confirmDialogDownload = AlertDialog.Builder(context)
.setTitle("Convert to excel?")
.setMessage("Attendance report will be saved as excel sheet")
.setPositiveButton(Constants.YES) { _, _ ->

lifecycleScope.launch(Dispatchers.IO) {
atdReportViewModel.downloadExcelSheet(downloadFolderFile.path.toString())
}
}
.setNegativeButton(Constants.NO, null)
.create()
}

private fun createConfirmDialogOpenUpload() {
confirmDialogOpenUpload = AlertDialog.Builder(context)
.setTitle("File Saved")
// .setMessage("Do you want to open it or upload it to google drive?")
.setMessage("Do you want to open it?")
.setPositiveButton("Open") { _, _ ->

openExcelFile()
}
// .setNegativeButton("Upload") {_,_ -> }
// .setNeutralButton("Cancel", null)
.setNegativeButton("Cancel", null)
.create()
}

private fun showSaving() {
binding.viewPager.alpha = 0.5F
binding.cvSaving.visibility = View.VISIBLE
}

private fun stopSaving() {
binding.viewPager.alpha = 1F
binding.cvSaving.visibility = View.GONE
}

private fun openExcelFile() {
try {
if(atdReportViewModel.sheetName != null) {
val filePath = File(downloadFolderFile, "")
val sheet = File(filePath, atdReportViewModel.sheetName!!)
val contentUri = FileProvider.getUriForFile(
requireContext(),
"com.example.teachjr.fileProvider",
sheet
)
Log.i(TAG, "TESTING, URI: ${contentUri.toString()}")

val intent = Intent(Intent.ACTION_VIEW)
if(contentUri.toString().contains(".xls") || contentUri.toString().contains(".xlsx")) {
intent.setDataAndType(contentUri, "application/vnd.ms-excel")
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)

// val file = File(atdReportViewModel.filePath)
// OpenExcel.openFile(requireContext(), file)
}
} catch (e: ActivityNotFoundException) {
// binding.tvNoActivityToHandleIntent.text = binding.tvNoActivityToHandleIntent.text.toString() + downloadFolderFile.path.toString()
// binding.tvNoActivityToHandleIntent.visibility = View.VISIBLE
Toast.makeText(context, "You don't have any app that can open an EXCEL file", Toast.LENGTH_LONG).show()
}
catch (e: Exception) {
e.printStackTrace()
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show()
}
}
}
Loading