Fragment transition animations, information dialog, SettingsFragment implements OnCheckedChangeListener

This commit is contained in:
Deniz Düzgören
2019-07-30 16:58:51 +02:00
parent 2fb0506db9
commit b82f595bdf
16 changed files with 138 additions and 343 deletions
@@ -1,37 +1,27 @@
package com.denizd.substitutionplan
import android.app.ActivityManager
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.graphics.BitmapFactory
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceManager
import android.util.AttributeSet
import android.view.*
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomsheet.BottomSheetBehavior
@@ -39,18 +29,18 @@ import com.google.android.material.chip.Chip
import com.google.android.material.snackbar.Snackbar
import com.jaredrummler.android.device.DeviceName
import java.lang.IllegalArgumentException
import java.text.SimpleDateFormat
import java.util.*
class Main : AppCompatActivity(R.layout.activity_main) {
class Main : AppCompatActivity(R.layout.app_bar_main) {
private var bottomSheetExpanded = false
private lateinit var bottomSheetBehaviour: BottomSheetBehavior<*>
private lateinit var context: Context
private lateinit var prefs: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = this as Context
val prefs = PreferenceManager.getDefaultSharedPreferences(context) as SharedPreferences
context = this
prefs = PreferenceManager.getDefaultSharedPreferences(context) as SharedPreferences
val edit = prefs.edit()
val firstTime = Intent(context, FirstTime::class.java)
@@ -67,19 +57,17 @@ class Main : AppCompatActivity(R.layout.activity_main) {
val appbarlayout = findViewById<AppBarLayout>(R.id.appbarlayout)
val toolbarTxt = findViewById<TextView>(R.id.toolbarTxt)
val bottomSheetRoot = findViewById<LinearLayout>(R.id.bottom_sheet)
bottomSheetBehaviour = BottomSheetBehavior.from(bottomSheetRoot)
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
val fragmentContainer = findViewById<CoordinatorLayout>(R.id.fragment_container)
val bottomSheetCloser = findViewById<LinearLayout>(R.id.bottom_sheet_closer)
val chip = findViewById<Chip>(R.id.chip)
val contextView = findViewById<View>(R.id.coordination)
val window = this.window as Window
bottomSheetExpanded = bottomSheetBehaviour.state == BottomSheetBehavior.STATE_EXPANDED
setTheme(R.style.AppTheme0)
if (prefs.getBoolean("openInfo", false)) {
openInfoDialog()
}
when (prefs.getInt("themeInt", 0)) {
0 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
@@ -109,42 +97,10 @@ class Main : AppCompatActivity(R.layout.activity_main) {
// } // TODO theming based on system settings for Android Q
}
if (!prefs.getBoolean("showinfotab", true)) {
bottomNav.menu.removeItem(R.id.openinfopanel)
}
fragmentContainer.setOnClickListener {
bottomSheetBehaviour.setState(BottomSheetBehavior.STATE_COLLAPSED)
}
bottomSheetBehaviour.setBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (bottomSheetBehaviour.state == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetCloser.visibility = View.VISIBLE
} else if (bottomSheetBehaviour.state == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetCloser.visibility = View.GONE
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
bottomSheetCloser.setOnClickListener {
bottomSheetCloser.visibility = View.GONE
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}
if (!prefs.getBoolean("autoRefresh", false) && prefs.getInt("firstTimeOpening", 0) != 0) {
try {
val oldFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
val newFormat = "yyyy-MM-dd, HH:mm:ss"
val d = Date(prefs.getString("time", ""))
val sdf = SimpleDateFormat(oldFormat)
sdf.applyPattern(newFormat)
val newDate = sdf.format(d)
val sb = StringBuilder()
val updated = sb.append(getText(R.string.lastupdatedK)).append(newDate).toString()
val updated = sb.append(getText(R.string.lastupdatedK)).append(prefs.getString("time", "")).toString()
Snackbar.make(contextView, updated, Snackbar.LENGTH_LONG).show()
} catch (e: IllegalArgumentException) {}
@@ -169,7 +125,7 @@ class Main : AppCompatActivity(R.layout.activity_main) {
}
if (prefs.getBoolean("greeting", false)) {
if (prefs.getString("username", "").isNotEmpty()) {
if ((prefs.getString("username", "") ?: "").isNotEmpty()) {
val animationIn = AnimationUtils.loadAnimation(context, R.anim.chip_slide_in)
val animationOut = AnimationUtils.loadAnimation(context, R.anim.chip_slide_out)
val generator = Random()
@@ -216,43 +172,28 @@ class Main : AppCompatActivity(R.layout.activity_main) {
}
if (prefs.getInt("firstTimeOpening", 0) == 0) {
handler.postDelayed({
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
}, 600)
handler.postDelayed({
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}, 2000)
edit.putInt("firstTimeOpening", prefs.getInt("firstTimeOpening", 0) + 1).apply()
}
if (prefs.getBoolean("openInfo", false)) {
handler.postDelayed({
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
}, 600)
}
val userPlan: String = if (prefs.getString("username", "").toString().isNotEmpty()) {
when (prefs.getString("username", "")[prefs.getString("username", "").length - 1].toString().toLowerCase()) {
"s", "x", "z" -> prefs.getString("username", "") + getString(R.string.nosplan)
else -> prefs.getString("username", "") + getString(R.string.splan)
val userPlan: String = if ((prefs.getString("username", "") ?: "").toString().isNotEmpty()) {
when ((prefs.getString("username", "") ?: "")[(prefs.getString("username", "") ?: "").length - 1].toString().toLowerCase()) {
"s", "x", "z" -> (prefs.getString("username", "") ?: "") + getString(R.string.nosplan)
else -> (prefs.getString("username", "") ?: "") + getString(R.string.splan)
}
} else {
getString(R.string.personalplan)
}
lateinit var fragment: Fragment
lateinit var defaultFragment: Fragment
if (prefs.getBoolean("defaultPersonalised", false)) {
fragment = PersonalPlanFragment()
defaultFragment = PersonalPlanFragment()
bottomNav.selectedItemId = R.id.personal
toolbarTxt.text = userPlan
} else {
fragment = GeneralPlanFragment()
defaultFragment = GeneralPlanFragment()
bottomNav.selectedItemId = R.id.plan
toolbarTxt.text = getString(R.string.app_name)
}
loadFragment(fragment)
loadFragment(defaultFragment)
bottomNav.setOnNavigationItemSelectedListener { item: MenuItem ->
var fragmentLoading = true
@@ -271,11 +212,7 @@ class Main : AppCompatActivity(R.layout.activity_main) {
toolbarTxt.text = getString(R.string.foodmenu)
}
R.id.openinfopanel -> {
if (bottomSheetBehaviour.state == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
} else if (bottomSheetBehaviour.state == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}
openInfoDialog()
fragmentLoading = false
}
R.id.settings -> {
@@ -284,7 +221,6 @@ class Main : AppCompatActivity(R.layout.activity_main) {
}
}
if (fragmentLoading) {
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
appbarlayout.setExpanded(true)
loadFragment(fragment)
} else {
@@ -299,8 +235,8 @@ class Main : AppCompatActivity(R.layout.activity_main) {
val recyclerView = findViewById<RecyclerView>(R.id.linearRecycler)
recyclerView.post {
recyclerView.smoothScrollToPosition(0)
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}
appbarlayout.setExpanded(true)
} catch (e: NullPointerException) {}
}
R.id.menu -> {
@@ -308,8 +244,8 @@ class Main : AppCompatActivity(R.layout.activity_main) {
val recyclerView = findViewById<RecyclerView>(R.id.linear_food)
recyclerView.post {
recyclerView.smoothScrollToPosition(0)
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}
appbarlayout.setExpanded(true)
} catch (e: NullPointerException) {}
}
R.id.settings -> {
@@ -317,18 +253,25 @@ class Main : AppCompatActivity(R.layout.activity_main) {
val nsv = findViewById<NestedScrollView>(R.id.nsvsettings)
nsv.post {
nsv.smoothScrollTo(0, 0)
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
}
appbarlayout.setExpanded(true)
} catch (e: NullPointerException) {}
}
}
appbarlayout.setExpanded(true)
}
}
}
private fun openInfoDialog() {
val dialog = AlertDialog.Builder(context)
val dialogView = LayoutInflater.from(context).inflate(R.layout.simple_dialog, null)
dialogView.findViewById<TextView>(R.id.textviewtitle).text = getString(R.string.information)
dialogView.findViewById<TextView>(R.id.dialogtext).text = prefs.getString("informational", "")
dialog.setView(dialogView).show()
}
private fun loadFragment(fragment: Fragment): Boolean {
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit()
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit()
return true
}
@@ -342,12 +285,4 @@ class Main : AppCompatActivity(R.layout.activity_main) {
val scheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
scheduler.schedule(info)
}
override fun onBackPressed() {
if (bottomSheetBehaviour.state == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetBehaviour.state = BottomSheetBehavior.STATE_COLLAPSED
} else {
super.onBackPressed()
}
}
}