Implement saving recipes by URLs
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package gq.kirmanak.mealient.ui.share
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.extensions.showLongToast
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ShareRecipeActivity : AppCompatActivity() {
|
||||
|
||||
private val viewModel: ShareRecipeViewModel by viewModels()
|
||||
|
||||
@Inject
|
||||
lateinit var logger: Logger
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
logger.v { "onCreate() called with: savedInstanceState = $savedInstanceState" }
|
||||
|
||||
if (intent.action != Intent.ACTION_SEND || intent.type != "text/plain") {
|
||||
logger.w { "onCreate: intent.action = ${intent.action}, intent.type = ${intent.type}" }
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
val url: CharSequence? = intent.getCharSequenceExtra(Intent.EXTRA_TEXT)
|
||||
if (url == null) {
|
||||
logger.w { "onCreate: Intent's EXTRA_TEXT was null" }
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
viewModel.saveOperationResult.observe(this) {
|
||||
showLongToast(
|
||||
if (it.isSuccess) R.string.activity_share_recipe_success_toast
|
||||
else R.string.activity_share_recipe_failure_toast
|
||||
)
|
||||
finish()
|
||||
}
|
||||
|
||||
viewModel.saveRecipeByURL(url)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package gq.kirmanak.mealient.ui.share
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.share.ShareRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class ShareRecipeViewModel @Inject constructor(
|
||||
private val shareRecipeRepo: ShareRecipeRepo,
|
||||
private val logger: Logger,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _saveOperationResult = MutableLiveData<Result<String>>()
|
||||
val saveOperationResult: LiveData<Result<String>> get() = _saveOperationResult
|
||||
|
||||
fun saveRecipeByURL(url: CharSequence) {
|
||||
logger.v { "saveRecipeByURL() called with: url = $url" }
|
||||
viewModelScope.launch {
|
||||
runCatchingExceptCancel {
|
||||
shareRecipeRepo.saveRecipeByURL(url)
|
||||
}.onSuccess {
|
||||
logger.d { "Successfully saved recipe by URL" }
|
||||
_saveOperationResult.postValue(Result.success(it))
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't save recipe by URL" }
|
||||
_saveOperationResult.postValue(Result.failure(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user