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() {
}
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
(
"sec_to_solve"
,
secToSolve
)
}
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 {
return
ContextCompat
.
getColor
(
context
,
id
)
}
fun
textSize
(
resources
:
Resources
):
Float
{
fun
textSize
Small
(
resources
:
Resources
):
Float
{
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
)
{
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
View file @
c183bc06
...
...
@@ -15,6 +15,7 @@ class AnswersFragment : Fragment() {
private
lateinit
var
answerSet
:
AnswerSet
private
lateinit
var
mView
:
View
private
var
numDigits
:
Int
=
0
private
var
isReady
:
Boolean
=
false
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
...
...
@@ -28,9 +29,21 @@ class AnswersFragment : Fragment() {
):
View
?
{
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_answers
,
container
,
false
)
mView
=
view
isReady
=
true
startNewRound
()
return
view
}
fun
startNewRound
()
{
if
(
isReady
)
{
answerSet
=
AnswerSet
(
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
->
answer
.
card
.
setOnCheckedChangeListener
{
_
,
isChecked
->
...
...
@@ -38,8 +51,7 @@ class AnswersFragment : Fragment() {
}
answer
.
card
.
initial
(
context
!!
,
answer
.
value
)
}
return
view
}
}
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
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.addButtonNextFragment
import
com.paktalin.quickmax.removeButtonNextFragment
import
com.paktalin.quickmax.task.model.Answer
import
com.paktalin.quickmax.task.model.AnswerSet
import
kotlinx.android.synthetic.main.activity_task.*
...
...
@@ -35,33 +34,40 @@ class TaskActivity : AppCompatActivity() {
)
}
if
(
savedInstanceState
!=
null
)
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
else
{
if
(
savedInstanceState
!=
null
)
{
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
answersFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"answers_fragment"
)
as
AnswersFragment
}
else
{
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
()
}
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
supportFragmentManager
.
putFragment
(
outState
,
"timer_fragment"
,
timerFragment
!!
)
supportFragmentManager
.
putFragment
(
outState
,
"timer_fragment"
,
timerFragment
)
supportFragmentManager
.
putFragment
(
outState
,
"answers_fragment"
,
answersFragment
)
// TODO save selection
}
internal
fun
startNewRound
()
{
timerFragment
=
TimerFragment
().
apply
{
arguments
=
Bundle
().
apply
{
putLong
(
"millis_to_solve"
,
millisToSolve
)
}
}
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"
)
}
timerFragment
.
startNewRound
()
answersFragment
.
startNewRound
()
removeButtonNextFragment
(
supportFragmentManager
)
}
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
View file @
c183bc06
...
...
@@ -10,11 +10,12 @@ import android.util.TypedValue
import
android.view.LayoutInflater
import
android.view.View
import
android.view.ViewGroup
import
android.widget.TextView
import
androidx.fragment.app.Fragment
import
com.paktalin.quickmax.R
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
...
...
@@ -23,6 +24,7 @@ private const val KEY_COLOR_FROM = "color_from"
private
const
val
KEY_STATE
=
"state"
enum
class
State
(
val
response
:
String
?)
{
NONE
(
null
),
IN_PROGRESS
(
null
),
CORRECT
(
"Correct!"
),
WRONG
(
"Wrong!"
),
...
...
@@ -31,7 +33,6 @@ enum class State(val response: String?) {
class
TimerFragment
:
Fragment
()
{
private
lateinit
var
tvResponse
:
TextView
private
lateinit
var
state
:
State
private
lateinit
var
mView
:
View
...
...
@@ -41,12 +42,13 @@ class TimerFragment : Fragment() {
private
var
colorAnimation
:
ValueAnimator
?
=
null
private
var
timer
:
CountDownTimer
?
=
null
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
millisToSolve
=
arguments
!!
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
colorFrom
=
Color
.
TRANSPARENT
state
=
State
.
IN_PROGRESS
state
=
State
.
NONE
}
override
fun
onCreateView
(
...
...
@@ -56,26 +58,14 @@ class TimerFragment : Fragment() {
):
View
?
{
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_timer
,
container
,
false
)
mView
=
view
tvResponse
=
mView
.
findViewById
(
R
.
id
.
tv_response
)
if
(
savedInstanceState
!=
null
)
restoreState
(
savedInstanceState
)
if
(
state
==
State
.
IN_PROGRESS
)
{
initTimer
()
initColorAnimation
()
}
else
{
setResult
()
setBackgroundFilter
(
colorFrom
)
}
else
startNewRound
()
return
view
}
override
fun
onStart
()
{
super
.
onStart
()
timer
?.
start
()
colorAnimation
?.
start
()
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
outState
.
putString
(
KEY_STATE
,
state
.
name
)
...
...
@@ -89,6 +79,24 @@ class TimerFragment : Fragment() {
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
)
{
this
.
state
=
state
setResult
()
...
...
@@ -97,27 +105,36 @@ class TimerFragment : Fragment() {
private
fun
setResult
()
{
if
(
isAdded
)
{
tvResponse
.
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSize
(
resources
)
)
tvResponse
.
text
=
state
.
response
mView
.
tv_response
.
apply
{
text
=
""
}
.
apply
{
setTextSize
(
TypedValue
.
COMPLEX_UNIT_SP
,
textSizeSmall
(
resources
))
}
.
apply
{
text
=
state
.
response
}
}
}
private
val
setBackgroundFilter
=
{
color
:
Int
->
if
(
isAdded
)
mView
.
background
.
setColorFilter
(
color
,
PorterDuff
.
Mode
.
SRC_ATOP
)
}
private
val
startTimer
=
{}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
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
()
{
timer
=
object
:
CountDownTimer
(
millisToSolve
,
interval
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
millisToSolve
=
millisUntilFinished
tvR
esponse
.
text
=
(
millisUntilFinished
/
interval
).
toString
()
mView
.
tv_r
esponse
.
text
=
(
millisUntilFinished
/
interval
).
toString
()
}
override
fun
onFinish
()
{
...
...
app/src/main/res/layout-land/fragment_timer.xml
View file @
c183bc06
...
...
@@ -19,7 +19,7 @@
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:textColor=
"@android:color/white"
android:textSize=
"
60sp
"
android:textSize=
"
@dimen/response_large
"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toStartOf=
"@+id/guideline2"
app:layout_constraintStart_toStartOf=
"parent"
...
...
app/src/main/res/layout/fragment_timer.xml
View file @
c183bc06
...
...
@@ -13,7 +13,7 @@
android:elevation=
"2dp"
android:gravity=
"center"
android:textColor=
"@android:color/white"
android:textSize=
"
60sp
"
android:textSize=
"
@dimen/response_large
"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
...
...
app/src/main/res/values/dimens.xml
View file @
c183bc06
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen
name=
"response_text_size"
>
40sp
</dimen>
<dimen
name=
"response_small"
>
40sp
</dimen>
<dimen
name=
"response_large"
>
60sp
</dimen>
</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