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
87a902a0
authored
Sep 28, 2019
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in MainActivity
parent
be189be0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
30 deletions
app/src/main/java/com/paktalin/quickmax/MainActivity.kt
app/src/main/java/com/paktalin/quickmax/MainActivity.kt
View file @
87a902a0
...
...
@@ -12,8 +12,7 @@ class MainActivity : AppCompatActivity() {
private
var
secToSolve
:
Int
=
4
private
var
numDigits
:
Int
=
3
private
lateinit
var
checkedCard
:
MaterialCardView
private
lateinit
var
cards
:
List
<
MaterialCardView
>
private
lateinit
var
cards
:
Map
<
Int
,
MaterialCardView
>
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
...
...
@@ -22,7 +21,12 @@ class MainActivity : AppCompatActivity() {
if
(
savedInstanceState
==
null
)
retrieveSharedPrefs
()
else
restoreInstanceState
(
savedInstanceState
)
checkedCard
.
isChecked
=
true
cards
=
mapOf
(
2
to
card_2_digits
,
3
to
card_3_digits
,
4
to
card_4_digits
)
cards
.
values
.
forEach
{
card
->
card
.
setOnClickListener
{
card
.
isChecked
=
true
}
card
.
setOnCheckedChangeListener
(
checkedChangeListener
)
}
cards
.
getValue
(
numDigits
).
isChecked
=
true
seek_bar
.
apply
{
setMinStartValue
(
secToSolve
.
toFloat
()).
apply
()
...
...
@@ -32,14 +36,10 @@ class MainActivity : AppCompatActivity() {
}
}
cards
=
listOf
(
card_2_digits
,
card_3_digits
,
card_4_digits
)
cards
.
forEach
{
card
->
card
.
setOnClickListener
(
cardOnClickListener
)
}
btn_play
.
setOnClickListener
(
playClickListener
)
}
private
fun
restoreInstanceState
(
savedInstanceState
:
Bundle
)
{
checkedCard
=
findViewById
(
savedInstanceState
.
getInt
(
"checked_num_id"
))
secToSolve
=
savedInstanceState
.
getInt
(
"sec_to_solve"
)
numDigits
=
savedInstanceState
.
getInt
(
"num_digits"
)
}
...
...
@@ -48,24 +48,18 @@ class MainActivity : AppCompatActivity() {
super
.
onSaveInstanceState
(
outState
)
outState
.
putInt
(
"num_digits"
,
numDigits
)
outState
.
putInt
(
"sec_to_solve"
,
secToSolve
)
outState
.
putInt
(
"checked_num_id"
,
checkedCard
.
id
)
}
private
val
cardOnClickListener
=
View
.
OnClickListener
{
card
->
val
cardM
=
card
as
MaterialCardView
if
(!
cardM
.
isChecked
)
{
checkedCard
=
cardM
checkedCard
.
isChecked
=
true
numDigits
=
numDigitsFromCard
(
checkedCard
)
cards
.
forEach
{
c
->
if
(
c
.
id
!=
checkedCard
.
id
)
c
.
isChecked
=
false
private
val
checkedChangeListener
=
MaterialCardView
.
OnCheckedChangeListener
{
card
,
isChecked
->
if
(
isChecked
&&
card
!=
cards
.
getValue
(
numDigits
))
{
numDigits
=
getTextView
(
card
).
text
.
toString
().
toInt
()
cards
.
values
.
forEach
{
c
->
if
(
c
!=
card
)
c
.
isChecked
=
false
}
}
}
private
val
playClickListener
=
View
.
OnClickListener
{
saveSelectedValues
()
val
intent
=
Intent
(
this
,
TaskActivity
::
class
.
java
).
also
{
i
->
i
.
putExtra
(
"num_digits"
,
numDigits
)
i
.
putExtra
(
"sec_to_solve"
,
secToSolve
)
...
...
@@ -73,26 +67,21 @@ class MainActivity : AppCompatActivity() {
startActivity
(
intent
)
}
override
fun
onStop
()
{
super
.
onStop
()
saveSelectedValues
()
}
private
fun
saveSelectedValues
()
{
val
editor
=
getSharedPreferences
(
"my_prefs"
,
Context
.
MODE_PRIVATE
).
edit
()
editor
.
putInt
(
"sec_to_solve"
,
secToSolve
)
editor
.
putInt
(
"
checked_num_id"
,
checkedCard
.
id
)
editor
.
putInt
(
"
num_digits"
,
numDigits
)
editor
.
apply
()
}
private
fun
retrieveSharedPrefs
()
{
val
prefs
=
getSharedPreferences
(
"my_prefs"
,
Context
.
MODE_PRIVATE
)
secToSolve
=
prefs
.
getInt
(
"sec_to_solve"
,
4
)
checkedCard
=
findViewById
(
prefs
.
getInt
(
"checked_num_id"
,
R
.
id
.
card_3_digits
)
)
numDigits
=
numDigitsFromCard
(
checkedCard
)
}
private
fun
numDigitsFromCard
(
card
:
MaterialCardView
):
Int
{
return
getTextView
(
card
).
text
.
toString
().
toInt
()
numDigits
=
prefs
.
getInt
(
"num_digits"
,
3
)
}
}
\ 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