Skip to content

Commit

Permalink
Merge pull request #119 from itszechs/settings
Browse files Browse the repository at this point in the history
Dedicated settings screen
  • Loading branch information
itszechs authored Jun 24, 2023
2 parents d311286 + 7d31545 commit 1ee013c
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ data class LatestRelease(
val htmlUrl: String
) {

fun isLatest() = tagName != BuildConfig.VERSION_NAME
fun isLatest() = tagName == BuildConfig.VERSION_NAME

}
4 changes: 2 additions & 2 deletions app/src/main/java/zechs/drive/stream/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import zechs.drive.stream.utils.SessionManager
import zechs.drive.stream.utils.ThemeManager
import zechs.drive.stream.utils.AppSettings
import javax.inject.Singleton


Expand All @@ -33,6 +33,6 @@ object AppModule {
@Provides
fun provideThemeDataStore(
@ApplicationContext appContext: Context
): ThemeManager = ThemeManager(appContext)
): AppSettings = AppSettings(appContext)

}
8 changes: 8 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ abstract class BaseFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

enterTransition = MaterialSharedAxis(
/* axis */ MaterialSharedAxis.X,
/* forward */ true
).apply {
interpolator = LinearInterpolator()
duration = 300
}

returnTransition = MaterialSharedAxis(
/* axis */ MaterialSharedAxis.X,
/* forward */ false
Expand Down
21 changes: 8 additions & 13 deletions app/src/main/java/zechs/drive/stream/ui/files/FilesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
Expand All @@ -23,7 +24,6 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.transition.Transition
import androidx.transition.TransitionManager
import com.google.android.material.color.MaterialColors
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.transition.MaterialFadeThrough
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -34,8 +34,10 @@ import zechs.drive.stream.databinding.FragmentFilesBinding
import zechs.drive.stream.ui.BaseFragment
import zechs.drive.stream.ui.files.adapter.FilesAdapter
import zechs.drive.stream.ui.files.adapter.FilesDataModel
import zechs.drive.stream.ui.main.MainViewModel
import zechs.drive.stream.ui.player.PlayerActivity
import zechs.drive.stream.ui.player2.MPVActivity
import zechs.drive.stream.utils.VideoPlayer
import zechs.drive.stream.utils.state.Resource


Expand All @@ -49,6 +51,7 @@ class FilesFragment : BaseFragment() {
private var _binding: FragmentFilesBinding? = null
private val binding get() = _binding!!

private val mainViewModel by activityViewModels<MainViewModel>()
private val viewModel by lazy {
ViewModelProvider(this)[FilesViewModel::class.java]
}
Expand Down Expand Up @@ -244,18 +247,10 @@ class FilesFragment : BaseFragment() {
}

private fun launchVideoPlayer(file: DriveFile) {

val items = arrayOf("ExoPlayer", "MPV")

MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.play_using))
.setItems(items) { dialog, which ->
when (which) {
0 -> launchExo(file)
1 -> viewModel.fetchToken(file)
}
dialog.dismiss()
}.show()
when (mainViewModel.currentPlayerIndex) {
VideoPlayer.EXO_PLAYER -> launchExo(file)
VideoPlayer.MPV -> viewModel.fetchToken(file)
}
}

private fun mpvObserver() {
Expand Down
32 changes: 5 additions & 27 deletions app/src/main/java/zechs/drive/stream/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import kotlinx.coroutines.launch
import zechs.drive.stream.R
import zechs.drive.stream.databinding.FragmentHomeBinding
import zechs.drive.stream.ui.BaseFragment
import zechs.drive.stream.ui.main.MainViewModel
import zechs.drive.stream.utils.AppTheme
import zechs.drive.stream.utils.ext.navigateSafe


Expand All @@ -32,7 +30,6 @@ class HomeFragment : BaseFragment() {
private val binding get() = _binding!!

private val viewModel by activityViewModels<HomeViewModel>()
private val mainViewModel by activityViewModels<MainViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -86,6 +83,10 @@ class HomeFragment : BaseFragment() {
query = "'root' in parents and trashed=true"
)

btnAppSettings.setOnClickListener {
findNavController().navigateSafe(R.id.action_homeFragment_to_settingsFragment)
}

}

setupToolbar()
Expand All @@ -106,11 +107,6 @@ class HomeFragment : BaseFragment() {
}

private fun setupToolbar() {
val themes = listOf(
getString(R.string.theme_dark),
getString(R.string.theme_light),
getString(R.string.theme_system)
)
binding.toolbar.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.action_logOut -> {
Expand All @@ -127,25 +123,7 @@ class HomeFragment : BaseFragment() {
.show()
return@setOnMenuItemClickListener true
}
R.id.action_theme -> {
MaterialAlertDialogBuilder(requireContext()).apply {
setTitle(getString(R.string.select_theme))
setSingleChoiceItems(
themes.toTypedArray(),
mainViewModel.currentThemeIndex
) { dialog, item ->
val theme = when (item) {
AppTheme.DARK.value -> AppTheme.DARK
AppTheme.LIGHT.value -> AppTheme.LIGHT
AppTheme.SYSTEM.value -> AppTheme.SYSTEM
else -> throw IllegalArgumentException("Unknown theme value")
}
mainViewModel.setTheme(theme)
dialog.dismiss()
}
}.also { it.show() }
return@setOnMenuItemClickListener true
}

else -> {
return@setOnMenuItemClickListener false
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/zechs/drive/stream/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ class MainActivity : AppCompatActivity() {
is Resource.Success -> {
val release = it.data!!
if (release.isLatest()) {
Log.d(TAG, "Already on latest version")
} else {
Log.d(TAG, "Newer version of app is available (latest=${release.tagName})")
sendUpdateNotification(release)
} else {
Log.d(TAG, "Already on latest version")
}
}
is Resource.Error -> Log.d(TAG, it.message!!)
Expand Down
43 changes: 38 additions & 5 deletions app/src/main/java/zechs/drive/stream/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import zechs.drive.stream.data.model.LatestRelease
import zechs.drive.stream.data.repository.GithubRepository
import zechs.drive.stream.utils.AppSettings
import zechs.drive.stream.utils.AppTheme
import zechs.drive.stream.utils.SessionManager
import zechs.drive.stream.utils.ThemeManager
import zechs.drive.stream.utils.VideoPlayer
import zechs.drive.stream.utils.state.Resource
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
private val sessionManager: SessionManager,
private val githubRepository: GithubRepository,
private val themeManager: ThemeManager
private val appSettings: AppSettings
) : ViewModel() {

private val _isLoading = MutableStateFlow(true)
Expand All @@ -42,10 +43,20 @@ class MainViewModel @Inject constructor(
var currentThemeIndex = 2
private set

private val _lastUpdated = MutableStateFlow<String?>(null)
val lastUpdated = _lastUpdated.asStateFlow()

var isChecking = false
private set

init {
viewModelScope.launch {
getTheme()
val status = getLoginStatus()
if (status) {
getPlayer()
getLastUpdated()
}
_hasLoggedIn.value = status
delay(250L)
_isLoading.value = false
Expand All @@ -59,19 +70,41 @@ class MainViewModel @Inject constructor(
return true
}

private fun getLatestRelease() = viewModelScope.launch {
fun getLatestRelease() = viewModelScope.launch {
_latest.postValue(Resource.Loading())
isChecking = true
_latest.postValue(githubRepository.getLatestRelease())
isChecking = false
appSettings.saveLastUpdated()
getLastUpdated()
}

private fun getLastUpdated() = viewModelScope.launch {
_lastUpdated.emit(appSettings.fetchLastUpdated())
}

private suspend fun getTheme() {
val fetchTheme = themeManager.fetchTheme()
val fetchTheme = appSettings.fetchTheme()
currentThemeIndex = fetchTheme.value
_theme.emit(fetchTheme)
}

fun setTheme(theme: AppTheme) = viewModelScope.launch {
themeManager.saveTheme(theme)
appSettings.saveTheme(theme)
getTheme()
}

var currentPlayerIndex = VideoPlayer.MPV
private set

private suspend fun getPlayer() {
val fetchPlayer = appSettings.fetchPlayer()
currentPlayerIndex = fetchPlayer
}

fun setPlayer(player: VideoPlayer) = viewModelScope.launch {
appSettings.savePlayer(player)
getPlayer()
}

}
Loading

0 comments on commit 1ee013c

Please sign in to comment.