Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] #39 카카오톡 공유하기 전송 구현 #80

Merged
merged 16 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ dependencies {
implementation(libs.orbit.compose)
implementation(libs.orbit.core)
implementation(libs.orbit.viewmodel)

// kakao
implementation(libs.kakao.share.v2)
}
18 changes: 17 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<queries>
<package android:name="com.kakao.talk" />
</queries>

<uses-permission android:name="android.permission.INTERNET" />

<application
Expand All @@ -14,6 +18,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Pokit"
tools:targetApi="34">

<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
Expand All @@ -22,16 +27,27 @@
android:theme="@style/Theme.Pokit"
android:launchMode="singleInstance"
tools:ignore="DiscouragedApi,LockedOrientationActivity">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:host="kakaolink"
android:scheme="kakao7890f93caf1d9d5da976da4b4bc6e5e7" />
</intent-filter>

</activity>
</application>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/pokitmons/pokit/PokitApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package pokitmons.pokit

import android.app.Application
import com.google.firebase.FirebaseApp
import com.kakao.sdk.common.KakaoSdk
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class PokitApplication : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
KakaoSdk.init(this, "7890f93caf1d9d5da976da4b4bc6e5e7")
l5x5l marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pokitmons.pokit.core.feature.utils

import android.content.Context
import android.content.Intent

fun ShareUrlLink(
context: Context,
url: String,
chooserTitle: String = "Pokit",
) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, url)
}
context.startActivity(Intent.createChooser(intent, chooserTitle))
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import pokitmons.pokit.data.model.auth.request.TokenRequest
import javax.inject.Inject

class AuthAuthenticator @Inject constructor(
private val tokenManager: TokenManager,
private val authManager: AuthManager,
private val tokenApi: TokenApi,
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request {
val refreshToken = runBlocking {
tokenManager.getRefreshToken().first()
authManager.getRefreshToken().first()
}

val accessToken = runBlocking {
tokenApi.reissue(TokenRequest(refreshToken ?: "")).also { response ->
tokenManager.saveAccessToken(response.accessToken)
authManager.saveAccessToken(response.accessToken)
}
}
return newRequestWithToken(accessToken.accessToken, response.request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package pokitmons.pokit.data.datasource.local

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class TokenManager @Inject constructor(
class AuthManager @Inject constructor(
private val dataStore: DataStore<androidx.datastore.preferences.core.Preferences>,
) {
companion object {
val ACCESS_TOKEN = stringPreferencesKey("access_token")
val REFRESH_TOKEN = stringPreferencesKey("refresh_token")
val AUTH_TYPE = stringPreferencesKey("auth_type")
val USER_ID = intPreferencesKey("user_id")

private const val INVALID_USER_ID = -1
}

fun getAccessToken(): Flow<String?> {
Expand Down Expand Up @@ -51,4 +55,16 @@ class TokenManager @Inject constructor(
prefs[REFRESH_TOKEN] = token
}
}

fun getUserId(): Flow<Int> {
return dataStore.data.map { prefs ->
prefs[USER_ID] ?: INVALID_USER_ID
}
}

suspend fun setUserId(userId: Int) {
dataStore.edit { prefs ->
prefs[USER_ID] = userId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import pokitmons.pokit.data.datasource.local.TokenManager
import pokitmons.pokit.data.datasource.local.AuthManager
import javax.inject.Inject

// 토큰 api 수정될 때 까지 사용
class BearerTokenInterceptor @Inject constructor(private val tokenManager: TokenManager) : Interceptor {
class BearerTokenInterceptor @Inject constructor(private val authManager: AuthManager) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val token: String = runBlocking {
tokenManager.getAccessToken().first()
authManager.getAccessToken().first()
} ?: ""

val originalRequest: Request = chain.request()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import pokitmons.pokit.data.api.RemindApi
import pokitmons.pokit.data.api.SettingApi
import pokitmons.pokit.data.api.TokenApi
import pokitmons.pokit.data.datasource.local.AuthAuthenticator
import pokitmons.pokit.data.datasource.local.TokenManager
import pokitmons.pokit.data.datasource.local.AuthManager
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit
import javax.inject.Named
Expand Down Expand Up @@ -75,23 +75,23 @@ object NetworkModule {

@Singleton
@Provides
fun provideTokenManager(dataStore: DataStore<Preferences>): TokenManager {
return TokenManager(dataStore)
fun provideTokenManager(dataStore: DataStore<Preferences>): AuthManager {
return AuthManager(dataStore)
}

@Singleton
@Provides
fun provideAuthenticator(
tokenManager: TokenManager,
authManager: AuthManager,
tokenApi: TokenApi,
): AuthAuthenticator {
return AuthAuthenticator(tokenManager, tokenApi)
return AuthAuthenticator(authManager, tokenApi)
}

@Singleton
@Provides
fun provideInterceptor(tokenManager: TokenManager): BearerTokenInterceptor {
return BearerTokenInterceptor(tokenManager)
fun provideInterceptor(authManager: AuthManager): BearerTokenInterceptor {
return BearerTokenInterceptor(authManager)
}

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pokitmons.pokit.data.repository.auth

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import pokitmons.pokit.data.datasource.local.TokenManager
import pokitmons.pokit.data.datasource.local.AuthManager
import pokitmons.pokit.data.datasource.remote.auth.AuthDataSource
import pokitmons.pokit.data.mapper.auth.AuthMapper
import pokitmons.pokit.data.model.auth.request.SNSLoginRequest
Expand All @@ -21,7 +21,7 @@ import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val remoteAuthDataSource: AuthDataSource,
private val tokenManager: TokenManager,
private val authManager: AuthManager,
) : AuthRepository {
override suspend fun snsLogin(
authPlatform: String,
Expand Down Expand Up @@ -61,7 +61,7 @@ class AuthRepositoryImpl @Inject constructor(
remoteAuthDataSource.withdraw(
WithdrawRequest(
refreshToken = "",
authPlatform = tokenManager.getAuthType().first()
authPlatform = authManager.getAuthType().first()
)
)
PokitResult.Success(Unit)
Expand All @@ -71,18 +71,26 @@ class AuthRepositoryImpl @Inject constructor(
}

override suspend fun setAccessToken(token: String) {
tokenManager.saveAccessToken(token)
authManager.saveAccessToken(token)
}

override suspend fun setRefreshToken(token: String) {
tokenManager.saveRefreshToken(token)
authManager.saveRefreshToken(token)
}

override suspend fun setAuthType(type: String) {
tokenManager.setAuthType(type)
authManager.setAuthType(type)
}

override suspend fun getAuthType(): Flow<String> {
return tokenManager.getAuthType()
return authManager.getAuthType()
}

override suspend fun setUserId(userId: Int) {
authManager.setUserId(userId)
}

override suspend fun getUserId(): Flow<Int> {
return authManager.getUserId()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ interface AuthRepository {
// TODO 리팩토링
suspend fun setAuthType(type: String)
suspend fun getAuthType(): Flow<String>

suspend fun setUserId(userId: Int)
suspend fun getUserId(): Flow<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.coroutines.flow.Flow
import pokitmons.pokit.domain.repository.auth.AuthRepository
import javax.inject.Inject

class TokenUseCase @Inject constructor(private val authRepository: AuthRepository) {
class AuthUseCase @Inject constructor(private val authRepository: AuthRepository) {
suspend fun setAccessToken(token: String) {
authRepository.setAccessToken(token)
}
Expand All @@ -20,4 +20,12 @@ class TokenUseCase @Inject constructor(private val authRepository: AuthRepositor
suspend fun getAuthType(): Flow<String> {
return authRepository.getAuthType()
}

suspend fun setUserId(userId: Int) {
authRepository.setUserId(userId)
}

suspend fun getUserId(): Flow<Int> {
return authRepository.getUserId()
}
}
3 changes: 3 additions & 0 deletions feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ dependencies {
// coil
implementation(libs.coil.compose)

// kakao share
implementation(libs.kakao.share.v2)

// module
implementation(project(":core:ui"))
implementation(project(":core:feature"))
Expand Down
Loading
Loading