Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advance Navigation and ViewModel
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
android
Advance Navigation and ViewModel
Commits
97bc0faf
Commit
97bc0faf
authored
2 years ago
by
62160052
Browse files
Options
Downloads
Patches
Plain Diff
Update StartFragment to use view model
parent
265f23b8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/src/main/java/com/example/cupcake/StartFragment.kt
+8
-0
8 additions, 0 deletions
app/src/main/java/com/example/cupcake/StartFragment.kt
app/src/main/java/com/example/cupcake/model/OrderViewModel.kt
+4
-0
4 additions, 0 deletions
...src/main/java/com/example/cupcake/model/OrderViewModel.kt
with
12 additions
and
0 deletions
app/src/main/java/com/example/cupcake/StartFragment.kt
+
8
−
0
View file @
97bc0faf
...
@@ -15,12 +15,14 @@
...
@@ -15,12 +15,14 @@
*/
*/
package
com.example.cupcake
package
com.example.cupcake
import
OrderViewModel
import
android.os.Bundle
import
android.os.Bundle
import
android.view.LayoutInflater
import
android.view.LayoutInflater
import
android.view.View
import
android.view.View
import
android.view.ViewGroup
import
android.view.ViewGroup
import
android.widget.Toast
import
android.widget.Toast
import
androidx.fragment.app.Fragment
import
androidx.fragment.app.Fragment
import
androidx.fragment.app.activityViewModels
import
androidx.navigation.fragment.findNavController
import
androidx.navigation.fragment.findNavController
import
com.example.cupcake.databinding.FragmentStartBinding
import
com.example.cupcake.databinding.FragmentStartBinding
...
@@ -33,6 +35,7 @@ class StartFragment : Fragment() {
...
@@ -33,6 +35,7 @@ class StartFragment : Fragment() {
// This property is non-null between the onCreateView() and onDestroyView() lifecycle callbacks,
// This property is non-null between the onCreateView() and onDestroyView() lifecycle callbacks,
// when the view hierarchy is attached to the fragment.
// when the view hierarchy is attached to the fragment.
private
var
binding
:
FragmentStartBinding
?
=
null
private
var
binding
:
FragmentStartBinding
?
=
null
private
val
sharedViewModel
:
OrderViewModel
by
activityViewModels
()
override
fun
onCreateView
(
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
...
@@ -58,9 +61,14 @@ class StartFragment : Fragment() {
...
@@ -58,9 +61,14 @@ class StartFragment : Fragment() {
* Start an order with the desired quantity of cupcakes and navigate to the next screen.
* Start an order with the desired quantity of cupcakes and navigate to the next screen.
*/
*/
fun
orderCupcake
(
quantity
:
Int
)
{
fun
orderCupcake
(
quantity
:
Int
)
{
sharedViewModel
.
setQuantity
(
quantity
)
if
(
sharedViewModel
.
hasNoFlavorSet
())
{
sharedViewModel
.
setFlavor
(
getString
(
R
.
string
.
vanilla
))
}
findNavController
().
navigate
(
R
.
id
.
action_startFragment_to_flavorFragment
)
findNavController
().
navigate
(
R
.
id
.
action_startFragment_to_flavorFragment
)
}
}
/**
/**
* This fragment lifecycle method is called when the view hierarchy associated with the fragment
* This fragment lifecycle method is called when the view hierarchy associated with the fragment
* is being removed. As a result, clear out the binding object.
* is being removed. As a result, clear out the binding object.
...
...
This diff is collapsed.
Click to expand it.
app/src/main/java/com/example/cupcake/model/OrderViewModel.kt
+
4
−
0
View file @
97bc0faf
...
@@ -26,4 +26,8 @@ class OrderViewModel : ViewModel() {
...
@@ -26,4 +26,8 @@ class OrderViewModel : ViewModel() {
fun
setDate
(
pickupDate
:
String
)
{
fun
setDate
(
pickupDate
:
String
)
{
_date
.
value
=
pickupDate
_date
.
value
=
pickupDate
}
}
fun
hasNoFlavorSet
():
Boolean
{
return
_flavor
.
value
.
isNullOrEmpty
()
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment