-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0db7611
commit dcd1734
Showing
17 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
.externalNativeBuild | ||
.cxx | ||
local.properties | ||
client_secret.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/samentic/youtubeclient/YoutubeApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.samentic.youtubeclient | ||
|
||
import android.app.Application | ||
import com.samentic.youtubeclient.core.di.AppComponent | ||
import com.samentic.youtubeclient.core.di.DaggerAppComponent | ||
|
||
class YoutubeApplication : Application() { | ||
val appComponent: AppComponent by lazy { | ||
DaggerAppComponent.factory().create(this) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/samentic/youtubeclient/core/di/AppComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.samentic.youtubeclient.core.di | ||
|
||
import com.samentic.youtubeclient.YoutubeApplication | ||
import com.samentic.youtubeclient.core.di.viewmodel.ViewModelModule | ||
import com.samentic.youtubeclient.features.auth.data.AuthModule | ||
import com.samentic.youtubeclient.features.auth.ui.AuthFragment | ||
import dagger.BindsInstance | ||
import dagger.Component | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
@Component( | ||
modules = [ | ||
ViewModelModule::class, ContextModule::class, LocalModule::class, | ||
AuthModule::class | ||
] | ||
) | ||
interface AppComponent { | ||
|
||
fun inject(fragment: AuthFragment) | ||
|
||
@Component.Factory | ||
interface Factory { | ||
fun create( | ||
@BindsInstance application: YoutubeApplication | ||
): AppComponent | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/samentic/youtubeclient/core/di/ContextModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.samentic.youtubeclient.core.di | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import com.samentic.youtubeclient.YoutubeApplication | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
|
||
@Module | ||
abstract class ContextModule { | ||
|
||
@Binds | ||
abstract fun bindApplication(application: YoutubeApplication): Application | ||
|
||
companion object { | ||
@Provides | ||
fun provideContext(application: Application): Context { | ||
return application.applicationContext | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/samentic/youtubeclient/core/di/Extensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.samentic.youtubeclient.core.di | ||
|
||
import androidx.fragment.app.Fragment | ||
import com.samentic.youtubeclient.YoutubeApplication | ||
|
||
fun Fragment.findAppComponent(): AppComponent { | ||
return (requireActivity().application as YoutubeApplication).appComponent | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/samentic/youtubeclient/core/di/LocalModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.samentic.youtubeclient.core.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import dagger.Module | ||
import dagger.Provides | ||
|
||
@Module | ||
object LocalModule { | ||
private const val PREF_NAME = "youtube" | ||
|
||
@Provides | ||
fun provideSharedPreferences(context: Context): SharedPreferences { | ||
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/samentic/youtubeclient/core/di/viewmodel/ViewModelKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.samentic.youtubeclient.core.di.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.MapKey | ||
import kotlin.reflect.KClass | ||
|
||
@MapKey | ||
@MustBeDocumented | ||
@Target( | ||
AnnotationTarget.FUNCTION, | ||
AnnotationTarget.PROPERTY_GETTER, | ||
AnnotationTarget.PROPERTY_SETTER | ||
) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class ViewModelKey(val value: KClass<out ViewModel>) |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/samentic/youtubeclient/core/di/viewmodel/ViewModelModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.samentic.youtubeclient.core.di.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.samentic.youtubeclient.features.auth.ui.AuthViewModel | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.multibindings.IntoMap | ||
|
||
@Module | ||
abstract class ViewModelModule { | ||
|
||
@Binds | ||
abstract fun bindViewModelFactory(factory: YoutubeViewModelFactory): ViewModelProvider.Factory | ||
|
||
@IntoMap | ||
@ViewModelKey(AuthViewModel::class) | ||
@Binds | ||
abstract fun bindAuthViewModel(viewModel: AuthViewModel): ViewModel | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/samentic/youtubeclient/core/di/viewmodel/YoutubeViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.samentic.youtubeclient.core.di.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import javax.inject.Inject | ||
import javax.inject.Provider | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class YoutubeViewModelFactory @Inject constructor( | ||
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>> | ||
) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
val creator = creators[modelClass] | ||
?: throw IllegalArgumentException("unknown model class $modelClass") | ||
try { | ||
@Suppress("UNCHECKED_CAST") | ||
return creator.get() as T | ||
} catch (e: Exception) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/samentic/youtubeclient/features/auth/data/AuthModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.samentic.youtubeclient.features.auth.data | ||
|
||
import android.content.SharedPreferences | ||
import com.samentic.youtubeclient.features.auth.data.AuthRepository.Companion.KEY_AUTH_STATE | ||
import dagger.Module | ||
import dagger.Provides | ||
import net.openid.appauth.AuthState | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
object AuthModule { | ||
@Singleton | ||
@Provides | ||
fun provideAuthState(pref: SharedPreferences): AuthState { | ||
val authState = pref.getString(KEY_AUTH_STATE, null)?.let { | ||
AuthState.jsonDeserialize(it) | ||
} | ||
return authState ?: AuthState() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/samentic/youtubeclient/features/auth/data/AuthRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.samentic.youtubeclient.features.auth.data | ||
|
||
import android.content.SharedPreferences | ||
import net.openid.appauth.AuthState | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AuthRepository @Inject constructor( | ||
private val authState: AuthState, | ||
private val prefs: SharedPreferences | ||
) { | ||
|
||
fun isAuthorized(): Boolean { | ||
return authState.isAuthorized | ||
} | ||
|
||
companion object { | ||
const val KEY_AUTH_STATE = "authState" | ||
} | ||
} |
20 changes: 17 additions & 3 deletions
20
app/src/main/java/com/samentic/youtubeclient/features/auth/ui/AuthFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
package com.samentic.youtubeclient.features.auth.ui | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.Toast | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.samentic.youtubeclient.R | ||
import com.samentic.youtubeclient.core.di.findAppComponent | ||
import com.samentic.youtubeclient.databinding.FragmentAuthBinding | ||
import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding | ||
import javax.inject.Inject | ||
|
||
class AuthFragment : Fragment(R.layout.fragment_auth) { | ||
@Inject | ||
lateinit var viewModelFactory: ViewModelProvider.Factory | ||
|
||
private val binding by viewBinding(FragmentAuthBinding::bind) | ||
private val viewModel by viewModels<AuthViewModel> { viewModelFactory } | ||
|
||
override fun onAttach(context: Context) { | ||
super.onAttach(context) | ||
findAppComponent().inject(this) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.btnAuthenticate.setOnClickListener { | ||
Toast.makeText(requireContext(), "Clicked!", Toast.LENGTH_SHORT).show() | ||
|
||
binding.btnAuthorize.setOnClickListener { | ||
Toast.makeText(requireContext(), "${viewModel.isAuthorized()}", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/samentic/youtubeclient/features/auth/ui/AuthViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.samentic.youtubeclient.features.auth.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import com.samentic.youtubeclient.features.auth.data.AuthRepository | ||
import javax.inject.Inject | ||
|
||
class AuthViewModel @Inject constructor( | ||
private val authRepository: AuthRepository | ||
) : ViewModel() { | ||
|
||
fun isAuthorized() = authRepository.isAuthorized() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<resources> | ||
<string name="app_name">YouTubeClient</string> | ||
<string name="label_authenticate">Authenticate</string> | ||
<string name="label_authorize">Authorize</string> | ||
</resources> |