Skip to content

Commit

Permalink
Merge pull request #21 from itszechs/feature-log-out-from-account
Browse files Browse the repository at this point in the history
Feature: Log out from account
  • Loading branch information
itszechs authored Jun 25, 2022
2 parents c320b39 + 6543b8f commit 4375b61
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 11 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/zechs/drive/stream/data/remote/DriveHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.content.Intent
import android.util.Log
import com.google.android.gms.auth.GoogleAuthUtil
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.Scopes
import com.google.android.gms.common.api.Scope
import com.google.api.client.extensions.android.http.AndroidHttp
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
Expand Down Expand Up @@ -35,6 +38,17 @@ class DriveHelper @Inject constructor(
var hasSignedIn = false
private set


fun getClient(): GoogleSignInClient {
val signInOptions = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Scope(Scopes.DRIVE_FULL))
.requestEmail()
.build()

return GoogleSignIn.getClient(context, signInOptions)
}

/**
* Public method to send intent result from
* Google Sign-in flow
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.delay
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.utils.ext.navigateSafe


class HomeFragment : BaseFragment() {

companion object {
Expand All @@ -21,6 +29,8 @@ class HomeFragment : BaseFragment() {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

private val viewModel by activityViewModels<HomeViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -75,6 +85,8 @@ class HomeFragment : BaseFragment() {

}

setupToolbar()
observeLogOutState()
}

private fun <T : MaterialButton> navigateToFiles(
Expand All @@ -90,6 +102,41 @@ class HomeFragment : BaseFragment() {
}
}

private fun setupToolbar() {
binding.toolbar.setOnMenuItemClickListener { item ->
if (item.itemId == R.id.action_logOut) {
MaterialAlertDialogBuilder(context!!)
.setTitle(getString(R.string.log_out_dialog_title))
.setNegativeButton(getString(R.string.no)) { dialog, _ ->
dialog.dismiss()
}
.setPositiveButton(getString(R.string.yes)) { dialog, _ ->
dialog.dismiss()
Log.d(TAG, "Logging out...")
viewModel.logOut()
}
.show()
return@setOnMenuItemClickListener true
}
return@setOnMenuItemClickListener false
}
}

private fun observeLogOutState() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.hasLoggedOut.collect {
if (it) {
// restart activity
activity!!.finish()
delay(250L)
activity!!.startActivity(activity!!.intent)
}
}
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package zechs.drive.stream.ui.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import zechs.drive.stream.data.remote.DriveHelper
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
private val driveHelper: DriveHelper
) : ViewModel() {

private val _hasLoggedOut = MutableStateFlow(false)
val hasLoggedOut = _hasLoggedOut.asStateFlow()

fun logOut() = viewModelScope.launch {
delay(250L)
val client = driveHelper.getClient()
// client.revokeAccess()
client.signOut()
_hasLoggedOut.value = true
}

}
12 changes: 1 addition & 11 deletions app/src/main/java/zechs/drive/stream/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.Scopes
import com.google.android.gms.common.api.Scope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import zechs.drive.stream.R
Expand Down Expand Up @@ -69,13 +65,7 @@ class MainActivity : AppCompatActivity() {
private fun requestSignIn() {
Log.d(TAG, "Requesting sign-in")

val signInOptions = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Scope(Scopes.DRIVE_FULL))
.requestEmail()
.build()

val client = GoogleSignIn.getClient(this, signInOptions)
val client = viewModel.getClient()

val startForResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ class MainViewModel @Inject constructor(
Log.d(TAG, msg)
}

fun getClient() = driveHelper.getClient()
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_logout_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="48"
android:viewportHeight="48">
<path
android:fillColor="@android:color/white"
android:pathData="M9,42Q7.8,42 6.9,41.1Q6,40.2 6,39V9Q6,7.8 6.9,6.9Q7.8,6 9,6H23.55V9H9Q9,9 9,9Q9,9 9,9V39Q9,39 9,39Q9,39 9,39H23.55V42ZM33.3,32.75 L31.15,30.6 36.25,25.5H18.75V22.5H36.15L31.05,17.4L33.2,15.25L42,24.05Z" />
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
style="@style/MenuTheme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="@menu/main_menu"
app:title="@string/app_name" />
</com.google.android.material.appbar.AppBarLayout>

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_logOut"
android:icon="@drawable/ic_logout_24"
android:title="@string/logout"
app:showAsAction="ifRoom" />

</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
<string name="ok">OK</string>
<string name="details">Details</string>
<string name="retry">Retry</string>
<string name="logout">Logout</string>
<string name="log_out_dialog_title">Are you sure you want to log out?</string>
<string name="no">No</string>
<string name="yes">Yes</string>
</resources>

0 comments on commit 4375b61

Please sign in to comment.