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
bc85a28a
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP saving AnswerFragment state: values saved
parent
bfcc0b31
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
175 deletions
app/src/main/java/com/paktalin/quickmax/task/model/AnswerSet.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/activity_task.xml
app/src/main/res/layout/activity_task.xml
app/src/main/java/com/paktalin/quickmax/task/model/AnswerSet.kt
View file @
bc85a28a
...
@@ -2,28 +2,33 @@ package com.paktalin.quickmax.task.model
...
@@ -2,28 +2,33 @@ package com.paktalin.quickmax.task.model
import
com.paktalin.quickmax.task.ui.AnswerCardView
import
com.paktalin.quickmax.task.ui.AnswerCardView
class
AnswerSet
(
numDigits
:
Int
,
cards
:
List
<
AnswerCardView
>):
Iterable
<
Answer
>
{
private
const
val
NUM_ANSWERS
=
4
private
val
numAnswers
=
4
class
AnswerSet
:
Iterable
<
Answer
>
{
private
lateinit
var
correctAnswer
:
Answer
private
lateinit
var
correctAnswer
:
Answer
val
answers
:
MutableList
<
Answer
>
=
mutableListOf
()
val
answers
:
MutableList
<
Answer
>
=
mutableListOf
()
init
{
val
randomNumbers
=
constructor
(
numDigits
:
Int
,
cards
:
List
<
AnswerCardView
>)
{
generateNRandomNumbers
(
numDigits
,
numAnswers
)
val
randomNumbers
=
generateNRandomNumbers
(
numDigits
,
NUM_ANSWERS
)
for
(
i
in
0
until
numAnswers
)
for
(
i
in
0
until
NUM_ANSWERS
)
answers
.
add
(
answers
.
add
(
Answer
(
cards
[
i
],
randomNumbers
.
elementAt
(
i
)))
Answer
(
setCorrectAnswer
()
cards
[
i
],
}
randomNumbers
.
elementAt
(
i
)
)
constructor
(
answerValues
:
IntArray
,
cards
:
List
<
AnswerCardView
>)
{
)
for
(
i
in
answerValues
.
indices
)
answers
.
add
(
Answer
(
cards
[
i
],
answerValues
[
i
]))
val
correctAnswerNum
=
setCorrectAnswer
()
findSecondMax
(
answers
.
map
{
answer
->
answer
.
value
})
}
answers
.
forEach
{
n
->
n
.
correct
=
n
.
value
==
correctAnswerNum
private
fun
setCorrectAnswer
()
{
if
(
n
.
correct
)
val
correctAnswerNum
=
findSecondMax
(
answers
.
map
{
answer
->
answer
.
value
})
correctAnswer
=
n
answers
.
forEach
{
answer
->
if
(
answer
.
value
==
correctAnswerNum
)
{
answer
.
correct
correctAnswer
=
answer
}
}
}
}
}
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
View file @
bc85a28a
...
@@ -17,6 +17,8 @@ class AnswersFragment : Fragment() {
...
@@ -17,6 +17,8 @@ class AnswersFragment : Fragment() {
private
var
numDigits
:
Int
=
0
private
var
numDigits
:
Int
=
0
private
var
isReady
:
Boolean
=
false
private
var
isReady
:
Boolean
=
false
private
lateinit
var
cards
:
List
<
AnswerCardView
>
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
numDigits
=
arguments
!!
.
getInt
(
"num_digits"
)
numDigits
=
arguments
!!
.
getInt
(
"num_digits"
)
...
@@ -29,29 +31,42 @@ class AnswersFragment : Fragment() {
...
@@ -29,29 +31,42 @@ 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
cards
=
listOf
(
mView
.
card_left_top
,
mView
.
card_right_top
,
mView
.
card_left_bottom
,
mView
.
card_right_bottom
)
if
(
savedInstanceState
!=
null
)
restoreState
(
savedInstanceState
)
else
{
answerSet
=
AnswerSet
(
numDigits
,
cards
)
startNewRound
()
}
isReady
=
true
isReady
=
true
startNewRound
()
startNewRound
()
return
view
return
view
}
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
super
.
onSaveInstanceState
(
outState
)
outState
.
putIntArray
(
"answers"
,
answerSet
.
answers
.
map
{
answer
->
answer
.
value
}.
toIntArray
())
}
fun
startNewRound
()
{
fun
startNewRound
()
{
if
(
isReady
)
{
if
(
isReady
)
{
answerSet
=
AnswerSet
(
numDigits
,
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
if
(
isChecked
)
processAnswer
(
answer
)
.
apply
{
setOnCheckedChangeListener
{
_
,
isChecked
->
if
(
isChecked
)
processAnswer
(
answer
)
}
}
.
apply
{
initial
(
context
!!
,
answer
.
value
)
}
}
}
answer
.
card
.
initial
(
context
!!
,
answer
.
value
)
}
}
}
}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
answerSet
=
AnswerSet
(
savedInstanceState
.
getIntArray
(
"answers"
)
!!
,
cards
)
}
}
private
fun
processAnswer
(
answer
:
Answer
)
{
private
fun
processAnswer
(
answer
:
Answer
)
{
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/TaskActivity.kt
View file @
bc85a28a
...
@@ -35,11 +35,7 @@ class TaskActivity : AppCompatActivity() {
...
@@ -35,11 +35,7 @@ class TaskActivity : AppCompatActivity() {
}
}
if
(
savedInstanceState
!=
null
)
{
if
(
savedInstanceState
!=
null
)
{
timerFragment
=
supportFragmentManager
.
getFragment
(
restoreState
(
savedInstanceState
)
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
answersFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"answers_fragment"
)
as
AnswersFragment
}
else
{
}
else
{
retrieveExtras
()
retrieveExtras
()
timerFragment
=
TimerFragment
()
timerFragment
=
TimerFragment
()
...
@@ -54,7 +50,6 @@ class TaskActivity : AppCompatActivity() {
...
@@ -54,7 +50,6 @@ class TaskActivity : AppCompatActivity() {
}
}
startNewRound
()
startNewRound
()
}
}
}
}
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
override
fun
onSaveInstanceState
(
outState
:
Bundle
)
{
...
@@ -71,6 +66,14 @@ class TaskActivity : AppCompatActivity() {
...
@@ -71,6 +66,14 @@ class TaskActivity : AppCompatActivity() {
removeButtonNextFragment
(
supportFragmentManager
)
removeButtonNextFragment
(
supportFragmentManager
)
}
}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
timerFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"timer_fragment"
)
as
TimerFragment
answersFragment
=
supportFragmentManager
.
getFragment
(
savedInstanceState
,
"answers_fragment"
)
as
AnswersFragment
}
private
fun
retrieveExtras
()
{
private
fun
retrieveExtras
()
{
numDigits
=
intent
.
getIntExtra
(
"num_digits"
,
3
)
numDigits
=
intent
.
getIntExtra
(
"num_digits"
,
3
)
millisToSolve
=
1000
*
intent
.
getIntExtra
(
"sec_to_solve"
,
4
).
toLong
()
millisToSolve
=
1000
*
intent
.
getIntExtra
(
"sec_to_solve"
,
4
).
toLong
()
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/TimerFragment.kt
View file @
bc85a28a
...
@@ -98,20 +98,6 @@ class TimerFragment : Fragment() {
...
@@ -98,20 +98,6 @@ class TimerFragment : Fragment() {
colorAnimation
?.
cancel
()
colorAnimation
?.
cancel
()
}
}
private
fun
setResult
()
{
if
(
isAdded
)
{
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
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
state
=
State
.
valueOf
(
savedInstanceState
.
getString
(
KEY_STATE
)
!!
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
millisToSolve
=
savedInstanceState
.
getLong
(
KEY_MILLIS_TO_SOLVE
)
...
@@ -125,6 +111,19 @@ class TimerFragment : Fragment() {
...
@@ -125,6 +111,19 @@ class TimerFragment : Fragment() {
}
}
}
}
private
fun
setResult
()
{
if
(
isAdded
)
{
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
fun
initTimer
(
toSolve
:
Long
):
CountDownTimer
{
private
fun
initTimer
(
toSolve
:
Long
):
CountDownTimer
{
return
object
:
CountDownTimer
(
toSolve
,
COUNTDOWN_INTERVAL
)
{
return
object
:
CountDownTimer
(
toSolve
,
COUNTDOWN_INTERVAL
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
override
fun
onTick
(
millisUntilFinished
:
Long
)
{
...
...
app/src/main/res/layout-land/activity_task.xml
View file @
bc85a28a
...
@@ -48,7 +48,7 @@
...
@@ -48,7 +48,7 @@
app:contentPaddingLeft=
"16dp"
app:contentPaddingLeft=
"16dp"
app:contentPaddingRight=
"16dp"
app:contentPaddingRight=
"16dp"
app:contentPaddingTop=
"40dp"
app:contentPaddingTop=
"40dp"
app:layout_constraintBottom_toBottomOf=
"@+id/
layout
_answers"
app:layout_constraintBottom_toBottomOf=
"@+id/
container
_answers"
app:layout_constraintEnd_toStartOf=
"@+id/guideline"
app:layout_constraintEnd_toStartOf=
"@+id/guideline"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintVertical_chainStyle=
"packed"
>
app:layout_constraintVertical_chainStyle=
"packed"
>
...
@@ -66,8 +66,9 @@
...
@@ -66,8 +66,9 @@
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.card.MaterialCardView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id=
"@+id/layout_answers"
<FrameLayout
android:id=
"@+id/container_answers"
android:layout_width=
"0dp"
android:layout_width=
"0dp"
android:layout_height=
"200dp"
android:layout_height=
"200dp"
android:layout_marginStart=
"8dp"
android:layout_marginStart=
"8dp"
...
@@ -75,56 +76,6 @@
...
@@ -75,56 +76,6 @@
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"@+id/guideline"
app:layout_constraintStart_toStartOf=
"@+id/guideline"
app:layout_constraintTop_toTopOf=
"parent"
>
app:layout_constraintTop_toTopOf=
"parent"
/>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id=
"@+id/card_left_top"
style=
"@style/MyCard"
app:cardCornerRadius=
"10dp"
app:layout_constraintBottom_toTopOf=
"@+id/card_left_bottom"
app:layout_constraintEnd_toStartOf=
"@+id/card_right_top"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
>
<TextView
style=
"@style/AnswerCardText"
/>
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id=
"@+id/card_right_top"
style=
"@style/MyCard"
app:layout_constraintBottom_toTopOf=
"@id/card_right_bottom"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toEndOf=
"@+id/card_left_top"
app:layout_constraintTop_toTopOf=
"parent"
>
<TextView
style=
"@style/AnswerCardText"
/>
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id=
"@+id/card_left_bottom"
style=
"@style/MyCard"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toStartOf=
"@+id/card_right_bottom"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/card_left_top"
>
<TextView
style=
"@style/AnswerCardText"
/>
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id=
"@+id/card_right_bottom"
style=
"@style/MyCard"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toEndOf=
"@+id/card_left_bottom"
app:layout_constraintTop_toBottomOf=
"@+id/card_right_top"
>
<TextView
style=
"@style/AnswerCardText"
/>
</com.paktalin.quickmax.task.ui.AnswerCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
app/src/main/res/layout/activity_task.xml
View file @
bc85a28a
...
@@ -82,73 +82,4 @@
...
@@ -82,73 +82,4 @@
app:layout_constraintVertical_bias=
"0.5"
app:layout_constraintVertical_bias=
"0.5"
app:layout_constraintVertical_chainStyle=
"packed"
/>
app:layout_constraintVertical_chainStyle=
"packed"
/>
<!--<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_answers"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cv_task"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed">
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_top"
style="@style/MyCard"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toTopOf="@+id/card_left_bottom"
app:layout_constraintEnd_toStartOf="@+id/card_right_top"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_top"
style="@style/MyCard"
app:layout_constraintBottom_toTopOf="@id/card_right_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/card_left_top"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/card_right_bottom"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_left_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/card_left_bottom"
app:layout_constraintTop_toBottomOf="@+id/card_right_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
</androidx.constraintlayout.widget.ConstraintLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ 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