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
dd87465c
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP saving AnswerFragment state
parent
94774942
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
22 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/model/AnswerSet.kt
View file @
dd87465c
package
com.paktalin.quickmax.task.model
import
android.content.Context
import
android.view.View
import
com.paktalin.quickmax.task.ui.AnswerCardView
import
kotlinx.android.synthetic.main.fragment_answers.view.*
...
...
@@ -25,13 +26,21 @@ class AnswerSet: Iterable<Answer> {
setCorrectAnswer
()
}
private
fun
setCorrectAnswer
()
{
val
correctAnswerNum
=
findSecondMax
(
answers
.
map
{
answer
->
answer
.
value
})
fun
setOnCheckedChangeListener
(
onChecked
:
(
answer
:
Answer
)
->
Unit
)
{
answers
.
forEach
{
answer
->
answer
.
c
orrect
=
answer
.
value
==
correctAnswerNum
answer
.
c
ard
.
setOnCheckedChangeListener
{
_
,
isChecked
->
if
(
isChecked
)
onChecked
(
answer
)
}
}
}
fun
setInitialStyle
(
context
:
Context
)
{
answers
.
forEach
{
answer
->
answer
.
card
.
initial
(
context
,
answer
.
value
)
}
}
private
fun
setCorrectAnswer
()
{
val
correctAnswerNum
=
findSecondMax
(
answers
.
map
{
answer
->
answer
.
value
})
answers
.
find
{
answer
->
answer
.
value
==
correctAnswerNum
}
!!
.
correct
=
true
}
override
fun
iterator
():
Iterator
<
Answer
>
{
return
answers
.
iterator
()
}
...
...
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
View file @
dd87465c
...
...
@@ -6,16 +6,13 @@ import android.view.View
import
android.view.ViewGroup
import
androidx.fragment.app.Fragment
import
com.paktalin.quickmax.R
import
com.paktalin.quickmax.addButtonNextFragment
import
com.paktalin.quickmax.task.model.Answer
import
com.paktalin.quickmax.task.model.AnswerSet
import
kotlinx.android.synthetic.main.fragment_answers.view.*
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
)
...
...
@@ -33,9 +30,7 @@ class AnswersFragment : Fragment() {
if
(
savedInstanceState
!=
null
)
restoreState
(
savedInstanceState
)
else
answerSet
=
AnswerSet
(
numDigits
,
mView
)
isReady
=
true
startNewRound
()
startNewRound
()
return
view
}
...
...
@@ -44,19 +39,21 @@ class AnswersFragment : Fragment() {
outState
.
putIntArray
(
"answers"
,
answerSet
.
answers
.
map
{
answer
->
answer
.
value
}.
toIntArray
())
}
fun
startNewRound
()
{
if
(
isReady
)
{
answerSet
.
forEach
{
answer
->
answer
.
card
.
apply
{
setOnCheckedChangeListener
{
_
,
isChecked
->
if
(
isChecked
)
processAnswer
(
answer
)
}
}
.
apply
{
initial
(
context
!!
,
answer
.
value
)
}
}
fun
startNewRound
(
answerSet
:
AnswerSet
?
=
null
)
{
if
(
isAdded
)
{
this
.
answerSet
=
answerSet
?:
AnswerSet
(
numDigits
,
mView
)
.
apply
{
setOnCheckedChangeListener
{
answer
->
processAnswer
(
answer
)
}
}
.
apply
{
setInitialStyle
(
mView
.
context
)
}
}
}
fun
disableCards
()
{
answerSet
.
forEach
{
answer
->
answer
.
card
.
disable
()
}
}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
val
restoredAnswers
=
savedInstanceState
.
getIntArray
(
"answers"
)
!!
answerSet
=
AnswerSet
(
restoredAnswers
,
mView
)
startNewRound
(
AnswerSet
(
restoredAnswers
,
mView
)
)
}
private
fun
processAnswer
(
answer
:
Answer
)
{
...
...
@@ -67,6 +64,6 @@ class AnswersFragment : Fragment() {
answer
.
card
.
markWrong
(
context
!!
)
(
activity
as
TaskActivity
).
onResponseWrong
()
}
answerSet
.
forEach
{
answer
->
answer
.
card
.
disable
()
}
disableCards
()
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/quickmax/task/ui/TaskActivity.kt
View file @
dd87465c
...
...
@@ -14,7 +14,6 @@ import kotlinx.android.synthetic.main.activity_task.*
// TODO save state
class
TaskActivity
:
AppCompatActivity
()
{
private
lateinit
var
answerSet
:
AnswerSet
private
var
millisToSolve
:
Long
=
4000
private
var
numDigits
:
Int
=
3
...
...
@@ -56,8 +55,6 @@ class TaskActivity : AppCompatActivity() {
super
.
onSaveInstanceState
(
outState
)
supportFragmentManager
.
putFragment
(
outState
,
"timer_fragment"
,
timerFragment
)
supportFragmentManager
.
putFragment
(
outState
,
"answers_fragment"
,
answersFragment
)
// TODO save selection
}
internal
fun
startNewRound
()
{
...
...
@@ -91,6 +88,6 @@ class TaskActivity : AppCompatActivity() {
fun
onTimeOver
()
{
addButtonNextFragment
(
supportFragmentManager
,
false
)
// answerSet.forEach { answer -> answer.card.disable() }
answersFragment
.
disableCards
()
}
}
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