Skip to content

Commit ff31c39

Browse files
authored
Merge pull request #104 from rosuH/dev
WaterMark bounds configuration.
2 parents 6582a2b + df10f28 commit ff31c39

7 files changed

Lines changed: 34 additions & 82 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ android {
1515
applicationId = "me.rosuh.easywatermark"
1616
minSdk = (Apps.minSdk)
1717
targetSdk = (Apps.targetSdk)
18-
versionCode = 20500
19-
versionName = "2.5.0"
18+
versionCode = 20501
19+
versionName = "2.5.1"
2020
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2121
setProperty("archivesBaseName", "$applicationId-v$versionName($versionCode)")
2222
}

app/src/main/java/me/rosuh/easywatermark/data/model/WaterMark.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ data class WaterMark(
1717
val vGap: Int,
1818
val iconUri: Uri,
1919
val markMode: WaterMarkRepository.MarkMode,
20+
val enableBounds: Boolean
2021
)

app/src/main/java/me/rosuh/easywatermark/data/repo/WaterMarkRepository.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import me.rosuh.easywatermark.data.model.TextTypeface
1717
import me.rosuh.easywatermark.data.model.WaterMark
1818
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_ALPHA
1919
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_DEGREE
20+
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_ENABLE_BOUNDS
2021
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_HORIZON_GAP
2122
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_ICON_URI
2223
import me.rosuh.easywatermark.data.repo.WaterMarkRepository.PreferenceKeys.KEY_MODE
@@ -48,6 +49,7 @@ class WaterMarkRepository @Inject constructor(
4849
val KEY_DEGREE = floatPreferencesKey(SP_KEY_DEGREE)
4950
val KEY_ICON_URI = stringPreferencesKey(SP_KEY_ICON_URI)
5051
val KEY_MODE = intPreferencesKey(SP_KEY_WATERMARK_MODE)
52+
val KEY_ENABLE_BOUNDS = booleanPreferencesKey(SP_KEY_ENABLE_BOUNDS)
5153
}
5254

5355
val waterMark: Flow<WaterMark> = dataStore.data
@@ -71,7 +73,8 @@ class WaterMarkRepository @Inject constructor(
7173
hGap = it[KEY_HORIZON_GAP] ?: 0,
7274
vGap = it[KEY_VERTICAL_GAP] ?: 0,
7375
iconUri = Uri.parse(it[KEY_ICON_URI] ?: ""),
74-
markMode = if (it[KEY_MODE] == MarkMode.Image.value) MarkMode.Image else MarkMode.Text
76+
markMode = if (it[KEY_MODE] == MarkMode.Image.value) MarkMode.Image else MarkMode.Text,
77+
enableBounds = it[KEY_ENABLE_BOUNDS] ?: false
7578
)
7679
}
7780

@@ -141,6 +144,10 @@ class WaterMarkRepository @Inject constructor(
141144
dataStore.edit { it[KEY_MODE] = MarkMode.Text.value }
142145
}
143146

147+
suspend fun toggleBounds(enable: Boolean) {
148+
dataStore.edit { it[KEY_ENABLE_BOUNDS] = enable }
149+
}
150+
144151
sealed class MarkMode(val value: Int) {
145152
object Text : MarkMode(0)
146153

@@ -160,6 +167,7 @@ class WaterMarkRepository @Inject constructor(
160167
const val SP_KEY_VERTICAL_GAP = "${SP_NAME}_key_vertical_gap"
161168
const val SP_KEY_DEGREE = "${SP_NAME}_key_degree"
162169
const val SP_KEY_CHANGE_LOG = "${SP_NAME}_key_change_log"
170+
const val SP_KEY_ENABLE_BOUNDS = "${SP_NAME}_key_enable_bounds"
163171
const val SP_KEY_ICON_URI = "${SP_NAME}_key_icon_uri"
164172
const val SP_KEY_WATERMARK_MODE = "${SP_NAME}_key_watermark_mode"
165173
const val MAX_TEXT_SIZE = 100f

app/src/main/java/me/rosuh/easywatermark/ui/about/AboutActivity.kt

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import android.os.Bundle
88
import android.view.View
99
import androidx.activity.viewModels
1010
import androidx.appcompat.app.AppCompatActivity
11-
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1211
import dagger.hilt.android.AndroidEntryPoint
1312
import me.rosuh.easywatermark.BuildConfig
14-
import me.rosuh.easywatermark.R
1513
import me.rosuh.easywatermark.data.model.UserPreferences
1614
import me.rosuh.easywatermark.databinding.ActivityAboutBinding
1715
import me.rosuh.easywatermark.ui.dialog.ChangeLogDialogFragment
@@ -97,71 +95,15 @@ class AboutActivity : AppCompatActivity() {
9795
openLink("https://tovi.fun/")
9896
}
9997
ivBack.setOnClickListener { finish() }
100-
}
101-
}
10298

103-
private fun showOutputDialog() {
104-
MaterialAlertDialogBuilder(this@AboutActivity, R.style.ThemeOverlay_App_MaterialAlertDialog)
105-
.setTitle(getString(R.string.dialog_out_put_title))
106-
.setSingleChoiceItems(
107-
arrayOf("JPG 100%", "JPG 95%", "JPG 80%", "PNG"),
108-
getSelectedFormatting(viewModel.userPreferences.value)
109-
) { _, which ->
110-
when (which) {
111-
0 -> {
112-
viewModel.saveOutput(Bitmap.CompressFormat.JPEG, 100)
113-
}
114-
1 -> {
115-
viewModel.saveOutput(Bitmap.CompressFormat.JPEG, 95)
116-
}
117-
2 -> {
118-
viewModel.saveOutput(Bitmap.CompressFormat.JPEG, 80)
119-
}
120-
3 -> {
121-
viewModel.saveOutput(Bitmap.CompressFormat.PNG, 95)
122-
}
123-
}
124-
}
125-
.setPositiveButton(R.string.tips_confirm_dialog) { dialog, _ ->
126-
dialog.cancel()
99+
switchDebug.setOnCheckedChangeListener { _, isChecked ->
100+
viewModel.toggleBounds(isChecked)
127101
}
128-
.setNegativeButton(R.string.tips_cancel_dialog) { dialog, _ ->
129-
viewModel.saveOutput(viewModel.outputFormat, viewModel.compressLevel)
130-
dialog.cancel()
131-
}
132-
.setCancelable(true)
133-
.show()
134-
}
135102

136-
private fun getSelectedFormatting(config: UserPreferences?): Int {
137-
if (config == null) {
138-
return 1
139-
}
140-
return when {
141-
config.outputFormat == Bitmap.CompressFormat.PNG -> 3
142-
config.compressLevel == 100 -> 0
143-
config.compressLevel == 80 -> 2
144-
else -> 1
145-
}
146-
}
147-
148-
private fun trapFormattingValue(config: UserPreferences?): String {
149-
if (config == null) {
150-
return ""
151-
}
152-
val format = when (config.outputFormat) {
153-
Bitmap.CompressFormat.JPEG -> "JPG"
154-
Bitmap.CompressFormat.PNG -> "PNG"
155-
Bitmap.CompressFormat.WEBP -> "WEBP"
156-
else -> {
157-
"WEBP"
103+
viewModel.waterMark.observe(this@AboutActivity) {
104+
switchDebug.isChecked = viewModel.waterMark.value?.enableBounds ?: false
158105
}
159106
}
160-
val value = if (config.outputFormat == Bitmap.CompressFormat.JPEG) {
161-
" ${config.compressLevel}%"
162-
} else {
163-
""
164-
}
165-
return "$format$value"
166107
}
108+
167109
}

app/src/main/java/me/rosuh/easywatermark/ui/about/AboutViewModel.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,20 @@ import androidx.lifecycle.viewModelScope
77
import dagger.hilt.android.lifecycle.HiltViewModel
88
import kotlinx.coroutines.launch
99
import me.rosuh.easywatermark.data.repo.UserConfigRepository
10+
import me.rosuh.easywatermark.data.repo.WaterMarkRepository
11+
import me.rosuh.easywatermark.utils.ktx.launch
1012
import javax.inject.Inject
1113

1214
@HiltViewModel
1315
class AboutViewModel @Inject constructor(
14-
private val userRepo: UserConfigRepository
16+
private val waterMarkRepository: WaterMarkRepository
1517
) : ViewModel() {
1618

17-
val userPreferences = userRepo.userPreferences.asLiveData()
19+
val waterMark = waterMarkRepository.waterMark.asLiveData()
1820

19-
val outputFormat: Bitmap.CompressFormat
20-
get() = userPreferences.value?.outputFormat ?: Bitmap.CompressFormat.JPEG
21-
22-
val compressLevel: Int
23-
get() = userPreferences.value?.compressLevel ?: UserConfigRepository.DEFAULT_COMPRESS_LEVEL
24-
25-
fun saveOutput(format: Bitmap.CompressFormat, level: Int) {
26-
viewModelScope.launch {
27-
userRepo.updateFormat(format)
28-
userRepo.updateCompressLevel(level)
21+
fun toggleBounds(enable: Boolean) {
22+
launch {
23+
waterMarkRepository.toggleBounds(enable)
2924
}
3025
}
3126
}

app/src/main/java/me/rosuh/easywatermark/ui/widget/WaterMarkImageView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class WaterMarkImageView : androidx.appcompat.widget.AppCompatImageView, Corouti
319319
if (srcBitmap.isRecycled) {
320320
return@withContext null
321321
}
322-
val showDebugRect = BuildConfig.DEBUG && false
322+
val showDebugRect = config.enableBounds
323323
val rawWidth =
324324
srcBitmap.width.toFloat().coerceAtLeast(1f).coerceAtMost(imageInfo.width.toFloat())
325325
val rawHeight = srcBitmap.height.toFloat().coerceAtLeast(1f)
@@ -397,7 +397,7 @@ class WaterMarkImageView : androidx.appcompat.widget.AppCompatImageView, Corouti
397397
if (config.text.isBlank()) {
398398
return@withContext null
399399
}
400-
val showDebugRect = BuildConfig.DEBUG && false
400+
val showDebugRect = config.enableBounds
401401
var maxLineWidth = 0
402402
// calculate the max width of all lines
403403
config.text.split("\n").forEach {

app/src/main/res/layout/activity_about.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,14 @@
321321

322322
</LinearLayout>
323323
</HorizontalScrollView>
324+
325+
<com.google.android.material.switchmaterial.SwitchMaterial
326+
android:id="@+id/switch_debug"
327+
android:layout_width="match_parent"
328+
android:layout_height="wrap_content"
329+
android:text="Show Bounds"
330+
android:padding="28dp"
331+
app:layout_constraintTop_toBottomOf="@id/hsv" />
324332
</androidx.constraintlayout.widget.ConstraintLayout>
325333
</androidx.core.widget.NestedScrollView>
326-
327-
328334
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)