Skip to content

Commit

Permalink
[setting]: Add Core Module && Kotlin Version up
Browse files Browse the repository at this point in the history
  • Loading branch information
kez-lab committed Dec 30, 2023
1 parent 74d2805 commit f2fe87f
Show file tree
Hide file tree
Showing 38 changed files with 911 additions and 30 deletions.
30 changes: 2 additions & 28 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ plugins {

android {
namespace = "com.hmh.hamyeonham"
compileSdk = 34

defaultConfig {
applicationId = "com.hmh.hamyeonham"
minSdk = 28
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
versionCode = libs.versions.versionCode.get().toInt()
versionName = libs.versions.appVersion.get()
}

buildTypes {
Expand All @@ -35,24 +27,6 @@ android {
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hmh.hamyeonham.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project

class AndroidFeaturePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
plugins.apply("com.android.library")
configureAndroidCommonPlugin()
}
}
1 change: 1 addition & 0 deletions core/common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
18 changes: 18 additions & 0 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
hmh("feature")
hmh("compose")
}

android {
namespace = "com.hmh.hamyeonham.common"

defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}
}

dependencies {
implementation(libs.fragment.ktx)
implementation(libs.retrofit)
}
Empty file added core/common/consumer-rules.pro
Empty file.
4 changes: 4 additions & 0 deletions core/common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hmh.hamyeonham.common.activity

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit

fun AppCompatActivity.showLoading() {
supportFragmentManager.commit(allowStateLoss = true) {
add(LoadingProgressIndicator.newInstance(), LoadingProgressIndicator.TAG)
}
}

fun AppCompatActivity.hideLoading() {
supportFragmentManager.findFragmentByTag(LoadingProgressIndicator.TAG)?.let { fragment ->
supportFragmentManager.commit(allowStateLoss = true) {
remove(fragment)
}
}
}

fun AppCompatActivity.showError() {
supportFragmentManager.commit(allowStateLoss = true) {
add(ErrorFullScreenDialogFragment.newInstance(), ErrorFullScreenDialogFragment.TAG)
}
}

fun AppCompatActivity.hideError() {
supportFragmentManager.findFragmentByTag(ErrorFullScreenDialogFragment.TAG)?.let { fragment ->
supportFragmentManager.commit(allowStateLoss = true) {
remove(fragment)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hmh.hamyeonham.common.ad

enum class AdName(val adName: String) {
ALBUM_EDIT_COVER_REWARD_01("AlbumEditCover_reward_01")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hmh.hamyeonham.common.compose

import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp

fun Modifier.bottomBorder(borderWidth: Dp, color: Color): Modifier = drawBehind {
val strokeWidth = borderWidth.value * density
val y = size.height - strokeWidth / 2
drawLine(
color,
Offset(0f, y),
Offset(size.width, y),
strokeWidth
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hmh.hamyeonham.common.compose

import androidx.compose.ui.tooling.preview.Preview

@Preview(
name = "phone",
device = "spec:shape=Normal,width=360,height=760,unit=dp,dpi=480",
showBackground = true,
backgroundColor = 0xFFFFFF
)
annotation class DefaultPreview
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hmh.hamyeonham.common.context

import android.app.Dialog
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.view.View
import android.view.WindowInsets
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar

fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

fun Context.longToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}

fun Context.snackBar(anchorView: View, message: () -> String) {
Snackbar.make(anchorView, message(), Snackbar.LENGTH_SHORT).show()
}

fun Context.stringOf(@StringRes resId: Int) = getString(resId)

fun Context.colorOf(@ColorRes resId: Int) = ContextCompat.getColor(this, resId)

fun Context.drawableOf(@DrawableRes resId: Int) = ContextCompat.getDrawable(this, resId)

fun Context.dialogWidthPercent(dialog: Dialog?, percent: Double = 0.8) {
val deviceSize = getDeviceSize()
dialog?.window?.run {
val params = attributes
params.width = (deviceSize[0] * percent).toInt()
attributes = params
}
}

fun Context.getDeviceSize(): IntArray {
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = windowManager.currentWindowMetrics
val windowInsets = windowMetrics.windowInsets

val insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout()
)
val insetsWidth = insets.right + insets.left
val insetsHeight = insets.top + insets.bottom

val bounds = windowMetrics.bounds

return intArrayOf(bounds.width() - insetsWidth, bounds.height() - insetsHeight)
} else {
val display = windowManager.defaultDisplay
val size = Point()

display?.getSize(size)

return intArrayOf(size.x, size.y)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.hmh.hamyeonham.common.fragment

import android.view.View
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar

fun Fragment.toast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}

fun Fragment.longToast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()
}

fun Fragment.snackBar(anchorView: View, message: () -> String) {
Snackbar.make(anchorView, message(), Snackbar.LENGTH_SHORT).show()
}

fun Fragment.stringOf(@StringRes resId: Int, formatArgs: Any? = null) = getString(resId, formatArgs)

fun Fragment.colorOf(@ColorRes resId: Int) = ContextCompat.getColor(requireContext(), resId)

fun Fragment.drawableOf(@DrawableRes resId: Int) =
ContextCompat.getDrawable(requireContext(), resId)

fun Fragment.showLoading() {
childFragmentManager.commit(allowStateLoss = true) {
add(LoadingProgressIndicator.newInstance(), LoadingProgressIndicator.TAG)
}
}

fun Fragment.hideLoading() {
childFragmentManager.findFragmentByTag(LoadingProgressIndicator.TAG)?.let { fragment ->
childFragmentManager.commit(allowStateLoss = true) {
remove(fragment)
}
}
}

val Fragment.viewLifeCycle
get() = viewLifecycleOwner.lifecycle

val Fragment.viewLifeCycleScope
get() = viewLifecycleOwner.lifecycleScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hmh.hamyeonham.common.image

import android.graphics.Bitmap
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okio.BufferedSink

class BitmapRequestBody(
private val bitmap: Bitmap,
private val bytes: Long,
private val compressRate: Int = 100
) : RequestBody() {
override fun contentLength() = bytes
override fun contentType(): MediaType = "image/jpeg".toMediaType()

override fun writeTo(sink: BufferedSink) {
bitmap.compress(Bitmap.CompressFormat.JPEG, compressRate, sink.outputStream())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.hmh.hamyeonham.common.image

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.MediaStore
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okio.BufferedSink
import java.io.ByteArrayOutputStream

class ContentUriRequestBody(
context: Context,
private val uri: Uri?
) : RequestBody() {
private val contentResolver = context.contentResolver

private var fileName = ""
private var size = -1L
private var compressedImage: ByteArray? = null

init {
if (uri != null) {
contentResolver.query(
uri,
arrayOf(MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME),
null,
null,
null
)?.use { cursor ->
if (cursor.moveToFirst()) {
size =
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))
fileName =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))
}
}

// Compress bitmap
compressBitmap()
}
}

private fun compressBitmap() {
if (uri != null) {
val originalBitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri))
val outputStream = ByteArrayOutputStream()
val imageSizeMb = size / (1024 * 1024.toDouble())
outputStream.use {
val compressRate = ((3 / imageSizeMb) * 100).toInt()
originalBitmap.compress(
Bitmap.CompressFormat.JPEG,
if (imageSizeMb >= 3) compressRate else 100,
it
)
}
compressedImage = outputStream.toByteArray()
size = compressedImage?.size?.toLong() ?: -1L
}
}

private fun getFileName() = fileName

override fun contentLength(): Long = size

override fun contentType(): MediaType? =
uri?.let { contentResolver.getType(it)?.toMediaTypeOrNull() }

override fun writeTo(sink: BufferedSink) {
compressedImage?.let(sink::write)
}

fun toFormData(name: String) = MultipartBody.Part.createFormData(name, getFileName(), this)
}
Loading

0 comments on commit f2fe87f

Please sign in to comment.