forked from FoKE-Developers/FourCutTogether
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GetQRCodeUseCase and qrcode repository [FoKE-Developers#76]
- QR코드 생성 및 Bitmap 반환 UseCase 추가
- Loading branch information
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
domain/src/main/java/com/foke/together/domain/interactor/GetQRCodeUseCase.kt
This file contains 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,17 @@ | ||
package com.foke.together.domain.interactor | ||
|
||
import android.graphics.Bitmap | ||
import com.foke.together.domain.output.QRCodeRepositoryInterface | ||
import javax.inject.Inject | ||
|
||
class GetQRCodeUseCase @Inject constructor( | ||
private val qrCodeRepository: QRCodeRepositoryInterface | ||
){ | ||
suspend operator fun invoke(key: String, url: String): Result<Bitmap> { | ||
qrCodeRepository.generateQRCode(key, url) | ||
.onSuccess { | ||
return qrCodeRepository.readQRCode(key) | ||
} | ||
return Result.failure(Exception("cannot generate qr code")) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
domain/src/main/java/com/foke/together/domain/output/QRCodeRepositoryInterface.kt
This file contains 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,8 @@ | ||
package com.foke.together.domain.output | ||
|
||
import android.graphics.Bitmap | ||
|
||
interface QRCodeRepositoryInterface { | ||
suspend fun generateQRCode(key: String, url: String): Result<Unit> | ||
suspend fun readQRCode(key: String): Result<Bitmap> | ||
} |
42 changes: 42 additions & 0 deletions
42
external/src/main/java/com/foke/together/external/qrcode/QRCodeGenerator.kt
This file contains 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,42 @@ | ||
package com.foke.together.external.qrcode | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import com.foke.together.util.AppPolicy | ||
import com.foke.together.util.FileUtil | ||
import com.foke.together.util.ImageFileUtil | ||
import qrcode.QRCode | ||
import javax.inject.Inject | ||
|
||
class QRCodeGenerator @Inject constructor( | ||
) { | ||
suspend fun generate(context: Context, key: String, data: String): Result<Unit> = | ||
runCatching { | ||
val squares = QRCode.ofSquares() | ||
// TODO: check customize qrcode | ||
// val circles = QRCode.ofCircles() | ||
// val roundedSquares = QRCode.ofRoundedSquares() | ||
|
||
val qrcode = squares | ||
.withInnerSpacing(0) | ||
// TODO: check customize qrcode | ||
// .withColor(Colors.BLACK) | ||
// .withRadius(20) | ||
// .withSize(25) | ||
// .withLogo(logo.readBytes(), 20, 20) | ||
.build(data) | ||
|
||
val qrImage = qrcode.render().nativeImage() as Bitmap | ||
|
||
// TODO: need to merge ImageFileUtil and FileUtil | ||
FileUtil.createDir(context, key) | ||
ImageFileUtil.cacheBitmap(context, qrImage, "$key/${AppPolicy.DEFAULT_QR_CODE_IMAGE_NAME}") | ||
} | ||
|
||
fun readFile(context: Context, key: String): Result<Bitmap> { | ||
FileUtil.readCachedQRCodeImage(context, key)?.run { | ||
return Result.success(this) | ||
} | ||
return Result.failure(Exception("cannot read file")) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
external/src/main/java/com/foke/together/external/repository/QRCodeRepository.kt
This file contains 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.foke.together.external.repository | ||
|
||
import android.content.Context | ||
import com.foke.together.domain.output.QRCodeRepositoryInterface | ||
import com.foke.together.external.qrcode.QRCodeGenerator | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
class QRCodeRepository @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val qrCodeGenerator: QRCodeGenerator | ||
): QRCodeRepositoryInterface { | ||
override suspend fun generateQRCode(key: String, url: String): Result<Unit> { | ||
return qrCodeGenerator.generate(context, key, url) | ||
} | ||
|
||
override suspend fun readQRCode(key: String) = | ||
qrCodeGenerator.readFile(context, key) | ||
|
||
companion object { | ||
private val TAG = QRCodeRepository::class.java.simpleName | ||
} | ||
} |
This file contains 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 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 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,25 @@ | ||
package com.foke.together.util | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import java.io.File | ||
|
||
object FileUtil { | ||
fun readCachedQRCodeImage(context: Context, key: String): Bitmap? { | ||
val qrImageFile = File("${context.cacheDir}/$key/${AppPolicy.DEFAULT_QR_CODE_IMAGE_NAME}.jpg") | ||
var qrImage: Bitmap? = null | ||
|
||
if (qrImageFile.exists()) { | ||
qrImage = BitmapFactory.decodeFile(qrImageFile.absolutePath) | ||
} | ||
return qrImage | ||
} | ||
|
||
fun createDir(context: Context, key: String) { | ||
val dir = File("${context.cacheDir}/$key") | ||
if(!dir.exists()) { | ||
dir.mkdirs() | ||
} | ||
} | ||
} |