Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
quick_max
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bfcc0b31
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished the state management of TimerFragment
parent
c183bc06
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
39 deletions
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
View file @
bfcc0b31
...
@@ -17,9 +17,11 @@ import com.paktalin.quickmax.textSizeLarge
...
@@ -17,9 +17,11 @@ import com.paktalin.quickmax.textSizeLarge
import
com.paktalin.quickmax.textSizeSmall
import
com.paktalin.quickmax.textSizeSmall
import
kotlinx.android.synthetic.main.fragment_timer.view.*
import
kotlinx.android.synthetic.main.fragment_timer.view.*
private
const
val
interval
:
Long
=
1000
private
const
val
COUNTDOWN_INTERVAL
:
Long
=
1000
private
const
val
COLOR_FROM
:
Int
=
Color
.
TRANSPARENT
private
const
val
KEY_MILLIS_TO_SOLVE
=
"millis_to_solve"
private
const
val
KEY_MILLIS_TO_SOLVE
=
"millis_to_solve"
private
const
val
KEY_MILLIS_LEFT
=
"millis_left"
private
const
val
KEY_COLOR_FROM
=
"color_from"
private
const
val
KEY_COLOR_FROM
=
"color_from"
private
const
val
KEY_STATE
=
"state"
private
const
val
KEY_STATE
=
"state"
...
@@ -32,23 +34,19 @@ enum class State(val response: String?) {
...
@@ -32,23 +34,19 @@ enum class State(val response: String?) {
}
}
class
TimerFragment
:
Fragment
()
{
class
TimerFragment
:
Fragment
()
{
private
lateinit
var
state
:
State
private
lateinit
var
mView
:
View
private
lateinit
var
mView
:
View
private
var
millisToSolve
:
Long
=
0
private
var
millisToSolve
:
Long
=
0
private
var
colorFrom
:
Int
=
-
1
private
var
state
:
State
=
State
.
NONE
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
private
var
millisLeft
:
Long
=
0
private
var
colorFrom
:
Int
=
0
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
millisToSolve
=
arguments
!!
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
millisToSolve
=
arguments
!!
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
colorFrom
=
Color
.
TRANSPARENT
state
=
State
.
NONE
}
}
override
fun
onCreateView
(
override
fun
onCreateView
(
...
@@ -70,30 +68,26 @@ class TimerFragment : Fragment() {
...
@@ -70,30 +68,26 @@ class TimerFragment : Fragment() {
super
.
onSaveInstanceState
(
outState
)
super
.
onSaveInstanceState
(
outState
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
outState
.
putLong
(
KEY_MILLIS_TO_SOLVE
,
millisToSolve
)
outState
.
putLong
(
KEY_MILLIS_TO_SOLVE
,
millisToSolve
)
// try to update colorFrom from animation
outState
.
putLong
(
KEY_MILLIS_LEFT
,
millisLeft
)
outState
.
putInt
(
KEY_COLOR_FROM
,
colorFrom
)
// try to update COLOR_FROM from animation
colorAnimation
?.
let
{
animator
->
colorAnimation
?.
let
{
animator
->
animator
.
animatedValue
?.
let
{
animator
.
animatedValue
?.
let
{
value
->
value
->
colorFrom
=
value
as
Int
outState
.
putInt
(
KEY_COLOR_FROM
,
value
as
Int
)
}
}
}
}
outState
.
putInt
(
KEY_COLOR_FROM
,
colorFrom
)
}
}
fun
startNewRound
()
{
fun
startNewRound
(
left
:
Long
=
millisToSolve
,
from
:
Int
=
COLOR_FROM
)
{
if
(
isAdded
)
{
if
(
isAdded
)
{
state
=
State
.
IN_PROGRESS
state
=
State
.
IN_PROGRESS
mView
.
tv_response
mView
.
tv_response
.
apply
{
text
=
""
}
.
apply
{
text
=
""
}
.
apply
{
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSizeLarge
(
resources
))
}
.
apply
{
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSizeLarge
(
resources
))
}
if
(
timer
==
null
)
initTimer
()
timer
?.
start
()
timer
=
initTimer
(
left
).
start
()
if
(
colorAnimation
==
null
)
colorAnimation
=
initColorAnimation
(
from
).
apply
{
start
()
}
initColorAnimation
()
colorAnimation
?.
start
()
}
}
}
}
...
@@ -101,7 +95,8 @@ class TimerFragment : Fragment() {
...
@@ -101,7 +95,8 @@ class TimerFragment : Fragment() {
this
.
state
=
state
this
.
state
=
state
setResult
()
setResult
()
timer
?.
cancel
()
timer
?.
cancel
()
colorAnimation
?.
cancel
()
}
colorAnimation
?.
cancel
()
}
private
fun
setResult
()
{
private
fun
setResult
()
{
if
(
isAdded
)
{
if
(
isAdded
)
{
...
@@ -112,29 +107,29 @@ class TimerFragment : Fragment() {
...
@@ -112,29 +107,29 @@ class TimerFragment : Fragment() {
}
}
}
}
private
val
setBackgroundFilter
=
{
color
:
Int
->
private
val
setBackgroundFilter
=
{
color
:
Int
->
if
(
isAdded
)
mView
.
background
.
setColorFilter
(
color
,
PorterDuff
.
Mode
.
SRC_ATOP
)
}
if
(
isAdded
)
mView
.
background
.
setColorFilter
(
color
,
PorterDuff
.
Mode
.
SRC_ATOP
)
}
private
val
startTimer
=
{}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
colorFrom
=
savedInstanceState
.
getInt
(
KEY_COLOR_FROM
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
colorFrom
=
savedInstanceState
.
getInt
(
KEY_COLOR_FROM
)
if
(
state
==
State
.
IN_PROGRESS
)
if
(
state
==
State
.
IN_PROGRESS
)
startNewRound
()
startNewRound
(
savedInstanceState
.
getLong
(
KEY_MILLIS_LEFT
),
colorFrom
)
else
{
else
{
setResult
()
setResult
()
setBackgroundFilter
(
colorFrom
)
setBackgroundFilter
(
colorFrom
)
}
}
}
}
private
fun
initTimer
(
)
{
private
fun
initTimer
(
toSolve
:
Long
):
CountDownTimer
{
timer
=
object
:
CountDownTimer
(
millisToSolve
,
interval
)
{
return
object
:
CountDownTimer
(
toSolve
,
COUNTDOWN_INTERVAL
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
millis
ToSolve
=
millisUntilFinished
millis
Left
=
millisUntilFinished
mView
.
tv_response
.
text
=
(
millisUntilFinished
/
interval
).
toString
()
mView
.
tv_response
.
text
=
(
millisUntilFinished
/
COUNTDOWN_INTERVAL
).
toString
()
}
}
override
fun
onFinish
()
{
override
fun
onFinish
()
{
...
@@ -145,13 +140,10 @@ class TimerFragment : Fragment() {
...
@@ -145,13 +140,10 @@ class TimerFragment : Fragment() {
}
}
}
}
private
fun
initColorAnimation
()
{
private
fun
initColorAnimation
(
colorFrom
:
Int
):
ValueAnimator
{
val
colorTo
=
val
colorTo
=
color
(
context
!!
,
R
.
color
.
transparent_red
)
color
(
context
!!
,
R
.
color
.
transparent_red
)
return
ValueAnimator
.
ofObject
(
ArgbEvaluator
(),
colorFrom
,
colorTo
)
colorAnimation
=
ValueAnimator
.
ofObject
(
ArgbEvaluator
(),
colorFrom
,
colorTo
)
.
apply
{
duration
=
millisToSolve
}
.
apply
{
duration
=
millisToSolve
}
colorAnimation
?.
addUpdateListener
{
animator
->
.
apply
{
addUpdateListener
{
a
->
setBackgroundFilter
(
a
.
animatedValue
as
Int
)
}
}
setBackgroundFilter
(
animator
.
animatedValue
as
Int
)
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment