Skip to content

Commit

Permalink
Merge pull request #53 from Team-Notitime/feat/sign/52-login-api-conn…
Browse files Browse the repository at this point in the history
…ection

Feat/sign/52 login api connection
  • Loading branch information
easyhz authored Aug 12, 2024
2 parents c9ed22f + 36ec85a commit 1c67095
Show file tree
Hide file tree
Showing 55 changed files with 701 additions and 49 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ dependencies {
implementation(projects.core.network)
implementation(projects.core.model)

implementation(projects.data.auth)
implementation(projects.data.organization)

implementation(projects.domain.home)
implementation(projects.domain.sign)

implementation(projects.feature.announcement)
implementation(projects.feature.home)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/easyhz/noffice/NofficeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.easyhz.noffice.navigation.announcement.announcementGraph
import com.easyhz.noffice.navigation.announcement.navigateToAnnouncementDetail
import com.easyhz.noffice.navigation.announcement.navigateToAnnouncementNofficeSelection
import com.easyhz.noffice.navigation.home.homeGraph
import com.easyhz.noffice.navigation.home.navigateToHome
import com.easyhz.noffice.navigation.home.screen.Home
import com.easyhz.noffice.navigation.my_page.myPageGraph
import com.easyhz.noffice.navigation.my_page.navigateToMyPage
Expand Down Expand Up @@ -119,7 +118,7 @@ internal fun NofficeApp(
navController = navController,
)
signGraph(
navigateToHome = navController::navigateToHome
navController = navController
)
announcementGraph(
navController = navController,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package com.easyhz.noffice.navigation.home

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.ui.Modifier
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import com.easyhz.noffice.feature.home.screen.home.HomeScreen
import com.easyhz.noffice.navigation.home.screen.Home
import com.easyhz.noffice.navigation.sign.screen.LogIn

internal fun NavGraphBuilder.homeGraph(
modifier: Modifier,
navigateToAnnouncementDetail: (Int, String) -> Unit,
navigateToMyPage: () -> Unit
) {
composable<Home>(
enterTransition = { fadeIn(animationSpec = tween(700)) },
enterTransition = {
if (this.enabledSlide()) null
else fadeIn(animationSpec = tween(700))
},
exitTransition = { fadeOut(animationSpec = tween(700)) },
popEnterTransition = { fadeIn(animationSpec = tween(700)) },
popExitTransition = { fadeOut(animationSpec = tween(700)) }
Expand All @@ -35,4 +41,7 @@ internal fun NavController.navigateToHome(navOptions: NavOptions? = null) {
route = Home,
navOptions = navOptions
)
}
}

private fun AnimatedContentTransitionScope<NavBackStackEntry>.enabledSlide() =
this.initialState.destination.route == LogIn::class.java.name
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.easyhz.noffice.navigation.sign

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navOptions
import com.easyhz.noffice.feature.sign.screen.login.LoginScreen
import com.easyhz.noffice.feature.sign.screen.signUp.SignUpScreen
import com.easyhz.noffice.navigation.home.navigateToHome
import com.easyhz.noffice.navigation.sign.screen.LogIn
import com.easyhz.noffice.navigation.sign.screen.SignUp

internal fun NavGraphBuilder.signGraph(
navigateToHome: () -> Unit
navController: NavController
) {
composable<LogIn> {
val navOptions = navOptions {
popUpTo(navController.graph.startDestinationId) {
inclusive = true
}
launchSingleTop = true
}
LoginScreen(
navigateToHome = navigateToHome
navigateToHome = { navController.navigateToHome(navOptions) }
)
}
composable<SignUp> {
Expand Down
1 change: 1 addition & 0 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.noffice.android.library)
alias(libs.plugins.noffice.android.library.compose)
alias(libs.plugins.noffice.android.application.test)
alias(libs.plugins.noffice.android.hilt)
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.easyhz.noffice.data.announcement.di.util
package com.easyhz.noffice.core.common.di

import com.easyhz.noffice.core.common.di.NofficeDispatchers.IO
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -10,7 +11,7 @@ import kotlinx.coroutines.Dispatchers
@Module
@InstallIn(SingletonComponent::class)
object DispatcherModule {
@DispatcherIO
@Dispatcher(IO)
@Provides
fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.easyhz.noffice.core.common.di

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class Dispatcher(val dispatcher: NofficeDispatchers)

enum class NofficeDispatchers {
DEFAULT,
IO,
}
1 change: 1 addition & 0 deletions core/datastore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
16 changes: 16 additions & 0 deletions core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
alias(libs.plugins.noffice.android.library)
alias(libs.plugins.noffice.android.hilt)
}

android {
namespace = "com.easyhz.noffice.core.datastore"
}

dependencies {
implementation(projects.core.common)
implementation(projects.core.model)

implementation(libs.androidx.datastore)
implementation(libs.androidx.datastore.core)
}
21 changes: 21 additions & 0 deletions core/datastore/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions core/datastore/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.easyhz.noffice.core.datastore.datasource.auth


interface AuthLocalDataSource {
suspend fun getAccessToken(): Result<String>
suspend fun getRefreshToken(): Result<String>
suspend fun deleteToken()
suspend fun updateAccessToken(access: String)
suspend fun updateTokens(access: String, refresh: String)
suspend fun getAuthProvider(): Result<String>
suspend fun updateAuthProvider(provider: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.easyhz.noffice.core.datastore.datasource.auth

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.easyhz.noffice.core.common.di.Dispatcher
import com.easyhz.noffice.core.common.di.NofficeDispatchers.IO
import com.easyhz.noffice.core.datastore.util.AuthKey
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import javax.inject.Inject

class AuthLocalDataSourceImpl @Inject constructor(
@Dispatcher(IO) private val dispatcher: CoroutineDispatcher,
private val dataStore: DataStore<Preferences>
): AuthLocalDataSource {
private val accessToken = stringPreferencesKey(AuthKey.ACCESS_TOKEN.key)
private val refreshToken = stringPreferencesKey(AuthKey.REFRESH_TOKEN.key)
private val authProvider = stringPreferencesKey(AuthKey.AUTH_PROVIDER.key)

override suspend fun getAccessToken(): Result<String> = withContext(dispatcher) {
val preferences = dataStore.data.first()
return@withContext preferences[accessToken]?.let {
Result.success(it)
} ?: Result.failure(generateNullException(AuthKey.ACCESS_TOKEN))
}

override suspend fun getRefreshToken(): Result<String> = withContext(dispatcher) {
val preferences = dataStore.data.first()
return@withContext preferences[refreshToken]?.let {
Result.success(it)
} ?: Result.failure(generateNullException(AuthKey.REFRESH_TOKEN))
}

override suspend fun deleteToken(): Unit = withContext(dispatcher) {
dataStore.edit { preferences ->
preferences.remove(accessToken)
preferences.remove(refreshToken)
}
}

override suspend fun updateAccessToken(access: String): Unit = withContext(dispatcher) {
dataStore.edit { preferences ->
preferences[accessToken] = access
}
}

override suspend fun updateTokens(access: String, refresh: String): Unit = withContext(dispatcher) {
dataStore.edit { preferences ->
preferences[accessToken] = access
preferences[refreshToken] = refresh
}
}

override suspend fun getAuthProvider(): Result<String> = withContext(dispatcher) {
val preferences = dataStore.data.first()
return@withContext preferences[authProvider]?.let {
Result.success(it)
} ?: Result.failure(generateNullException(AuthKey.AUTH_PROVIDER))
}

override suspend fun updateAuthProvider(provider: String): Unit = withContext(dispatcher) {
dataStore.edit { preferences ->
preferences[authProvider] = provider
}
}

private fun generateNullException(authKey: AuthKey): Exception {
return Exception("${authKey.key} is null")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.easyhz.noffice.core.datastore.di

import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSource
import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
interface DataSourceModule {
@Binds
fun bindAuthLocalDataSource(
authLocalDataSourceImpl: AuthLocalDataSourceImpl
): AuthLocalDataSource
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.easyhz.noffice.core.datastore.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.easyhz.noffice.core.datastore.util.authDataStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
abstract class DataStoreModule {
companion object {

@Singleton
@Provides
fun provideUserDataStorePreferences(
@ApplicationContext context: Context
): DataStore<Preferences> =
context.authDataStore
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.easyhz.noffice.core.datastore.util

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore

val Context.authDataStore: DataStore<Preferences> by preferencesDataStore(
name = "noffice-auth"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.easyhz.noffice.core.datastore.util

internal enum class AuthKey(
val key: String
) {
ACCESS_TOKEN(
key = "ACCESS_TOKEN"
), REFRESH_TOKEN(
key = "REFRESH_TOKEN"
), AUTH_PROVIDER(
key = "PROVIDER"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.easyhz.noffice.core.model.auth.param

import android.content.Context

data class AuthParam(
val context: Context,
val providerName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.easyhz.noffice.core.network.api.auth

import com.easyhz.noffice.core.network.model.request.sign.LoginRequest
import com.easyhz.noffice.core.network.model.response.auth.UserResponse
import com.easyhz.noffice.core.network.util.NofficeResult
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthService {
@POST("/api/v1/member/login")
suspend fun login(
@Body body: LoginRequest
): NofficeResult<UserResponse>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.easyhz.noffice.core.network.converter

import com.easyhz.noffice.core.network.model.response.ErrorResponse
import com.easyhz.noffice.core.network.model.response.base.ErrorResponse
import com.google.gson.Gson

class ErrorResponseConverter constructor(
Expand Down
Loading

0 comments on commit 1c67095

Please sign in to comment.