From b2f4ae4f493e7ad0eb33aacc1e268125c63f9195 Mon Sep 17 00:00:00 2001 From: luisansal Date: Mon, 8 Mar 2021 09:33:02 -0500 Subject: [PATCH] update! --- app/src/main/AndroidManifest.xml | 5 ++ .../jetpack/features/login/LoginViewModel.kt | 2 +- .../jetpack/features/login/LoginViewState.kt | 2 +- .../jetpack/features/main/MainActivity.kt | 8 +-- .../{Bindings.kt => BindingAdapters.kt} | 0 .../features/workmanager/CalculatorWorker.kt | 28 +++++++++ .../workmanager/WorkManagerActivity.kt | 45 ++++++++++++++ .../main/res/layout/activity_workmanager.xml | 59 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 9 files changed, 144 insertions(+), 6 deletions(-) rename app/src/main/java/com/luisansal/jetpack/features/viewbinding/{Bindings.kt => BindingAdapters.kt} (100%) create mode 100644 app/src/main/java/com/luisansal/jetpack/features/workmanager/CalculatorWorker.kt create mode 100644 app/src/main/java/com/luisansal/jetpack/features/workmanager/WorkManagerActivity.kt create mode 100644 app/src/main/res/layout/activity_workmanager.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9a21723..669e870 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -104,6 +104,11 @@ android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> + + diff --git a/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewModel.kt b/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewModel.kt index a5efc9e..1a5253a 100644 --- a/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewModel.kt +++ b/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewModel.kt @@ -73,7 +73,7 @@ class LoginViewModel(private val loginUseCase: LoginUseCase) : ViewModel() { fun logout() { CoroutineScope(Dispatchers.Main).launch { loginUseCase.logout() - loginViewState.postValue(LoginViewState.SuccessState(true)) + loginViewState.postValue(LoginViewState.LogoutSuccessState(true)) } } } \ No newline at end of file diff --git a/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewState.kt b/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewState.kt index 4fbb821..5900c6b 100644 --- a/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewState.kt +++ b/app/src/main/java/com/luisansal/jetpack/features/login/LoginViewState.kt @@ -1,5 +1,5 @@ package com.luisansal.jetpack.features.login sealed class LoginViewState { - data class SuccessState(val ok: Boolean) : LoginViewState() + data class LogoutSuccessState(val ok: Boolean) : LoginViewState() } \ No newline at end of file diff --git a/app/src/main/java/com/luisansal/jetpack/features/main/MainActivity.kt b/app/src/main/java/com/luisansal/jetpack/features/main/MainActivity.kt index 9f587f9..53a649e 100644 --- a/app/src/main/java/com/luisansal/jetpack/features/main/MainActivity.kt +++ b/app/src/main/java/com/luisansal/jetpack/features/main/MainActivity.kt @@ -19,12 +19,11 @@ import com.luisansal.jetpack.features.login.LoginViewModel import com.luisansal.jetpack.features.login.LoginViewState import com.luisansal.jetpack.features.manageusers.RoomActivity import com.luisansal.jetpack.features.maps.MainMapsActivity -import com.luisansal.jetpack.features.maps.MapsActivity -import com.luisansal.jetpack.features.maps.MapsFragment import com.luisansal.jetpack.features.multimedia.MultimediaActivity import com.luisansal.jetpack.features.onboarding.OnboardingActivity import com.luisansal.jetpack.features.viewbinding.ViewBindingActivity import com.luisansal.jetpack.features.viewpager.ViewPagerActivity +import com.luisansal.jetpack.features.workmanager.WorkManagerActivity import kotlinx.android.synthetic.main.activity_main.* import org.koin.androidx.viewmodel.ext.android.viewModel @@ -67,7 +66,7 @@ class MainActivity : BaseActivity() { startActivity(Intent(this, ViewBindingActivity::class.java)) } FeaturesEnum.WORK_MANAGER -> { - startActivity(Intent(this, OnboardingActivity::class.java)) + startActivity(Intent(this, WorkManagerActivity::class.java)) } FeaturesEnum.ALARM_MANAGER -> { startActivity(Intent(this, OnboardingActivity::class.java)) @@ -98,12 +97,13 @@ class MainActivity : BaseActivity() { data.add(FeaturesEnum.VIEW_PAGER) data.add(FeaturesEnum.VIEW_BINDING_COUNTDOWN) data.add(FeaturesEnum.BROADCAST_RECEIVER) + data.add(FeaturesEnum.WORK_MANAGER) featuresAdapter.dataSet = data rvFeatures.adapter = featuresAdapter loginViewModel.loginViewState.observe(this, Observer { - if (it is LoginViewState.SuccessState) { + if (it is LoginViewState.LogoutSuccessState) { startActivity(Intent(this, LoginActivity::class.java)) } }) diff --git a/app/src/main/java/com/luisansal/jetpack/features/viewbinding/Bindings.kt b/app/src/main/java/com/luisansal/jetpack/features/viewbinding/BindingAdapters.kt similarity index 100% rename from app/src/main/java/com/luisansal/jetpack/features/viewbinding/Bindings.kt rename to app/src/main/java/com/luisansal/jetpack/features/viewbinding/BindingAdapters.kt diff --git a/app/src/main/java/com/luisansal/jetpack/features/workmanager/CalculatorWorker.kt b/app/src/main/java/com/luisansal/jetpack/features/workmanager/CalculatorWorker.kt new file mode 100644 index 0000000..c0774bf --- /dev/null +++ b/app/src/main/java/com/luisansal/jetpack/features/workmanager/CalculatorWorker.kt @@ -0,0 +1,28 @@ +package com.luisansal.jetpack.features.workmanager + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.widget.Toast +import androidx.work.Worker +import androidx.work.WorkerParameters + +class CalculatorWorker(private val context: Context, params: WorkerParameters) : Worker(context, params) { + + override fun doWork(): Result { + var text = "" + + Handler(Looper.getMainLooper()).postDelayed({ + for (x: Int in 1..2) { + for (y: Int in 1..10) { + text += "$x x $y = ${x * y}" + Toast.makeText(context, text, Toast.LENGTH_SHORT).show() + text = "" + } + } + }, 500) + + + return Result.success() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/luisansal/jetpack/features/workmanager/WorkManagerActivity.kt b/app/src/main/java/com/luisansal/jetpack/features/workmanager/WorkManagerActivity.kt new file mode 100644 index 0000000..ff8453a --- /dev/null +++ b/app/src/main/java/com/luisansal/jetpack/features/workmanager/WorkManagerActivity.kt @@ -0,0 +1,45 @@ +package com.luisansal.jetpack.features.workmanager + +import android.os.CountDownTimer +import android.view.View +import androidx.work.Constraints +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager +import com.luisansal.jetpack.R +import com.luisansal.jetpack.base.BaseBindingActivity +import com.luisansal.jetpack.databinding.ActivityWorkmanagerBinding +import java.time.Duration + +class WorkManagerActivity : BaseBindingActivity() { + private val binding by lazy { + ActivityWorkmanagerBinding.inflate(layoutInflater).apply { + lifecycleOwner = this@WorkManagerActivity + } + } + private val mWork by lazy { + OneTimeWorkRequest.Builder(CalculatorWorker::class.java) + .setInitialDelay(Duration.ofSeconds(3)) + .setConstraints(Constraints.Builder().setRequiresCharging(true).build()) + .addTag("MY_CALCULATOR") + .build() + } + + override fun getViewResource() = binding.root + + fun onclickStartWorkManager(view: View) { + binding.tvInfo2.visibility = View.VISIBLE + val countDownTimer = object : CountDownTimer(3000, 1000) { + override fun onFinish() = Unit + override fun onTick(tickTime: Long) { + binding.tvInfo2.text = String.format(getString(R.string.seconds_remaining), tickTime / 1000) + } + } + countDownTimer.start() + WorkManager.getInstance().enqueue(mWork) + } + + fun onclickCancel(view: View) { + WorkManager.getInstance().cancelWorkById(mWork.id) + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_workmanager.xml b/app/src/main/res/layout/activity_workmanager.xml new file mode 100644 index 0000000..d1ed6d9 --- /dev/null +++ b/app/src/main/res/layout/activity_workmanager.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + +