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 .
*
* /
2021-02-06 19:02:12 +01:00
package org.mosad.teapod.ui.activity.main
2020-10-08 22:20:20 +02:00
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
2021-01-13 20:57:00 +01:00
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onDismiss
2020-10-15 18:51:29 +02:00
import com.google.android.material.bottomnavigation.BottomNavigationView
2021-01-13 20:57:00 +01:00
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.runBlocking
2021-01-16 00:16:47 +01:00
import org.mosad.teapod.R
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
2021-02-06 19:02:12 +01:00
import org.mosad.teapod.ui.activity.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
2021-02-06 19:02:12 +01:00
import org.mosad.teapod.ui.activity.main.fragments.AccountFragment
import org.mosad.teapod.ui.activity.main.fragments.HomeFragment
import org.mosad.teapod.ui.activity.main.fragments.LibraryFragment
import org.mosad.teapod.ui.activity.main.fragments.SearchFragment
import org.mosad.teapod.ui.activity.onboarding.OnboardingActivity
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
2021-01-21 20:31:15 +01:00
import org.mosad.teapod.util.exitAndRemoveTask
2021-01-13 20:57:00 +01:00
import java.net.SocketTimeoutException
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
2021-02-06 19:02:12 +01:00
lateinit var instance : MainActivity
}
init {
instance = this
2020-11-23 20:11:10 +01:00
}
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 ) {
2021-01-16 00:16:47 +01:00
DataTypes . Theme . LIGHT -> R . style . AppTheme _Light
else -> R . style . AppTheme _Dark
2020-11-23 20:11:10 +01:00
}
}
2021-01-13 20:57:00 +01:00
/ * *
* initial loading and login are run in parallel , as initial loading doesn ' t require
* any login cookies
* /
2020-10-08 22:20:20 +02:00
private fun load ( ) {
2020-10-15 18:51:29 +02:00
val time = measureTimeMillis {
2021-01-13 20:57:00 +01:00
val loadingJob = AoDParser . initialLoading ( ) // start the initial loading
2020-10-23 11:28:47 +02:00
2021-01-13 20:57:00 +01:00
// load all saved stuff here
Preferences . load ( this )
2020-10-16 19:56:08 +02:00
EncryptedPreferences . readCredentials ( this )
StorageController . load ( this )
2020-11-23 20:11:10 +01:00
2021-01-16 00:16:47 +01:00
// show onbaording
if ( EncryptedPreferences . password . isEmpty ( ) ) {
showOnboarding ( )
} else {
try {
if ( ! AoDParser . login ( ) ) {
showLoginDialog ( )
}
} catch ( ex : SocketTimeoutException ) {
Log . w ( javaClass . name , " Timeout during login! " )
// show waring dialog before finishing
MaterialDialog ( this ) . show {
title ( R . string . dialog _timeout _head )
message ( R . string . dialog _timeout _desc )
onDismiss { exitAndRemoveTask ( ) }
}
2021-01-13 20:57:00 +01:00
}
}
runBlocking { loadingJob . joinAll ( ) } // wait for initial loading to finish
2020-10-15 18:51:29 +02:00
}
2021-01-13 20:57:00 +01:00
Log . i ( javaClass . name , " loading and login in $time ms " )
wasInitialized = true
2020-10-08 22:20:20 +02:00
}
2020-10-11 10:02:00 +02:00
2021-01-16 00:16:47 +01:00
private fun showLoginDialog ( ) {
LoginDialog ( this , false ) . positiveButton {
2020-11-23 20:11:10 +01:00
EncryptedPreferences . saveCredentials ( login , password , context )
if ( ! AoDParser . login ( ) ) {
2021-01-16 00:16:47 +01:00
showLoginDialog ( )
2020-11-23 20:11:10 +01:00
Log . w ( javaClass . name , " Login failed, please try again. " )
}
} . negativeButton {
Log . i ( javaClass . name , " Login canceled, exiting. " )
finish ( )
} . show ( )
}
2021-01-16 00:16:47 +01:00
/ * *
* start the onboarding activity and finish the main activity
* /
private fun showOnboarding ( ) {
startActivity ( Intent ( this , OnboardingActivity :: class . java ) )
finish ( )
}
2021-01-08 10:58:24 +01:00
/ * *
* start the player as new activity
* /
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
}