-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstFragment.kt
83 lines (57 loc) · 2.83 KB
/
FirstFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.example.obesitron.fragments
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.obesitron.R
import com.example.obesitron.databinding.FragmentFirstBinding
import com.example.obesitron.databinding.FragmentSecondBinding
class FirstFragment : Fragment() {
companion object { private const val TAG = "FirstFragment" }
private var _binding1: FragmentFirstBinding? = null
private var _binding2: FragmentSecondBinding? = null
private val binding1 get() = _binding1!!
private val binding2 get() = _binding2!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val prefs = requireActivity().getSharedPreferences("myPrefs", Context.MODE_PRIVATE)
val editor = prefs.edit()
if(!prefs.contains("NbUse")) {
_binding1 = FragmentFirstBinding.inflate(inflater, container, false)
binding1.btnSubmit.setOnClickListener {
if(binding1.textInputName.text.isNullOrEmpty() || binding1.textInputEditAge.text.isNullOrEmpty() || binding1.textInputEditWeight.text.isNullOrEmpty() || binding1.textInputEditSurname.text.isNullOrEmpty()) { }
else {
editor.putString("Nom", binding1.textInputName.text.toString())
editor.putString("Prenom", binding1.textInputEditSurname.text.toString())
editor.putString("Age", binding1.textInputEditAge.text.toString())
editor.putString("Poids", binding1.textInputEditWeight.text.toString())
editor.putInt("NbUse", 1)
editor.commit()
findNavController().navigate(R.id.action_FirstFragment_to_HomeFragment)
}
}
return binding1.root
}
else {
_binding2 = FragmentSecondBinding.inflate(inflater, container, false)
binding2.button2.setOnClickListener { findNavController().navigate(R.id.action_FirstFragment_to_HomeFragment) }
var nbUse = prefs.getInt("NbUse", 0)
nbUse += 1
prefs.edit().remove("NbUse").commit()
prefs.edit().putInt("NbUse", nbUse).commit()
Log.d(TAG, prefs.getInt("NbUse", 0).toString())
return binding2.root
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroyView() {
super.onDestroyView()
_binding1 = null
_binding2 = null
}
}