2020-10-13 21:27:05 +02:00
/ * *
* Teapod
*
2021-01-01 12:15:17 +01:00
* Copyright 2020 - 2021 < seil0 @mosad . xyz >
2020-10-13 21:27:05 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2020-10-08 22:20:20 +02:00
package org.mosad.teapod
2020-10-11 13:18:20 +02:00
import android.content.Intent
2020-10-08 22:20:20 +02:00
import android.os.Bundle
2020-10-11 13:18:20 +02:00
import android.util.Log
2020-10-11 10:02:00 +02:00
import android.view.MenuItem
2020-10-08 22:20:20 +02:00
import androidx.appcompat.app.AppCompatActivity
2020-10-11 10:02:00 +02:00
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
2020-10-15 18:51:29 +02:00
import com.google.android.material.bottomnavigation.BottomNavigationView
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.ActivityMainBinding
2020-10-08 22:20:20 +02:00
import org.mosad.teapod.parser.AoDParser
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.player.PlayerActivity
2020-10-11 13:18:20 +02:00
import org.mosad.teapod.preferences.EncryptedPreferences
2020-10-23 11:28:47 +02:00
import org.mosad.teapod.preferences.Preferences
2020-10-11 14:44:38 +02:00
import org.mosad.teapod.ui.components.LoginDialog
2020-10-19 19:59:53 +02:00
import org.mosad.teapod.ui.fragments.*
2020-11-23 20:11:10 +01:00
import org.mosad.teapod.util.DataTypes
2020-10-16 11:23:32 +02:00
import org.mosad.teapod.util.StorageController
2020-10-15 18:51:29 +02:00
import kotlin.system.measureTimeMillis
2020-10-08 22:20:20 +02:00
2020-10-11 10:02:00 +02:00
class MainActivity : AppCompatActivity ( ) , BottomNavigationView . OnNavigationItemSelectedListener {
2020-11-25 22:35:55 +01:00
private lateinit var binding : ActivityMainBinding
2020-10-16 11:23:32 +02:00
private var activeBaseFragment : Fragment = HomeFragment ( ) // the currently active fragment, home at the start
2020-10-08 22:20:20 +02:00
2020-11-23 20:11:10 +01:00
companion object {
var wasInitialized = false
}
2020-10-08 22:20:20 +02:00
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
2020-11-23 20:11:10 +01:00
2020-11-25 23:26:46 +01:00
if ( ! wasInitialized ) { load ( ) }
2020-11-23 20:11:10 +01:00
theme . applyStyle ( getThemeResource ( ) , true )
2020-11-25 23:26:46 +01:00
binding = ActivityMainBinding . inflate ( layoutInflater )
2020-11-25 22:35:55 +01:00
binding . navView . setOnNavigationItemSelectedListener ( this )
2020-11-25 23:26:46 +01:00
setContentView ( binding . root )
2020-10-08 22:20:20 +02:00
2020-10-16 11:23:32 +02:00
supportFragmentManager . commit {
replace ( R . id . nav _host _fragment , activeBaseFragment , activeBaseFragment . javaClass . simpleName )
}
2020-10-08 22:20:20 +02:00
}
2020-10-11 10:02:00 +02:00
override fun onBackPressed ( ) {
if ( supportFragmentManager . backStackEntryCount > 0 ) {
supportFragmentManager . popBackStack ( )
} else {
2020-10-16 11:23:32 +02:00
if ( activeBaseFragment !is HomeFragment ) {
2020-11-25 22:35:55 +01:00
binding . navView . selectedItemId = R . id . navigation _home
2020-10-11 10:02:00 +02:00
} else {
super . onBackPressed ( )
}
}
}
override fun onNavigationItemSelected ( item : MenuItem ) : Boolean {
2020-10-14 20:22:20 +02:00
if ( supportFragmentManager . backStackEntryCount > 0 ) {
supportFragmentManager . popBackStack ( )
}
2020-10-11 10:02:00 +02:00
val ret = when ( item . itemId ) {
R . id . navigation _home -> {
2020-10-16 11:23:32 +02:00
activeBaseFragment = HomeFragment ( )
2020-10-11 10:02:00 +02:00
true
}
R . id . navigation _library -> {
2020-10-16 11:23:32 +02:00
activeBaseFragment = LibraryFragment ( )
2020-10-11 10:02:00 +02:00
true
}
R . id . navigation _search -> {
2020-10-16 11:23:32 +02:00
activeBaseFragment = SearchFragment ( )
2020-10-11 10:02:00 +02:00
true
}
R . id . navigation _account -> {
2020-10-16 11:23:32 +02:00
activeBaseFragment = AccountFragment ( )
2020-10-11 10:02:00 +02:00
true
}
else -> false
}
supportFragmentManager . commit {
2020-10-16 11:23:32 +02:00
replace ( R . id . nav _host _fragment , activeBaseFragment , activeBaseFragment . javaClass . simpleName )
2020-10-11 10:02:00 +02:00
}
return ret
}
2020-11-23 20:11:10 +01:00
private fun getThemeResource ( ) : Int {
return when ( Preferences . theme ) {
DataTypes . Theme . DARK -> R . style . AppTheme _Dark
else -> R . style . AppTheme _Light
}
}
2020-10-08 22:20:20 +02:00
private fun load ( ) {
2020-10-15 18:51:29 +02:00
// running login and list in parallel does not bring any speed improvements
val time = measureTimeMillis {
2020-10-23 11:28:47 +02:00
Preferences . load ( this )
2020-12-30 14:18:04 +01:00
// make sure credentials are set, run's async
2020-10-16 19:56:08 +02:00
EncryptedPreferences . readCredentials ( this )
if ( EncryptedPreferences . password . isEmpty ( ) ) {
showLoginDialog ( true )
} else {
// try to login in, as most sites can only bee loaded once loged in
2020-10-19 19:59:53 +02:00
if ( ! AoDParser . login ( ) ) showLoginDialog ( false )
2020-10-16 19:56:08 +02:00
}
StorageController . load ( this )
2020-10-19 19:59:53 +02:00
AoDParser . initialLoading ( )
2020-11-23 20:11:10 +01:00
wasInitialized = true
2020-10-15 18:51:29 +02:00
}
Log . i ( javaClass . name , " login and list in $time ms " )
2020-10-08 22:20:20 +02:00
}
2020-10-11 10:02:00 +02:00
2020-11-23 20:11:10 +01:00
private fun showLoginDialog ( firstTry : Boolean ) {
LoginDialog ( this , firstTry ) . positiveButton {
EncryptedPreferences . saveCredentials ( login , password , context )
if ( ! AoDParser . login ( ) ) {
showLoginDialog ( false )
Log . w ( javaClass . name , " Login failed, please try again. " )
}
} . negativeButton {
Log . i ( javaClass . name , " Login canceled, exiting. " )
finish ( )
} . show ( )
}
2020-10-13 15:56:07 +02:00
/ * *
2020-12-05 21:51:12 +01:00
* Show a fragment on top of the current fragment .
* The current fragment is replaced and the new one is added
* to the back stack .
2020-10-13 15:56:07 +02:00
* /
2020-12-05 21:51:12 +01:00
fun showFragment ( fragment : Fragment ) {
2020-10-11 10:02:00 +02:00
supportFragmentManager . commit {
2020-12-05 21:51:12 +01:00
replace ( R . id . nav _host _fragment , fragment , fragment . javaClass . simpleName )
addToBackStack ( fragment . javaClass . name )
show ( fragment )
2020-12-05 21:03:14 +01:00
}
}
2020-11-13 11:23:09 +01:00
fun startPlayer ( mediaId : Int , episodeId : Int ) {
2020-10-11 13:18:20 +02:00
val intent = Intent ( this , PlayerActivity :: class . java ) . apply {
2020-11-13 11:23:09 +01:00
putExtra ( getString ( R . string . intent _media _id ) , mediaId )
putExtra ( getString ( R . string . intent _episode _id ) , episodeId )
2020-10-11 13:18:20 +02:00
}
startActivity ( intent )
}
2020-10-12 23:26:32 +02:00
2020-11-23 20:11:10 +01:00
/ * *
* use custom restart instead of recreate ( ) , since it has animations
* /
fun restart ( ) {
val restartIntent = intent
restartIntent . addFlags ( Intent . FLAG _ACTIVITY _NO _ANIMATION )
finish ( )
startActivity ( restartIntent )
2020-10-12 23:26:32 +02:00
}
2020-11-23 20:11:10 +01:00
2020-10-08 22:20:20 +02:00
}