-
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/mz 145 엑셀 파일 다운로드 #80
Changes from 3 commits
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.susu.data.data.repository | ||
|
||
import android.app.DownloadManager | ||
import android.os.Environment | ||
import androidx.core.net.toUri | ||
import com.susu.domain.repository.ExcelRepository | ||
import com.susu.domain.repository.TokenRepository | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import javax.inject.Inject | ||
|
||
class ExcelRepositoryImpl @Inject constructor( | ||
private val downloadManager: DownloadManager, | ||
private val tokenRepository: TokenRepository, | ||
) : ExcelRepository { | ||
override suspend fun downloadEnvelopExcel(): Long { | ||
val token = tokenRepository.getAccessToken().firstOrNull() ?: return -1L | ||
|
||
val request = DownloadManager.Request(url.toUri()) | ||
.setMimeType(mimeType) | ||
.setAllowedOverMetered(true) | ||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) | ||
.setTitle(downloaderName) | ||
.addRequestHeader(headerTokenName, token) | ||
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) | ||
|
||
return downloadManager.enqueue(request) | ||
} | ||
|
||
companion object { | ||
private const val url = "https://api.oksusu.site/api/v1/excel/all-envelopes" | ||
private const val mimeType = "application/vnd.ms-excel" | ||
private const val downloaderName = "수수" | ||
private const val headerTokenName = "X-SUSU-AUTH-TOKEN" | ||
private const val fileName = "수수_기록.xlsx" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.susu.data.remote.di | ||
|
||
import android.app.DownloadManager | ||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ExcelModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun providesDownloadManager(@ApplicationContext context: Context): DownloadManager { | ||
return context.getSystemService(DownloadManager::class.java) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.susu.domain.repository | ||
|
||
interface ExcelRepository { | ||
suspend fun downloadEnvelopExcel(): Long | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.susu.domain.usecase.mypage | ||
|
||
import com.susu.core.common.runCatchingIgnoreCancelled | ||
import com.susu.domain.repository.ExcelRepository | ||
import javax.inject.Inject | ||
|
||
class DownloadExcelUseCase @Inject constructor( | ||
private val excelRepository: ExcelRepository, | ||
) { | ||
suspend operator fun invoke() = runCatchingIgnoreCancelled { | ||
excelRepository.downloadEnvelopExcel() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package com.susu.feature.navigator | ||
|
||
import android.app.DownloadManager | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
|
@@ -11,9 +16,12 @@ import androidx.compose.foundation.layout.only | |
import androidx.compose.foundation.layout.systemBars | ||
import androidx.compose.foundation.layout.windowInsetsPadding | ||
import androidx.compose.ui.Modifier | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen | ||
import androidx.core.view.WindowCompat | ||
import com.susu.core.designsystem.theme.SusuTheme | ||
import com.susu.core.ui.INTENT_ACTION_DOWNLOAD_COMPLETE | ||
import com.susu.core.ui.SnackbarToken | ||
import com.susu.feature.loginsignup.social.KakaoLoginHelper | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
|
@@ -24,6 +32,8 @@ class MainActivity : ComponentActivity() { | |
private val uiState | ||
get() = viewModel.uiState.value | ||
|
||
private lateinit var broadcastReceiver: BroadcastReceiver | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
val splashScreen = installSplashScreen() | ||
super.onCreate(savedInstanceState) | ||
|
@@ -45,6 +55,24 @@ class MainActivity : ComponentActivity() { | |
|
||
WindowCompat.setDecorFitsSystemWindows(window, false) | ||
|
||
broadcastReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent?) { | ||
if (intent?.action == INTENT_ACTION_DOWNLOAD_COMPLETE) { | ||
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L) | ||
if (id != -1L) { | ||
viewModel.onShowSnackbar(SnackbarToken(message = context.getString(com.susu.feature.mypage.R.string.snackbar_success_export))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
ContextCompat.registerReceiver( | ||
this, | ||
broadcastReceiver, | ||
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), | ||
ContextCompat.RECEIVER_EXPORTED, | ||
) | ||
Comment on lines
+58
to
+74
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. MyInfoScreen으로 이동 못하나유? 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. MainActivity의 onDestry()에서 BroadcastReceiver 등록을 해제하기 위해 여기에 위치해있습니당 |
||
|
||
setContent { | ||
SusuTheme { | ||
MainScreen( | ||
|
@@ -60,4 +88,9 @@ class MainActivity : ComponentActivity() { | |
} | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
unregisterReceiver(broadcastReceiver) | ||
} | ||
} |
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.
null인 경우에는 UnknownException을 throw하는게 더 좋을거같아요!
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.
그러네요 여기서 -1L을 리턴해버리면 저장된 토큰이 없는 오류와 다운로드 중 발생한 오류인지 구분할 수 없어서 Exception throw 하는게 맞다고 생각이 드네여
반영했습니다~!
d86f0d7