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
94774942
authored
Sep 29, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in AnswerSet and Fragment
parent
bc85a28a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
21 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/model/AnswerSet.kt
View file @
94774942
package
com.paktalin.quickmax.task.model
import
android.view.View
import
com.paktalin.quickmax.task.ui.AnswerCardView
import
kotlinx.android.synthetic.main.fragment_answers.view.*
private
const
val
NUM_ANSWERS
=
4
class
AnswerSet
:
Iterable
<
Answer
>
{
private
lateinit
var
correctAnswer
:
Answer
val
answers
:
MutableList
<
Answer
>
=
mutableListOf
()
constructor
(
numDigits
:
Int
,
cards
:
List
<
AnswerCardView
>)
{
constructor
(
numDigits
:
Int
,
view
:
View
)
{
val
cards
=
cardsFromView
(
view
)
val
randomNumbers
=
generateNRandomNumbers
(
numDigits
,
NUM_ANSWERS
)
for
(
i
in
0
until
NUM_ANSWERS
)
answers
.
add
(
Answer
(
cards
[
i
],
randomNumbers
.
elementAt
(
i
)))
setCorrectAnswer
()
}
constructor
(
answerValues
:
IntArray
,
cards
:
List
<
AnswerCardView
>)
{
constructor
(
answerValues
:
IntArray
,
view
:
View
)
{
val
cards
=
cardsFromView
(
view
)
for
(
i
in
answerValues
.
indices
)
answers
.
add
(
Answer
(
cards
[
i
],
answerValues
[
i
]))
setCorrectAnswer
()
...
...
@@ -25,10 +28,7 @@ class AnswerSet: Iterable<Answer> {
private
fun
setCorrectAnswer
()
{
val
correctAnswerNum
=
findSecondMax
(
answers
.
map
{
answer
->
answer
.
value
})
answers
.
forEach
{
answer
->
if
(
answer
.
value
==
correctAnswerNum
)
{
answer
.
correct
correctAnswer
=
answer
}
answer
.
correct
=
answer
.
value
==
correctAnswerNum
}
}
...
...
@@ -36,4 +36,13 @@ class AnswerSet: Iterable<Answer> {
return
answers
.
iterator
()
}
private
fun
cardsFromView
(
view
:
View
):
List
<
AnswerCardView
>
{
return
listOf
(
view
.
card_left_top
,
view
.
card_right_top
,
view
.
card_left_bottom
,
view
.
card_right_bottom
)
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/quickmax/task/ui/AnswersFragment.kt
View file @
94774942
...
...
@@ -17,8 +17,6 @@ class AnswersFragment : Fragment() {
private
var
numDigits
:
Int
=
0
private
var
isReady
:
Boolean
=
false
private
lateinit
var
cards
:
List
<
AnswerCardView
>
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
numDigits
=
arguments
!!
.
getInt
(
"num_digits"
)
...
...
@@ -32,19 +30,10 @@ class AnswersFragment : Fragment() {
val
view
=
inflater
.
inflate
(
R
.
layout
.
fragment_answers
,
container
,
false
)
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
()
}
else
answerSet
=
AnswerSet
(
numDigits
,
mView
)
isReady
=
true
startNewRound
()
return
view
...
...
@@ -66,7 +55,8 @@ class AnswersFragment : Fragment() {
}
private
fun
restoreState
(
savedInstanceState
:
Bundle
)
{
answerSet
=
AnswerSet
(
savedInstanceState
.
getIntArray
(
"answers"
)
!!
,
cards
)
val
restoredAnswers
=
savedInstanceState
.
getIntArray
(
"answers"
)
!!
answerSet
=
AnswerSet
(
restoredAnswers
,
mView
)
}
private
fun
processAnswer
(
answer
:
Answer
)
{
...
...
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