@@ -6,64 +6,64 @@ import android.os.Bundle
66import android.widget.Toast
77import androidx.appcompat.app.AppCompatActivity
88import androidx.core.content.getSystemService
9- import androidx.lifecycle.Lifecycle
109import androidx.lifecycle.lifecycleScope
11- import androidx.lifecycle.repeatOnLifecycle
1210import com.wstxda.clippy.R
13- import com.wstxda.clippy.cleaner.modules.utils.ClipboardLinkState
11+ import com.wstxda.clippy.cleaner.processor.LinkProcessor
1412import com.wstxda.clippy.activity.utils.getSharedLink
15- import com.wstxda.clippy.viewmodel.ClipboardLinkViewModel
13+ import kotlinx.coroutines.Dispatchers
14+ import kotlinx.coroutines.withContext
1615import kotlinx.coroutines.launch
1716
1817abstract class ClipboardLinkActivity : AppCompatActivity () {
1918
20- abstract val viewModel : ClipboardLinkViewModel
19+ protected abstract val cleanLinks : Boolean
2120
2221 override fun onCreate (savedInstanceState : Bundle ? ) {
2322 super .onCreate(savedInstanceState)
24- observeState()
25- intent.getSharedLink()?.let { sharedLink ->
26- onLinkReceived(sharedLink)
27- } ? : handleFailure()
28- }
2923
30- private fun observeState () {
24+ val shared = intent.getSharedLink()
25+ if (shared.isNullOrBlank()) {
26+ finishWithToast(getString(R .string.copy_failure))
27+ return
28+ }
29+
3130 lifecycleScope.launch {
32- repeatOnLifecycle(Lifecycle .State .STARTED ) {
33- viewModel.state.collect { state ->
34- when (state) {
35- is ClipboardLinkState .Loading -> showToast(getString(R .string.copy_process))
36- is ClipboardLinkState .Success -> handleSuccess(state.links.joinToString(" \n " ))
37- is ClipboardLinkState .Error -> handleFailure(state.message)
38- else -> Unit
39- }
40- }
31+ val (valids, invalids) = LinkProcessor .extractAndValidateLinks(shared)
32+ if (valids.isEmpty()) {
33+ if (invalids.isNotEmpty()) LinkProcessor .logInvalids(invalids)
34+ finishWithToast(getString(R .string.copy_failure_no_valid_links))
35+ return @launch
4136 }
42- }
43- }
4437
45- protected open fun handleFailure (message : String = getString(R .string.copy_failure)) {
46- showToast(message)
47- finish()
48- }
38+ val output = if (cleanLinks) {
39+ showToast(getString(R .string.copy_process))
40+ val cleaned = withContext(Dispatchers .IO ) { LinkProcessor .cleanLinks(valids) }
41+ if (cleaned.isEmpty()) {
42+ finishWithToast(getString(R .string.copy_failure_empty_after_cleaning))
43+ return @launch
44+ }
45+ cleaned
46+ } else {
47+ valids
48+ }
4949
50- protected open fun handleSuccess ( link : CharSequence ) {
51- copyLinkToClipboard(link )
52- showToast(getString( R .string.copy_success) )
53- finish()
50+ copyToClipboard(output.joinToString( " \n " ))
51+ showToast(getString( R .string.copy_success) )
52+ finish( )
53+ }
5454 }
5555
56- private fun copyLinkToClipboard (link : CharSequence ) {
57- getSystemService<ClipboardManager >()?.setPrimaryClip(
58- ClipData .newPlainText(" link" , link)
59- ) ? : showToast(getString(R .string.copy_failure))
56+ private fun copyToClipboard (text : String ) {
57+ getSystemService<ClipboardManager >()?.setPrimaryClip(ClipData .newPlainText(" link" , text))
58+ ? : showToast(getString(R .string.copy_failure))
6059 }
6160
62- private fun showToast (message : String ) {
63- Toast .makeText(this , message , Toast .LENGTH_SHORT ).show()
61+ protected fun showToast (msg : String ) {
62+ Toast .makeText(applicationContext, msg , Toast .LENGTH_SHORT ).show()
6463 }
6564
66- protected open fun onLinkReceived (sharedLink : String ) {
67- viewModel.processSharedLinks(sharedLink)
65+ private fun finishWithToast (msg : String ) {
66+ showToast(msg)
67+ finish()
6868 }
6969}
0 commit comments