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
c183bc06
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polished the TimerFragment logic
parent
d2679ecf
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
92 additions
and
50 deletions
app/src/main/java/com/paktalin/quickmax/MainActivity.kt
app/src/main/java/com/paktalin/quickmax/ViewUtils.kt
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
app/src/main/java/com/paktalin/quickmax/task/ui/TaskActivity.kt
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
app/src/main/res/layout-land/fragment_timer.xml
app/src/main/res/layout/fragment_timer.xml
app/src/main/res/values/dimens.xml
app/src/main/java/com/paktalin/quickmax/MainActivity.kt
View file @
c183bc06
...
@@ -64,7 +64,7 @@ class MainActivity : AppCompatActivity() {
...
@@ -64,7 +64,7 @@ class MainActivity : AppCompatActivity() {
}
}
private
fun
startTaskActivity
()
{
private
fun
startTaskActivity
()
{
val
intent
=
Intent
(
this
,
TaskActivity
::
class
.
java
)
val
intent
=
Intent
(
this
@MainActivity
,
TaskActivity
::
class
.
java
)
.
apply
{
putExtra
(
"num_digits"
,
numDigits
)
}
.
apply
{
putExtra
(
"num_digits"
,
numDigits
)
}
.
apply
{
putExtra
(
"sec_to_solve"
,
secToSolve
)
}
.
apply
{
putExtra
(
"sec_to_solve"
,
secToSolve
)
}
startActivity
(
intent
)
startActivity
(
intent
)
...
...
app/src/main/java/com/paktalin/quickmax/ViewUtils.kt
View file @
c183bc06
...
@@ -20,9 +20,14 @@ fun color(context: Context, id: Int): Int {
...
@@ -20,9 +20,14 @@ fun color(context: Context, id: Int): Int {
return
ContextCompat
.
getColor
(
context
,
id
)
return
ContextCompat
.
getColor
(
context
,
id
)
}
}
fun
textSize
(
resources
:
Resources
):
Float
{
fun
textSize
Small
(
resources
:
Resources
):
Float
{
val
screenDensity
=
resources
.
displayMetrics
.
density
val
screenDensity
=
resources
.
displayMetrics
.
density
return
resources
.
getDimension
(
R
.
dimen
.
response_text_size
)
/
screenDensity
return
resources
.
getDimension
(
R
.
dimen
.
response_small
)
/
screenDensity
}
fun
textSizeLarge
(
resources
:
Resources
):
Float
{
val
screenDensity
=
resources
.
displayMetrics
.
density
return
resources
.
getDimension
(
R
.
dimen
.
response_large
)
/
screenDensity
}
}
fun
addButtonNextFragment
(
supportFragmentManager
:
FragmentManager
,
correct
:
Boolean
)
{
fun
addButtonNextFragment
(
supportFragmentManager
:
FragmentManager
,
correct
:
Boolean
)
{
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
View file @
c183bc06
...
@@ -15,6 +15,7 @@ class AnswersFragment : Fragment() {
...
@@ -15,6 +15,7 @@ class AnswersFragment : Fragment() {
private
lateinit
var
answerSet
:
AnswerSet
private
lateinit
var
answerSet
:
AnswerSet
private
lateinit
var
mView
:
View
private
lateinit
var
mView
:
View
private
var
numDigits
:
Int
=
0
private
var
numDigits
:
Int
=
0
private
var
isReady
:
Boolean
=
false
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
...
@@ -28,9 +29,21 @@ class AnswersFragment : Fragment() {
...
@@ -28,9 +29,21 @@ class AnswersFragment : Fragment() {
):
View
?
{
):
View
?
{
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_answers
,
container
,
false
)
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_answers
,
container
,
false
)
mView
=
view
mView
=
view
isReady
=
true
startNewRound
()
return
view
}
fun
startNewRound
()
{
if
(
isReady
)
{
answerSet
=
AnswerSet
(
answerSet
=
AnswerSet
(
numDigits
,
numDigits
,
listOf
(
view
.
card_left_top
,
view
.
card_right_top
,
view
.
card_left_bottom
,
view
.
card_right_bottom
)
listOf
(
mView
.
card_left_top
,
mView
.
card_right_top
,
mView
.
card_left_bottom
,
mView
.
card_right_bottom
)
)
)
answerSet
.
forEach
{
answer
->
answerSet
.
forEach
{
answer
->
answer
.
card
.
setOnCheckedChangeListener
{
_
,
isChecked
->
answer
.
card
.
setOnCheckedChangeListener
{
_
,
isChecked
->
...
@@ -38,8 +51,7 @@ class AnswersFragment : Fragment() {
...
@@ -38,8 +51,7 @@ class AnswersFragment : Fragment() {
}
}
answer
.
card
.
initial
(
context
!!
,
answer
.
value
)
answer
.
card
.
initial
(
context
!!
,
answer
.
value
)
}
}
}
return
view
}
}
private
fun
processAnswer
(
answer
:
Answer
)
{
private
fun
processAnswer
(
answer
:
Answer
)
{
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/TaskActivity.kt
View file @
c183bc06
...
@@ -8,7 +8,6 @@ import com.paktalin.quickmax.MainActivity
...
@@ -8,7 +8,6 @@ import com.paktalin.quickmax.MainActivity
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.addButtonNextFragment
import
com.paktalin.quickmax.addButtonNextFragment
import
com.paktalin.quickmax.removeButtonNextFragment
import
com.paktalin.quickmax.removeButtonNextFragment
import
com.paktalin.quickmax.task.model.Answer
import
com.paktalin.quickmax.task.model.AnswerSet
import
com.paktalin.quickmax.task.model.AnswerSet
import
kotlinx.android.synthetic.main.activity_task.*
import
kotlinx.android.synthetic.main.activity_task.*
...
@@ -35,33 +34,40 @@ class TaskActivity : AppCompatActivity() {
...
@@ -35,33 +34,40 @@ class TaskActivity : AppCompatActivity() {
)
)
}
}
if
(
savedInstanceState
!=
null
)
if
(
savedInstanceState
!=
null
)
{
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
timerFragment
=
supportFragmentManager
.
getFragment
(
else
{
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
answersFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"answers_fragment"
)
as
AnswersFragment
}
else
{
retrieveExtras
()
retrieveExtras
()
timerFragment
=
TimerFragment
()
.
apply
{
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
}
.
apply
{
supportFragmentManager
.
commit
(
true
)
{
add
(
R
.
id
.
container_timer
,
this
@apply
,
"timer_fragment"
)}
}
answersFragment
=
AnswersFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putInt
(
"num_digits"
,
numDigits
)
}
}
supportFragmentManager
.
commit
(
true
)
{
replace
(
R
.
id
.
container_answers
,
answersFragment
,
"answers_fragment"
)
}
startNewRound
()
startNewRound
()
}
}
}
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
super
.
onSaveInstanceState
(
outState
)
supportFragmentManager
.
putFragment
(
outState
,
"timer_fragment"
,
timerFragment
!!
)
supportFragmentManager
.
putFragment
(
outState
,
"timer_fragment"
,
timerFragment
)
supportFragmentManager
.
putFragment
(
outState
,
"answers_fragment"
,
answersFragment
)
// TODO save selection
// TODO save selection
}
}
internal
fun
startNewRound
()
{
internal
fun
startNewRound
()
{
timerFragment
=
TimerFragment
().
apply
{
timerFragment
.
startNewRound
()
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
answersFragment
.
startNewRound
()
}
supportFragmentManager
.
commit
(
true
)
{
replace
(
R
.
id
.
container_timer
,
timerFragment
,
"timer_fragment"
)
}
answersFragment
=
AnswersFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putInt
(
"num_digits"
,
numDigits
)
}
}
supportFragmentManager
.
commit
(
true
)
{
replace
(
R
.
id
.
container_answers
,
answersFragment
,
"answers_fragment"
)
}
removeButtonNextFragment
(
supportFragmentManager
)
removeButtonNextFragment
(
supportFragmentManager
)
}
}
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
View file @
c183bc06
...
@@ -10,11 +10,12 @@ import android.util.TypedValue
...
@@ -10,11 +10,12 @@ import android.util.TypedValue
import
android.view.LayoutInflater
import
android.view.LayoutInflater
import
android.view.View
import
android.view.View
import
android.view.ViewGroup
import
android.view.ViewGroup
import
android.widget.TextView
import
androidx.fragment.app.Fragment
import
androidx.fragment.app.Fragment
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.color
import
com.paktalin.quickmax.color
import
com.paktalin.quickmax.textSize
import
com.paktalin.quickmax.textSizeLarge
import
com.paktalin.quickmax.textSizeSmall
import
kotlinx.android.synthetic.main.fragment_timer.view.*
private
const
val
interval
:
Long
=
1000
private
const
val
interval
:
Long
=
1000
...
@@ -23,6 +24,7 @@ private const val KEY_COLOR_FROM = "color_from"
...
@@ -23,6 +24,7 @@ private const val KEY_COLOR_FROM = "color_from"
private
const
val
KEY_STATE
=
"state"
private
const
val
KEY_STATE
=
"state"
enum
class
State
(
val
response
:
String
?)
{
enum
class
State
(
val
response
:
String
?)
{
NONE
(
null
),
IN_PROGRESS
(
null
),
IN_PROGRESS
(
null
),
CORRECT
(
"Correct!"
),
CORRECT
(
"Correct!"
),
WRONG
(
"Wrong!"
),
WRONG
(
"Wrong!"
),
...
@@ -31,7 +33,6 @@ enum class State(val response: String?) {
...
@@ -31,7 +33,6 @@ enum class State(val response: String?) {
class
TimerFragment
:
Fragment
()
{
class
TimerFragment
:
Fragment
()
{
private
lateinit
var
tvResponse
:
TextView
private
lateinit
var
state
:
State
private
lateinit
var
state
:
State
private
lateinit
var
mView
:
View
private
lateinit
var
mView
:
View
...
@@ -41,12 +42,13 @@ class TimerFragment : Fragment() {
...
@@ -41,12 +42,13 @@ class TimerFragment : Fragment() {
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
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
colorFrom
=
Color
.
TRANSPARENT
state
=
State
.
IN_PROGRESS
state
=
State
.
NONE
}
}
override
fun
onCreateView
(
override
fun
onCreateView
(
...
@@ -56,26 +58,14 @@ class TimerFragment : Fragment() {
...
@@ -56,26 +58,14 @@ class TimerFragment : Fragment() {
):
View
?
{
):
View
?
{
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_timer
,
container
,
false
)
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_timer
,
container
,
false
)
mView
=
view
mView
=
view
tvResponse
=
mView
.
findViewById
(
R
.
id
.
tv_response
)
if
(
savedInstanceState
!=
null
)
if
(
savedInstanceState
!=
null
)
restoreState
(
savedInstanceState
)
restoreState
(
savedInstanceState
)
if
(
state
==
State
.
IN_PROGRESS
)
{
else
initTimer
()
startNewRound
()
initColorAnimation
()
}
else
{
setResult
()
setBackgroundFilter
(
colorFrom
)
}
return
view
return
view
}
}
override
fun
onStart
()
{
super
.
onStart
()
timer
?.
start
()
colorAnimation
?.
start
()
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
super
.
onSaveInstanceState
(
outState
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
...
@@ -89,6 +79,24 @@ class TimerFragment : Fragment() {
...
@@ -89,6 +79,24 @@ class TimerFragment : Fragment() {
outState
.
putInt
(
KEY_COLOR_FROM
,
colorFrom
)
outState
.
putInt
(
KEY_COLOR_FROM
,
colorFrom
)
}
}
fun
startNewRound
()
{
if
(
isAdded
)
{
state
=
State
.
IN_PROGRESS
mView
.
tv_response
.
apply
{
text
=
""
}
.
apply
{
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSizeLarge
(
resources
))
}
if
(
timer
==
null
)
initTimer
()
timer
?.
start
()
if
(
colorAnimation
==
null
)
initColorAnimation
()
colorAnimation
?.
start
()
}
}
fun
cancel
(
state
:
State
)
{
fun
cancel
(
state
:
State
)
{
this
.
state
=
state
this
.
state
=
state
setResult
()
setResult
()
...
@@ -97,27 +105,36 @@ class TimerFragment : Fragment() {
...
@@ -97,27 +105,36 @@ class TimerFragment : Fragment() {
private
fun
setResult
()
{
private
fun
setResult
()
{
if
(
isAdded
)
{
if
(
isAdded
)
{
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
mView
.
tv_response
textSize
(
resources
)
.
apply
{
text
=
""
}
)
.
apply
{
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSizeSmall
(
resources
))
}
tvResponse
.
text
=
state
.
response
.
apply
{
text
=
state
.
response
}
}
}
}
}
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
)
colorFrom
=
savedInstanceState
.
getInt
(
KEY_COLOR_FROM
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
,
0
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
if
(
state
==
State
.
IN_PROGRESS
)
startNewRound
()
else
{
setResult
()
setBackgroundFilter
(
colorFrom
)
}
}
}
private
fun
initTimer
()
{
private
fun
initTimer
()
{
timer
=
object
:
CountDownTimer
(
millisToSolve
,
interval
)
{
timer
=
object
:
CountDownTimer
(
millisToSolve
,
interval
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
millisToSolve
=
millisUntilFinished
millisToSolve
=
millisUntilFinished
tvR
esponse
.
text
=
(
millisUntilFinished
/
interval
).
toString
()
mView
.
tv_r
esponse
.
text
=
(
millisUntilFinished
/
interval
).
toString
()
}
}
override
fun
onFinish
()
{
override
fun
onFinish
()
{
...
...
app/src/main/res/layout-land/fragment_timer.xml
View file @
c183bc06
...
@@ -19,7 +19,7 @@
...
@@ -19,7 +19,7 @@
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:gravity=
"center"
android:textColor=
"@android:color/white"
android:textColor=
"@android:color/white"
android:textSize=
"
60sp
"
android:textSize=
"
@dimen/response_large
"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toStartOf=
"@+id/guideline2"
app:layout_constraintEnd_toStartOf=
"@+id/guideline2"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
...
...
app/src/main/res/layout/fragment_timer.xml
View file @
c183bc06
...
@@ -13,7 +13,7 @@
...
@@ -13,7 +13,7 @@
android:elevation=
"2dp"
android:elevation=
"2dp"
android:gravity=
"center"
android:gravity=
"center"
android:textColor=
"@android:color/white"
android:textColor=
"@android:color/white"
android:textSize=
"
60sp
"
android:textSize=
"
@dimen/response_large
"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
...
...
app/src/main/res/values/dimens.xml
View file @
c183bc06
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources>
<dimen
name=
"response_text_size"
>
40sp
</dimen>
<dimen
name=
"response_small"
>
40sp
</dimen>
<dimen
name=
"response_large"
>
60sp
</dimen>
</resources>
</resources>
\ 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