Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gallery drawer (#19) #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .idea/gradle.xml

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

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

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

15 changes: 14 additions & 1 deletion .idea/misc.xml

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

10 changes: 9 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 31
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "club.devsoc.scanf"
Expand Down Expand Up @@ -118,4 +117,13 @@ dependencies {
// Jetpack Compose Integration
implementation("androidx.navigation:navigation-compose:2.4.1")

// Glide for file and image loading
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'

}

repositories {
google()
mavenCentral()
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="club.devsoc.scanf">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
Expand All @@ -24,6 +25,7 @@
</intent-filter>
</activity>
<activity android:name=".view.activity.ImageActivity" />
<!-- <activity android:name=".view.activity.GalleryActivity" />-->
<activity android:name=".view.activity.QRScanActivity" />
<activity android:name=".view.activity.MainActivity"/>

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/club/devsoc/scanf/model/GalleryModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package club.devsoc.scanf.model

import android.net.Uri

data class GalleryModel(var path : Uri)
{

}
16 changes: 16 additions & 0 deletions app/src/main/java/club/devsoc/scanf/view/activity/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ package club.devsoc.scanf.view.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import club.devsoc.scanf.R
import club.devsoc.scanf.view.fragment.HomeFragment
import com.google.android.material.bottomsheet.BottomSheetBehavior

class HomeActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.home_activity)

val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Log.i("Permission: ", "Granted")
} else {
Log.i("Permission: ", "Denied")
}
}

}

}
36 changes: 36 additions & 0 deletions app/src/main/java/club/devsoc/scanf/view/adapter/GalleryAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package club.devsoc.scanf.view.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import club.devsoc.scanf.R
import club.devsoc.scanf.model.GalleryModel
import kotlinx.android.synthetic.main.gallery_photo_view.view.*
import kotlinx.android.synthetic.main.pdf_files_view.view.*

class GalleryAdapter(private var galleryModel : ArrayList<GalleryModel>):RecyclerView.Adapter<GalleryAdapter.GalleryVH>() {

class GalleryVH(itemView: View) :RecyclerView.ViewHolder(itemView){

fun bind(galleryModel: GalleryModel)
{
itemView.gallery_photo.setImageURI(galleryModel.path)
Log.e("yeet", galleryModel.path.toString())
}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)=
GalleryVH(LayoutInflater.from(parent.context).inflate(R.layout.gallery_photo_view,parent,false))


override fun onBindViewHolder(holder: GalleryVH, position: Int) {
holder.bind(galleryModel[position])
}

override fun getItemCount(): Int {
return galleryModel.size
}
}
114 changes: 113 additions & 1 deletion app/src/main/java/club/devsoc/scanf/view/fragment/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
package club.devsoc.scanf.view.fragment

import android.content.ContentUris
import android.content.Intent
import android.os.Bundle
import android.transition.TransitionInflater
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import club.devsoc.scanf.R
import club.devsoc.scanf.databinding.HomeFragmentBinding
import club.devsoc.scanf.model.GalleryModel
//import club.devsoc.scanf.view.activity.GalleryActivity
import club.devsoc.scanf.view.adapter.GalleryAdapter
import club.devsoc.scanf.viewmodel.HomeViewModel
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.bottom_sheet.*
import kotlinx.android.synthetic.main.gallery_bottom_sheet.*
import kotlinx.android.synthetic.main.gallery_bottom_sheet.view.*
import kotlinx.android.synthetic.main.home_fragment.*

private lateinit var bottomSheetBehavior :
BottomSheetBehavior<ConstraintLayout>

private lateinit var photoList : ArrayList<GalleryModel>



class HomeFragment : Fragment() {
lateinit var binding: HomeFragmentBinding
companion object {
Expand All @@ -35,8 +56,10 @@ class HomeFragment : Fragment() {
return binding.root
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

startCamera()
viewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
// TODO: Use the ViewModel
Expand All @@ -47,6 +70,21 @@ class HomeFragment : Fragment() {
null,
extras)
}
binding.galleryBtn.setOnClickListener{ view ->

openGallery()
}

binding.root.gallery_exit.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}


getImages() // Gets all image URIs from gallery

setGallery() // sets the recyclerview for gallery
bottomSheet() // Initializes bottom sheet

}

private fun startCamera() {
Expand Down Expand Up @@ -83,4 +121,78 @@ class HomeFragment : Fragment() {
}, ContextCompat.getMainExecutor(requireContext()))
}

private fun setGallery() {

var galleryAdapter = GalleryAdapter(photoList)
gallery_view.layoutManager=GridLayoutManager(activity, 2)
gallery_view.adapter = galleryAdapter
//notify data set changed
galleryAdapter.notifyDataSetChanged()
}

private fun openGallery() {

val view = layoutInflater.inflate(R.layout.gallery_bottom_sheet, null)

view.gallery_exit.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}

if (bottomSheetBehavior.state == 2 ||
bottomSheetBehavior.state == 5)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
else
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

}

private fun bottomSheet() {

bottomSheetBehavior = BottomSheetBehavior.from(gallery_layout)

// setting initial state of bottom sheet to hidden
bottomSheetBehavior.isHideable = true;
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

}

private fun getImages() {
val imageProjection = arrayOf(
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media._ID
)
val imageSortOrder = "${MediaStore.Images.Media.DATE_TAKEN} DESC"
val contentResolver = activity?.contentResolver
val cursor = contentResolver?.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageProjection,
null,
null,
imageSortOrder
)

photoList = ArrayList(3)
if(cursor != null && cursor.count != 0) {

cursor.moveToNext()
while(cursor.isAfterLast == false) {
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val id = cursor.getLong(idColumn)
val contentUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
id
)

photoList.add(GalleryModel(contentUri))
cursor.moveToNext()


}
}

}


}
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/rounded_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/darker_gray"/>
<corners android:topLeftRadius="32dp"
android:topRightRadius="32dp"/>

</shape>
53 changes: 53 additions & 0 deletions app/src/main/res/layout/gallery_bottom_sheet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gallery_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/gallery_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="@string/qr_scanner"
android:onClick="closeGallery"
app:backgroundTint="#716F6F"
app:layout_constraintBottom_toTopOf="@+id/relativeLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.636"
app:srcCompat="@drawable/ic_baseline_arrow_back_24" />

<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="236dp"
android:background="@drawable/rounded_dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/gallery_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="-1dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="-25dp">

</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Loading