-
Notifications
You must be signed in to change notification settings - Fork 0
Gallery 기본 세팅 #8
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
Open
WorldOneTop
wants to merge
5
commits into
TDH-6-develop
Choose a base branch
from
TDH-22-gallery
base: TDH-6-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/dining/coach/ui/gallery/GalleryActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.dining.coach.ui.gallery | ||
|
|
||
| import android.Manifest | ||
| import android.content.pm.PackageManager | ||
| import android.os.Bundle | ||
| import android.widget.Toast | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.activity.viewModels | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.dining.coach.R | ||
| import com.dining.coach.base.BaseActivity | ||
| import com.dining.coach.databinding.ActivityGalleryBinding | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import kotlinx.coroutines.flow.collectLatest | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| @AndroidEntryPoint | ||
| class GalleryActivity : BaseActivity<ActivityGalleryBinding>(R.layout.activity_gallery) { | ||
| private val viewModel: GalleryViewModel by viewModels() | ||
| private val requiredPermissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE) | ||
| private lateinit var galleryRecyclerViewAdapter:GalleryRecyclerViewAdapter | ||
|
|
||
| override fun createActivity() = viewModel | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| setRecyclerView() | ||
| } | ||
| private fun setRecyclerView(){ | ||
| wrapGridRecyclerView(bind.galleryRv, 4) | ||
| galleryRecyclerViewAdapter = GalleryRecyclerViewAdapter() | ||
| bind.galleryRv.adapter = galleryRecyclerViewAdapter | ||
|
|
||
| fetchGalleryAdapter() | ||
| } | ||
|
|
||
| private fun fetchGalleryAdapter(){ | ||
| if (hasAllPermissions()) { | ||
| lifecycleScope.launch { | ||
| viewModel.galleryPager.collectLatest { pagingData -> | ||
| galleryRecyclerViewAdapter.submitData(pagingData) | ||
| } | ||
| } | ||
| } else { | ||
| registerForActivityResult( | ||
| ActivityResultContracts.RequestMultiplePermissions() | ||
| ) { isGranted -> | ||
| if (isGranted.all { it.value }) { | ||
| fetchGalleryAdapter() | ||
| } else { | ||
| // TODO("replace permission denied process") | ||
| Toast.makeText(this, "permission error", Toast.LENGTH_SHORT).show() | ||
| finish() | ||
| } | ||
| }.launch(requiredPermissions) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private fun hasAllPermissions() = requiredPermissions.all { | ||
| ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED | ||
| } | ||
|
|
||
| } | ||
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/dining/coach/ui/gallery/GalleryRecyclerViewAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.dining.coach.ui.gallery | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.paging.PagingDataAdapter | ||
| import androidx.recyclerview.widget.DiffUtil | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.bumptech.glide.Glide | ||
| import com.dining.coach.databinding.RowGalleryImageBinding | ||
| import com.diningcoach.domain.model.Photo | ||
|
|
||
| class GalleryRecyclerViewAdapter : | ||
| PagingDataAdapter<Photo, GalleryRecyclerViewAdapter.ImageViewHolder>(diffCallback) { | ||
|
|
||
| companion object { | ||
| val diffCallback = object : DiffUtil.ItemCallback<Photo>() { | ||
| override fun areItemsTheSame(oldItem: Photo, newItem: Photo) = | ||
| oldItem.uri == newItem.uri | ||
|
|
||
| override fun areContentsTheSame(oldItem: Photo, newItem: Photo) = | ||
| oldItem == newItem | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateViewHolder( | ||
| parent: ViewGroup, | ||
| viewType: Int | ||
| ): ImageViewHolder = ImageViewHolder( | ||
| RowGalleryImageBinding.inflate( | ||
| LayoutInflater.from(parent.context), | ||
| parent, | ||
| false | ||
| ) | ||
| ) | ||
|
|
||
| override fun onBindViewHolder(holder: ImageViewHolder, position: Int) { | ||
| val item = getItem(position) | ||
| item?.let { | ||
| holder.bind(it) | ||
| } | ||
| } | ||
|
|
||
| inner class ImageViewHolder( | ||
| private val binding: RowGalleryImageBinding | ||
| ) : RecyclerView.ViewHolder(binding.root) { | ||
| fun bind(item: Photo) { | ||
| binding.run { | ||
| Glide.with(binding.root.context) | ||
| .load(item.uri) | ||
| .into(binding.rowGalleryImageView) | ||
| } | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/dining/coach/ui/gallery/GalleryViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.dining.coach.ui.gallery | ||
|
|
||
| import androidx.lifecycle.viewModelScope | ||
| import androidx.paging.Pager | ||
| import androidx.paging.PagingConfig | ||
| import androidx.paging.cachedIn | ||
| import com.dining.coach.base.BaseViewModel | ||
| import com.diningcoach.domain.usecase.gallery.GalleryImageFetchUseCase | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
| class GalleryViewModel @Inject constructor( | ||
| private val galleryImageFetch: GalleryImageFetchUseCase | ||
| ): BaseViewModel() { | ||
| val galleryPager = Pager( | ||
| config = PagingConfig(pageSize = 50) | ||
| ) { | ||
| galleryImageFetch() | ||
| }.flow.cachedIn(viewModelScope) | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <layout | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools"> | ||
| <androidx.constraintlayout.widget.ConstraintLayout | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| tools:context=".ui.gallery.GalleryActivity"> | ||
|
|
||
| <androidx.recyclerview.widget.RecyclerView | ||
| android:id="@+id/galleryRv" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent"/> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
| </layout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="120dp"> | ||
|
|
||
| <ImageView | ||
| android:id="@+id/rowGalleryImageView" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:scaleType="centerCrop" | ||
| tools:ignore="ContentDescription" /> | ||
|
|
||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
data/src/main/java/com/diningcoach/data/di/manager/local/MediaStoreManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.diningcoach.data.di.manager.local | ||
|
|
||
| import android.content.ContentResolver | ||
| import android.content.Context | ||
| import android.database.Cursor | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import android.provider.MediaStore | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import javax.inject.Inject | ||
|
|
||
| class MediaStoreManager @Inject constructor( | ||
| @ApplicationContext context: Context | ||
| ) { | ||
| private val contentResolver = context.contentResolver | ||
|
|
||
| fun fetchImages(projection:Array<String>, limit: Int, offset: Int): Cursor?{ | ||
| val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
| val selection = | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) MediaStore.Images.Media.SIZE + " > 0" | ||
| else null | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
| return contentResolver.query( | ||
| contentUri, | ||
| projection, | ||
| Bundle().apply { | ||
| // Limit & Offset | ||
| putInt(ContentResolver.QUERY_ARG_LIMIT, limit) | ||
| putInt(ContentResolver.QUERY_ARG_OFFSET, offset) | ||
|
|
||
| // Sort function | ||
| putStringArray( | ||
| ContentResolver.QUERY_ARG_SORT_COLUMNS, | ||
| arrayOf(MediaStore.Images.Media.DATE_TAKEN) | ||
| ) | ||
| putInt( | ||
| ContentResolver.QUERY_ARG_SORT_DIRECTION, | ||
| ContentResolver.QUERY_SORT_DIRECTION_DESCENDING | ||
| ) | ||
|
|
||
| // Selection | ||
| putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection) | ||
| }, null | ||
| ) | ||
| } else { | ||
| val sortOrder = | ||
| "${MediaStore.Images.Media.DATE_TAKEN} DESC, ${MediaStore.Images.Media._ID} DESC LIMIT $limit OFFSET $offset" | ||
| return contentResolver.query( | ||
| contentUri, | ||
| projection, | ||
| selection, | ||
| null, | ||
| sortOrder | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
data/src/main/java/com/diningcoach/data/extensions/Extensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| package com.diningcoach.data.extensions | ||
|
|
||
| import com.diningcoach.data.model.PhotoModel | ||
| import com.diningcoach.data.model.UserModel | ||
| import com.diningcoach.domain.model.Photo | ||
| import com.diningcoach.domain.model.User | ||
| import java.util.* | ||
|
|
||
| fun UserModel.toUser(): User { | ||
| return User( | ||
| id, accessToken, refreshToken | ||
| id, accessToken, refreshToken | ||
| ) | ||
| } | ||
|
|
||
| fun PhotoModel.toPhoto() = Photo( | ||
| uri, | ||
| name, | ||
| fullName, | ||
| mimeType, | ||
| Date(addedDate), | ||
| folder, | ||
| size, | ||
| width, | ||
| height, | ||
| ) |
15 changes: 15 additions & 0 deletions
15
data/src/main/java/com/diningcoach/data/model/PhotoModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.diningcoach.data.model | ||
|
|
||
| import android.net.Uri | ||
|
|
||
| data class PhotoModel( | ||
| val uri: Uri, | ||
| val name: String, | ||
| val fullName: String, | ||
| val mimeType: String, | ||
| val addedDate: Long, | ||
| val folder: String, | ||
| val size: Long, | ||
| val width: Int, | ||
| val height: Int, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위의 필드 requiredPermissions에 Manifest.permission.WRITE_EXTERNAL_STORAGE 도 필요하지 않을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
커스텀 갤러리 사용 목적이 바코드 인식으로 업로드 관련이라 저장하는 기능은 필요 없을 것 같아 추가하지않았습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Figma에 보시면 ios-식단 기록 화면에 식단 목록이 보여야 하는데, 저장하는 기능도 필요하지 않을까요?