Ajout d’une toolbar.

This commit is contained in:
biloute02 2024-01-12 15:05:46 +01:00
parent e81f70f045
commit 652b46f4c5
3 changed files with 76 additions and 21 deletions

View file

@ -3,8 +3,14 @@ package com.example.palto
import android.nfc.NfcAdapter import android.nfc.NfcAdapter
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.appcompat.app.AppCompatActivity import android.view.Menu
import android.view.MenuInflater
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.example.palto.databinding.ActivityPaltoBinding
import com.example.palto.ui.NfcViewModel import com.example.palto.ui.NfcViewModel
import com.example.palto.ui.UserViewModel import com.example.palto.ui.UserViewModel
@ -14,31 +20,65 @@ class PaltoActivity : AppCompatActivity() {
private var nfcAdapter: NfcAdapter? = null private var nfcAdapter: NfcAdapter? = null
private val nfcViewModel: NfcViewModel by viewModels() private val nfcViewModel: NfcViewModel by viewModels()
private val userViewModel: UserViewModel by viewModels()
private val userViewModel: UserViewModel by viewModels() { UserViewModel.Factory }
private lateinit var binding: ActivityPaltoBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// get the NFC Adapter binding = ActivityPaltoBinding.inflate(layoutInflater)
nfcAdapter = NfcAdapter.getDefaultAdapter(this) setContentView(binding.root)
// check if NFC is supported //
// Toolbar
//
// Set the toolbar as the app bar for the activity.
setSupportActionBar(binding.paltoToolbar)
// Configure the app bar
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.palto_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.menuFragment, R.id.loginFragment
)
)
binding.paltoToolbar.setupWithNavController(navController, appBarConfiguration)
//
// NFC Adapter
//
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
// Check if NFC is supported (already checked in the app manifest).
if (nfcAdapter == null) { if (nfcAdapter == null) {
Log.e("NFC", "NFC is not supported") Log.e("NFC", "NFC is not supported")
return
} }
// check if NFC is disabled
if (nfcAdapter?.isEnabled == false) { if (nfcAdapter?.isEnabled == false) {
Log.w("NFC", "NFC is not enabled") Log.w("NFC", "NFC is not enabled")
} }
setContentView(R.layout.activity_palto)
} }
/**
* Specify the options menu for the Activity.
*/
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.palto_menu, menu)
return true
}
/**
* Just before the application is displayed.
*/
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// Begin to read NFC Cards.
nfcAdapter?.enableReaderMode( nfcAdapter?.enableReaderMode(
this, this,
nfcViewModel.tag::postValue, nfcViewModel.tag::postValue,
@ -47,17 +87,13 @@ class PaltoActivity : AppCompatActivity() {
) )
} }
/**
* Just after the application has been quit.
*/
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
// disable the NFC discovery // Disable the NFC discovery.
nfcAdapter?.disableReaderMode(this) nfcAdapter?.disableReaderMode(this)
} }
/*
@OptIn(ExperimentalStdlibApi::class)
fun processTag(tag: Tag) {
Log.d("NFC", "Tag ID : " + tag.id.toHexString())
}
*/
} }

View file

@ -1,12 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/palto_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView" android:id="@+id/palto_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
@ -14,6 +24,7 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toBottomOf="@+id/palto_toolbar"
app:navGraph="@navigation/nav_graph" /> app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="@string/menu_item_logout" />
</menu>