Skip to content

Commit

Permalink
update!
Browse files Browse the repository at this point in the history
  • Loading branch information
luisansal committed Mar 8, 2021
1 parent aa76945 commit b2f4ae4
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />

<activity
android:name=".features.workmanager.WorkManagerActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />

<activity
android:name=".features.login.LoginActivity"
android:theme="@style/AppTheme.NoActionBar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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)
}

}
59 changes: 59 additions & 0 deletions app/src/main/res/layout/activity_workmanager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/ds_padding_xsmall"
>
<TextView
android:id="@+id/tvInfo1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tabla de multiplicar, configurado para que inicie en"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:layout_marginTop="@dimen/ds_margin_largex3"
/>

<TextView
android:id="@+id/tvInfo2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/seconds_remaining"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvInfo1"
android:gravity="center"
android:visibility="gone"
android:layout_marginTop="@dimen/ds_margin_small"
/>
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Work Manager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvInfo2"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="onclickStartWorkManager"
style="@style/Widget.ButtonGreen"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Manager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnStart"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="onclickCancel"
android:layout_marginTop="@dimen/ds_margin_largex3"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
<string name="not_internet_connection">No tiene conexión a internet</string>
<string name="select_location">Seleccione ubicación</string>
<string name="waiting_time_seconds">Tiempo de espera: %s %s</string>
<string name="seconds_remaining">%s segundos</string>
</resources>

0 comments on commit b2f4ae4

Please sign in to comment.