Skip to content

Commit

Permalink
show toolbar in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Jun 2, 2024
1 parent a4df6b3 commit a5b0157
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
26 changes: 18 additions & 8 deletions app/src/main/java/de/felixnuesse/timedsilence/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.felixnuesse.timedsilence

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import de.felixnuesse.timedintenttrigger.database.xml.Exporter
import de.felixnuesse.timedintenttrigger.database.xml.Importer

import de.felixnuesse.timedsilence.databinding.ActivitySettingsBinding
import de.felixnuesse.timedsilence.fragments.settings.SelectorFragment


Expand All @@ -17,31 +17,41 @@ class SettingsActivity : AppCompatActivity() {
private var mSelectorFragment = SelectorFragment(this)
private var mCurrentFragment: Fragment = mSelectorFragment
private var mOnBackCallback: OnBackPressedCallback? = null
private lateinit var binding: ActivitySettingsBinding


private var mExporter = Exporter(this)
private var mImporter = Importer(this)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
openFragment(mCurrentFragment)
setSupportActionBar(binding.toolbar)

supportActionBar?.setDisplayHomeAsUpEnabled(true)

mOnBackCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(mCurrentFragment is SelectorFragment) {
finish()
}
openFragment(mSelectorFragment)
back()
}
}

onBackPressedDispatcher.addCallback(this, mOnBackCallback as OnBackPressedCallback)
}

private fun back() {
if(mCurrentFragment is SelectorFragment) {
finish()
}
openFragment(mSelectorFragment)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
back()
true
}
else -> super.onOptionsItemSelected(item)
Expand All @@ -57,7 +67,7 @@ class SettingsActivity : AppCompatActivity() {
fun openFragment(fragment: Fragment) {
supportFragmentManager
.beginTransaction()
.replace(android.R.id.content, fragment)
.replace(R.id.content, fragment)
.commit()
mCurrentFragment = fragment

Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/action_settings" />

<FrameLayout
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit a5b0157

Please sign in to comment.