2020-10-13 21:27:05 +02:00
/ * *
* Teapod
*
* Copyright 2020 < seil0 @mosad . xyz >
*
* 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 com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
2020-10-11 10:02:00 +02:00
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import kotlinx.android.synthetic.main.activity_main.*
2020-10-13 15:56:07 +02:00
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
2020-10-08 22:20:20 +02:00
import org.mosad.teapod.parser.AoDParser
2020-10-11 13:18:20 +02:00
import org.mosad.teapod.preferences.EncryptedPreferences
2020-10-11 10:02:00 +02:00
import org.mosad.teapod.ui.MediaFragment
import org.mosad.teapod.ui.account.AccountFragment
2020-10-11 14:44:38 +02:00
import org.mosad.teapod.ui.components.LoginDialog
2020-10-11 10:02:00 +02:00
import org.mosad.teapod.ui.home.HomeFragment
import org.mosad.teapod.ui.library.LibraryFragment
import org.mosad.teapod.ui.search.SearchFragment
2020-10-14 20:22:20 +02:00
import org.mosad.teapod.util.*
2020-10-08 22:20:20 +02:00
2020-10-11 10:02:00 +02:00
class MainActivity : AppCompatActivity ( ) , BottomNavigationView . OnNavigationItemSelectedListener {
private var activeFragment : Fragment = HomeFragment ( ) // the currently active fragment, home at the start
2020-10-08 22:20:20 +02:00
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity _main )
val navView : BottomNavigationView = findViewById ( R . id . nav _view )
2020-10-11 10:02:00 +02:00
navView . setOnNavigationItemSelectedListener ( this )
2020-10-08 22:20:20 +02:00
load ( )
}
2020-10-11 10:02:00 +02:00
override fun onBackPressed ( ) {
if ( supportFragmentManager . backStackEntryCount > 0 ) {
supportFragmentManager . popBackStack ( )
} else {
if ( activeFragment !is HomeFragment ) {
nav _view . selectedItemId = R . id . navigation _home
} 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 -> {
activeFragment = HomeFragment ( )
true
}
R . id . navigation _library -> {
activeFragment = LibraryFragment ( )
true
}
R . id . navigation _search -> {
activeFragment = SearchFragment ( )
true
}
R . id . navigation _account -> {
activeFragment = AccountFragment ( )
true
}
else -> false
}
supportFragmentManager . commit {
replace ( R . id . nav _host _fragment , activeFragment )
}
return ret
}
2020-10-08 22:20:20 +02:00
private fun load ( ) {
2020-10-11 13:18:20 +02:00
EncryptedPreferences . readCredentials ( this )
2020-10-15 13:00:44 +02:00
// make sure credentials are set
2020-10-11 13:18:20 +02:00
if ( EncryptedPreferences . password . isEmpty ( ) ) {
2020-10-12 23:26:32 +02:00
showLoginDialog ( true )
2020-10-11 13:18:20 +02:00
}
2020-10-15 13:00:44 +02:00
// TODO save last loginSuccess, if false show login dialog even if credentials are present
AoDParser ( ) . listAnimes ( )
2020-10-08 22:20:20 +02:00
}
2020-10-11 10:02:00 +02:00
2020-10-13 15:56:07 +02:00
/ * *
2020-10-14 20:22:20 +02:00
* show the media fragment for the selected media
* while loading show the loading fragment
2020-10-13 15:56:07 +02:00
* /
2020-10-14 20:22:20 +02:00
fun showMediaFragment ( media : Media ) = GlobalScope . launch {
val loadingFragment = LoadingFragment ( )
supportFragmentManager . commit {
add ( R . id . nav _host _fragment , loadingFragment , " MediaFragment " )
show ( loadingFragment )
}
2020-10-11 10:02:00 +02:00
2020-10-14 20:22:20 +02:00
// load the streams for the selected media
media . episodes = AoDParser ( ) . loadStreams ( media )
2020-10-12 20:30:45 +02:00
val tmdb = TMDBApiController ( ) . search ( media . title , media . type )
val mediaFragment = MediaFragment ( media , tmdb )
2020-10-11 10:02:00 +02:00
supportFragmentManager . commit {
add ( R . id . nav _host _fragment , mediaFragment , " MediaFragment " )
addToBackStack ( null )
show ( mediaFragment )
}
2020-10-14 20:22:20 +02:00
supportFragmentManager . commit {
remove ( loadingFragment )
}
2020-10-11 10:02:00 +02:00
}
2020-10-11 13:18:20 +02:00
fun startPlayer ( streamUrl : String ) {
val intent = Intent ( this , PlayerActivity :: class . java ) . apply {
2020-10-11 15:19:42 +02:00
putExtra ( getString ( R . string . intent _stream _url ) , streamUrl )
2020-10-11 13:18:20 +02:00
}
startActivity ( intent )
}
2020-10-12 23:26:32 +02: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-08 22:20:20 +02:00
}