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
6df10aa4
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in TimeFragment
parent
5456656a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
75 deletions
app/src/main/java/com/paktalin/quickmax/TaskActivity.kt
app/src/main/java/com/paktalin/quickmax/TimerFragment.kt
app/src/main/java/com/paktalin/quickmax/ViewUtils.kt
app/src/main/java/com/paktalin/quickmax/TaskActivity.kt
View file @
6df10aa4
...
...
@@ -16,7 +16,7 @@ class TaskActivity : AppCompatActivity() {
private
var
millisToSolve
:
Long
=
4000
private
var
numDigits
:
Int
=
3
private
var
timerFragment
:
Fragment
?
=
null
private
lateinit
var
timerFragment
:
TimerFragment
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
...
...
@@ -32,8 +32,11 @@ class TaskActivity : AppCompatActivity() {
}
if
(
savedInstanceState
!=
null
)
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
else
{
timerFragment
=
TimerFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
}
retrieveExtras
()
startNewRound
()
}
...
...
@@ -54,12 +57,8 @@ class TaskActivity : AppCompatActivity() {
numDigits
,
listOf
(
card_left_top
,
card_right_top
,
card_left_bottom
,
card_right_bottom
)
)
if
(
timerFragment
==
null
)
timerFragment
=
TimerFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
}
supportFragmentManager
.
commit
(
true
)
{
replace
(
R
.
id
.
fragment_timer
,
timerFragment
!!
,
"timer_fragment"
)
replace
(
R
.
id
.
fragment_timer
,
timerFragment
,
"timer_fragment"
)
}
setUpCards
()
removeButtonNextFragment
(
supportFragmentManager
)
...
...
@@ -82,11 +81,11 @@ class TaskActivity : AppCompatActivity() {
private
fun
processAnswer
(
answer
:
Answer
)
{
if
(
answer
.
correct
)
{
answer
.
card
.
markCorrect
(
this
@TaskActivity
)
(
supportFragmentManager
.
findFragmentByTag
(
"timer_fragment"
)
as
TimerFragment
).
cancelCorrect
(
)
timerFragment
.
cancel
(
State
.
CORRECT
)
addButtonNextFragment
(
supportFragmentManager
,
true
)
}
else
{
answer
.
card
.
markWrong
(
this
@TaskActivity
)
(
supportFragmentManager
.
findFragmentByTag
(
"timer_fragment"
)
as
TimerFragment
).
cancelWrong
(
)
timerFragment
.
cancel
(
State
.
WRONG
)
addButtonNextFragment
(
supportFragmentManager
,
false
)
}
answerSet
.
forEach
{
answer
->
answer
.
card
.
disable
()
}
...
...
app/src/main/java/com/paktalin/quickmax/TimerFragment.kt
View file @
6df10aa4
...
...
@@ -13,28 +13,37 @@ import android.view.ViewGroup
import
android.widget.TextView
import
androidx.fragment.app.Fragment
private
const
val
RESPONSE_CORRECT
=
"Correct!"
private
const
val
RESPONSE_WRONG
=
"Wrong!"
private
const
val
RESPONSE_TIME_OVER
=
"Time is over!"
private
const
val
interval
:
Long
=
1000
private
const
val
KEY_MILLIS_TO_SOLVE
=
"millis_to_solve"
private
const
val
KEY_COLOR_FROM
=
"color_from"
private
const
val
KEY_STATE
=
"state"
enum
class
State
(
val
response
:
String
?)
{
IN_PROGRESS
(
null
),
CORRECT
(
"Correct!"
),
WRONG
(
"Wrong!"
),
TIME_IS_OVER
(
"Time is over!"
)
}
class
TimerFragment
:
Fragment
()
{
private
lateinit
var
timer
:
CountDownTimer
private
lateinit
var
tvResponse
:
TextView
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
lateinit
var
state
:
State
private
lateinit
var
mView
:
View
private
var
millisToSolve
:
Long
=
0
private
var
colorFrom
:
Int
=
-
1
private
val
interval
:
Long
=
1000
private
var
finished
:
Boolean
=
false
private
lateinit
var
mView
:
View
private
var
restoredColor
:
Int
?
=
null
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
millisToSolve
=
arguments
!!
.
getLong
(
"millis_to_solve"
)
millisToSolve
=
arguments
!!
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
colorFrom
=
Color
.
TRANSPARENT
state
=
State
.
IN_PROGRESS
}
override
fun
onCreateView
(
...
...
@@ -48,72 +57,58 @@ class TimerFragment : Fragment() {
if
(
savedInstanceState
!=
null
)
restoreState
(
savedInstanceState
)
if
(
!
finished
)
{
if
(
state
==
State
.
IN_PROGRESS
)
{
initTimer
()
initColorAnimation
()
}
return
view
}
override
fun
onStart
()
{
super
.
onStart
()
if
(!
finished
)
{
timer
?.
start
()
colorAnimation
?.
start
()
}
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
val
text
=
tvResponse
.
text
.
toString
()
if
(
text
==
RESPONSE_CORRECT
||
text
==
RESPONSE_WRONG
||
text
==
RESPONSE_TIME_OVER
)
outState
.
putString
(
"response_text"
,
text
)
else
outState
.
putLong
(
"millis_to_solve"
,
millisToSolve
)
colorAnimation
?.
apply
{
if
(
animatedValue
!=
null
)
restoredColor
=
animatedValue
as
Int
if
(
state
==
State
.
IN_PROGRESS
)
outState
.
putLong
(
KEY_MILLIS_TO_SOLVE
,
millisToSolve
)
colorAnimation
?.
let
{
animator
->
animator
.
animatedValue
?.
let
{
value
->
colorFrom
=
value
as
Int
}
restoredColor
?.
let
{
outState
.
putInt
(
"color_from"
,
it
)
}
}
fun
cancelCorrect
()
{
cancel
()
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSize
(
resources
))
tvResponse
.
text
=
RESPONSE_CORRECT
outState
.
putInt
(
KEY_COLOR_FROM
,
colorFrom
)
}
fun
cancel
Wrong
(
)
{
cancel
()
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSize
(
resources
)
)
t
vResponse
.
text
=
RESPONSE_WRONG
}
fun
cancel
(
state
:
State
)
{
this
.
state
=
state
setResult
(
)
t
imer
?.
cancel
()
colorAnimation
?.
cancel
()
}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
val
responseText
=
savedInstanceState
.
getString
(
"response_text"
)
if
(
responseText
!=
null
)
{
private
fun
setResult
()
{
if
(
isAdded
)
{
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSize
(
resources
))
tvResponse
.
text
=
responseText
restoredColor
=
savedInstanceState
.
getInt
(
"color_from"
)
restoredColor
?.
let
{
setBackgroundFilter
(
it
)
}
finished
=
true
}
else
{
millisToSolve
=
savedInstanceState
.
getLong
(
"millis_to_solve"
)
colorFrom
=
savedInstanceState
.
getInt
(
"color_from"
)
tvResponse
.
text
=
state
.
response
}
}
private
fun
cancel
()
{
timer
.
cancel
()
colorAnimation
?.
cancel
()
}
private
val
setBackgroundFilter
=
{
color
:
Int
->
if
(
isAdded
)
mView
.
background
.
setColorFilter
(
color
,
PorterDuff
.
Mode
.
SRC_ATOP
)
}
private
fun
setBackgroundFilter
(
color
:
Int
)
{
if
(
isAdded
)
mView
.
background
.
setColorFilter
(
color
,
PorterDuff
.
Mode
.
SRC_ATOP
)
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
colorFrom
=
savedInstanceState
.
getInt
(
KEY_COLOR_FROM
)
if
(
state
==
State
.
IN_PROGRESS
)
{
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
}
else
{
setResult
()
setBackgroundFilter
(
colorFrom
)
}
}
private
fun
initTimer
()
{
...
...
@@ -124,12 +119,9 @@ class TimerFragment : Fragment() {
}
override
fun
onFinish
()
{
if
(
isAdded
)
{
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSize
(
resources
))
tvResponse
.
text
=
RESPONSE_TIME_OVER
(
activity
as
TaskActivity
).
onTimeOver
()
}
this
@TimerFragment
.
state
=
State
.
TIME_IS_OVER
setResult
()
if
(
isAdded
)
(
activity
as
TaskActivity
).
onTimeOver
()
}
}
}
...
...
app/src/main/java/com/paktalin/quickmax/ViewUtils.kt
View file @
6df10aa4
...
...
@@ -24,15 +24,6 @@ fun textSize(resources: Resources): Float {
return
resources
.
getDimension
(
R
.
dimen
.
response_text_size
)
/
screenDensity
}
fun
addTimerFragment
(
supportFragmentManager
:
FragmentManager
,
millisToSolve
:
Long
)
{
val
timerFragment
=
TimerFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
}
supportFragmentManager
.
commit
(
true
)
{
replace
(
R
.
id
.
fragment_timer
,
timerFragment
,
"timer_fragment"
)
}
}
fun
addButtonNextFragment
(
supportFragmentManager
:
FragmentManager
,
correct
:
Boolean
)
{
val
fragmentBtnNext
=
ButtonNextFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putBoolean
(
"correct"
,
correct
)
}
...
...
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