-
Notifications
You must be signed in to change notification settings - Fork 1
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
[Feature/qr] QR코드 인식 및 사진 불러오기 기능 작업 #314
Changes from all commits
5c5c533
95034ae
bd17c3b
3577bda
c1cb67d
8535085
3e0b87b
576944f
b8ccdac
a3c97a0
2222650
e5fddae
154728e
88a8be9
261d516
5928b34
bc2df6a
394a5eb
299ee26
721f5c4
80bb168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ package com.teampophory.pophory.feature.home | |
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.result.PickVisualMediaRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
|
@@ -13,8 +11,8 @@ import com.teampophory.pophory.R | |
import com.teampophory.pophory.common.context.stringOf | ||
import com.teampophory.pophory.common.view.viewBinding | ||
import com.teampophory.pophory.databinding.ActivityHomeBinding | ||
import com.teampophory.pophory.feature.home.add.AddPhotoBottomSheet | ||
import com.teampophory.pophory.feature.home.mypage.MyPageFragment | ||
import com.teampophory.pophory.feature.home.photo.AddPhotoActivity | ||
import com.teampophory.pophory.feature.home.store.StoreFragment | ||
import com.teampophory.pophory.util.dialog.DialogUtil | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
@@ -23,22 +21,6 @@ import dagger.hilt.android.AndroidEntryPoint | |
class HomeActivity : AppCompatActivity() { | ||
private val binding: ActivityHomeBinding by viewBinding(ActivityHomeBinding::inflate) | ||
private val viewModel by viewModels<HomeViewModel>() | ||
private val addPhotoResultLauncher = | ||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
if (it.resultCode == RESULT_OK) { | ||
viewModel.eventAlbumCountUpdate() | ||
} | ||
} | ||
|
||
private val imagePicker = | ||
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri -> | ||
val currentAlbumPosition = viewModel.homeState.value.currentAlbumPosition | ||
val albumItem = viewModel.homeState.value.currentAlbums?.getOrNull(currentAlbumPosition) | ||
if (uri != null && albumItem != null) { | ||
val intent = AddPhotoActivity.getIntent(this, uri.toString(), albumItem) | ||
addPhotoResultLauncher.launch(intent) | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
@@ -75,7 +57,8 @@ class HomeActivity : AppCompatActivity() { | |
return@setOnClickListener | ||
} | ||
} | ||
imagePicker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) | ||
val modalBottomSheet = AddPhotoBottomSheet() | ||
modalBottomSheet.show(supportFragmentManager, AddPhotoBottomSheet.TAG) | ||
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
} | ||
} | ||
|
||
|
@@ -92,7 +75,6 @@ class HomeActivity : AppCompatActivity() { | |
} | ||
} | ||
|
||
|
||
companion object { | ||
@JvmStatic | ||
fun getIntent(context: Context) = Intent(context, HomeActivity::class.java).apply { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.teampophory.pophory.feature.home.add | ||
|
||
import android.app.Activity.RESULT_CANCELED | ||
import android.app.Activity.RESULT_OK | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.PickVisualMediaRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.activityViewModels | ||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
import com.teampophory.pophory.R | ||
import com.teampophory.pophory.common.view.setOnSingleClickListener | ||
import com.teampophory.pophory.common.view.viewBinding | ||
import com.teampophory.pophory.databinding.BottomSheetHomeAddPhotoBinding | ||
import com.teampophory.pophory.feature.home.HomeViewModel | ||
import com.teampophory.pophory.feature.home.photo.AddPhotoActivity | ||
import com.teampophory.pophory.feature.qr.QRActivity | ||
|
||
class AddPhotoBottomSheet : BottomSheetDialogFragment() { | ||
|
||
private val binding by viewBinding(BottomSheetHomeAddPhotoBinding::bind) | ||
private val viewModel by activityViewModels<HomeViewModel>() | ||
private lateinit var imagePicker: ActivityResultLauncher<PickVisualMediaRequest> | ||
private lateinit var addPhotoResultLauncher: ActivityResultLauncher<Intent> | ||
private lateinit var qrActivityResultLauncher: ActivityResultLauncher<Intent> | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
return inflater.inflate(R.layout.bottom_sheet_home_add_photo, container, false) | ||
} | ||
|
||
override fun onAttach(context: Context) { | ||
super.onAttach(context) | ||
|
||
imagePicker = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri -> | ||
val currentAlbumPosition = viewModel.homeState.value.currentAlbumPosition | ||
val albumItem = viewModel.homeState.value.currentAlbums?.getOrNull(currentAlbumPosition) | ||
if (uri != null && albumItem != null) { | ||
val intent = AddPhotoActivity.getIntent(context, uri.toString(), albumItem) | ||
addPhotoResultLauncher.launch(intent) | ||
Comment on lines
+40
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onAttach에서 초기화 하는 이유가 따로 있나요?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
Comment on lines
+46
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 구현되면 사진 추가 화면에서 제출 안하고 바로 뒤로가기 했을때 이 BottomSheet 그대로 보이지 않나? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다 백버튼 누르면 RESULT_CANCELED을 발생시키고 dismiss시키는 방향으로 수정하겠습니다! |
||
|
||
qrActivityResultLauncher = | ||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
if (it.resultCode == RESULT_OK) { | ||
val uriString = it.data?.getStringExtra("downloaded_image_uri") | ||
val currentAlbumPosition = viewModel.homeState.value.currentAlbumPosition | ||
val albumItem = | ||
viewModel.homeState.value.currentAlbums?.getOrNull(currentAlbumPosition) | ||
if (uriString != null && albumItem != null) { | ||
val intent = AddPhotoActivity.getIntent(context, uriString, albumItem) | ||
addPhotoResultLauncher.launch(intent) | ||
} | ||
} | ||
if (it.resultCode == RESULT_CANCELED) { | ||
dismiss() | ||
} | ||
} | ||
|
||
addPhotoResultLauncher = | ||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
if (it.resultCode == RESULT_OK) { | ||
viewModel.eventAlbumCountUpdate() | ||
dismiss() | ||
} else if (it.resultCode == RESULT_CANCELED) { | ||
dismiss() | ||
} | ||
} | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
initListMenuViews() | ||
} | ||
|
||
private fun initListMenuViews() { | ||
with(binding) { | ||
layoutQr.setOnSingleClickListener { | ||
val intent = Intent(context, QRActivity::class.java) | ||
qrActivityResultLauncher.launch(intent) | ||
} | ||
layoutGallery.setOnSingleClickListener { | ||
imagePicker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
val TAG: String = AddPhotoBottomSheet::class.java.simpleName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const 적용가능하지 않을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이게 const를 붙이면 뒤의 값이 constant하지 않다고 오류가 나옵니다! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,75 @@ | ||||||
package com.teampophory.pophory.feature.qr | ||||||
|
||||||
import android.app.DownloadManager | ||||||
import android.content.BroadcastReceiver | ||||||
import android.content.Context | ||||||
import android.content.Intent | ||||||
import android.content.IntentFilter | ||||||
import android.net.Uri | ||||||
import android.os.Environment | ||||||
import android.webkit.CookieManager | ||||||
import android.webkit.URLUtil | ||||||
|
||||||
class ImageDownloader() { | ||||||
private var receiver: BroadcastReceiver? = null | ||||||
|
||||||
fun downloadImageFromUrl(context: Context, url: String, callback: (Uri?) -> Unit) { | ||||||
val request = createDownloadRequest(context, url) | ||||||
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
이렇게 하셔도 됩니다. |
||||||
val downloadID = downloadManager.enqueue(request) | ||||||
setupDownloadCompletionReceiver(context, downloadID, callback) | ||||||
} | ||||||
|
||||||
private fun createDownloadRequest(context: Context, url: String): DownloadManager.Request { | ||||||
return DownloadManager.Request(Uri.parse(url)).apply { | ||||||
setMimeType("image/jpeg") | ||||||
val cookies = CookieManager.getInstance().getCookie(url) | ||||||
addRequestHeader("cookie", cookies) | ||||||
setDescription("Downloading image...") | ||||||
setTitle(URLUtil.guessFileName(url, null, "image/jpeg")) | ||||||
allowScanningByMediaScanner() | ||||||
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) | ||||||
setDestinationInExternalPublicDir( | ||||||
Environment.DIRECTORY_DOWNLOADS, | ||||||
URLUtil.guessFileName(url, null, "image/jpeg") | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun setupDownloadCompletionReceiver( | ||||||
context: Context, | ||||||
downloadID: Long, | ||||||
callback: (Uri?) -> Unit | ||||||
) { | ||||||
receiver = object : BroadcastReceiver() { | ||||||
override fun onReceive(context: Context?, intent: Intent?) { | ||||||
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) | ||||||
if (downloadID == id) { | ||||||
val downloadManager = | ||||||
context?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||||||
val query = DownloadManager.Query().setFilterById(downloadID) | ||||||
downloadManager.query(query).use { cursor -> | ||||||
if (cursor.moveToFirst()) { | ||||||
val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS) | ||||||
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) { | ||||||
val uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)) | ||||||
uriString?.let { | ||||||
callback(Uri.parse(it)) | ||||||
return | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
callback(null) | ||||||
} | ||||||
} | ||||||
} | ||||||
context.registerReceiver(receiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) | ||||||
} | ||||||
|
||||||
fun unregisterReceiver(context: Context) { | ||||||
receiver?.let { | ||||||
context.unregisterReceiver(it) | ||||||
} | ||||||
} | ||||||
} |
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.
이게 아마 지금 우리가 사용하고 있는 버전에서는 영향을 미치지 못할거야. 하위 버전 때문에 넣은거라면 okay
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.
예전에 사용해본 기억으로 당연히 사용하는줄 알았는데 몰랐습니다! 하위버전 대응하는 목적으로 그냥 두고 알고 있겠습니다!