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

MainActivity 정리하기 #80

Merged
merged 25 commits into from
Dec 7, 2023
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
10 changes: 8 additions & 2 deletions frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ dependencies {
implementation("androidx.test.ext:junit-ktx:1.1.5")
implementation("androidx.compose.foundation:foundation-layout-android:1.5.4")
implementation("io.coil-kt:coil-compose:2.5.0")

implementation("androidx.work:work-runtime-ktx:2.8.0")
androidTestImplementation("androidx.work:work-testing:2.8.0")

implementation("com.squareup.picasso:picasso:2.8")

testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:4.11.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
Expand All @@ -86,8 +92,8 @@ dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.5.4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation("com.google.android.gms:play-services-location:17.0.0")
implementation("com.google.android.gms:play-services-maps:17.0.0")
implementation("com.google.android.gms:play-services-location:21.0.1")
implementation("com.google.android.gms:play-services-maps:18.2.0")
implementation("com.google.maps.android:maps-compose:2.11.4")
implementation("androidx.activity:activity-ktx:1.3.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1")
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
android:theme="@style/Theme.Frontend" />

<activity
android:name=".MapActivity"
android:name=".MainActivity"
android:exported="false"
android:label="@string/title_activity_map"
android:theme="@style/Theme.Frontend" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,46 @@ import android.os.Bundle
import android.provider.Settings
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.frontend.ui.component.BottomBar
import com.example.frontend.ui.component.MapWithMarker
import androidx.lifecycle.lifecycleScope
import com.example.frontend.ui.map.FriendsMapUI
import com.example.frontend.ui.theme.FrontendTheme
import com.example.frontend.viewmodel.FriendsViewModel
import com.example.frontend.usecase.CheckInUseCase
import com.example.frontend.usecase.PeriodicCheckInUseCase
import com.example.frontend.usecase.SaveMyInfoUseCase
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.model.LatLng
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import javax.inject.Inject


@AndroidEntryPoint
class MapActivity : ComponentActivity() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
class MainActivity : ComponentActivity() {
@Inject
lateinit var fusedLocationClient: FusedLocationProviderClient
var currentLocation by mutableStateOf<LatLng?>(null)

@Inject
lateinit var checkInUseCase: CheckInUseCase

@Inject
lateinit var saveMyInfoUseCase: SaveMyInfoUseCase

private fun hasBackgroundLocationPermission(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ContextCompat.checkSelfPermission(
Expand Down Expand Up @@ -108,7 +100,6 @@ class MapActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

setContent {
FrontendTheme {
Expand All @@ -127,11 +118,16 @@ class MapActivity : ComponentActivity() {
}

checkAndRequestLocationPermissions()

PeriodicCheckInUseCase(this).execute()

lifecycleScope.launch {
saveMyInfoUseCase.execute()
}
}

override fun onResume() {
super.onResume()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

setContent {
FrontendTheme {
Expand Down Expand Up @@ -222,45 +218,3 @@ class MapActivity : ComponentActivity() {
}
}

@Composable
fun FriendsMapUI(currentLocation: LatLng?, onClick: () -> Unit) {
val viewModel: FriendsViewModel = viewModel()
val friendsList by viewModel.friendsList.observeAsState(emptyList())
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(Unit) {
while (isActive) {
viewModel.fetchFriends()
delay(3000)
}
}

Scaffold(
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
bottomBar = {
BottomBar(currentLocation)
}
) { paddingValues ->
Box(
modifier = Modifier
.padding(paddingValues)
.fillMaxSize(),
contentAlignment = Alignment.BottomEnd
) {
MapWithMarker(currentLocation, friendsList)
FloatingActionButton(
onClick = { onClick() },
modifier = Modifier.padding(16.dp)
) {
Icon(Icons.Filled.Add, contentDescription = null)
}

}

}


}


Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fun ShowMeetUpUIPreview() {
modifier = Modifier
.size(46.dp)
.clickable {
val nextIntent = Intent(context, MapActivity::class.java)
val nextIntent = Intent(context, MainActivity::class.java)
context.startActivity(nextIntent)
// finish current activity
if (context is Activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SplashScreenActivity : AppCompatActivity() {

if (userContextRepository.getIsLoggedIn()) {
// User is already logged in, skip the login activity
startActivity(Intent(this, MapActivity::class.java))
startActivity(Intent(this, MainActivity::class.java))
} else {
// User is not logged in, go to the login activity
startActivity(Intent(this, LoginActivity::class.java))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.frontend.api

import android.content.Context
import com.example.frontend.interceptor.AuthInterceptor
import com.example.frontend.model.CheckInModel
import com.example.frontend.utilities.BASE_URL
import com.example.frontend.utilities.GsonProvider
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.POST

/**
* Used to connect to the server
* About check in domain
*/
interface CheckInService {
@POST("/check_ins")
fun login(@Body checkInModel: CheckInModel): Call<CheckInModel?>?

companion object {
fun create(context: Context): CheckInService {
val logger = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }

val client = OkHttpClient.Builder()
.addInterceptor(logger)
.addInterceptor(AuthInterceptor(context))
.build()

return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonProvider.gson))
.build()
.create(CheckInService::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface UserService {
@GET("/users")
suspend fun getAllUsers(): List<UserModel>

@GET("/users/me")
suspend fun getMyInfo(): UserModel

@GET("/friends")
suspend fun getAllFriends(): List<UserModel>

Expand All @@ -22,4 +25,4 @@ interface UserService {

@POST("/friends/{id}/confirm")
suspend fun confirmRequest(@Path("id") friendId: Long)
}
}
52 changes: 52 additions & 0 deletions frontend/app/src/main/java/com/example/frontend/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.frontend.di

import android.content.Context
import com.example.frontend.api.FriendService
import com.example.frontend.api.UserService
import com.example.frontend.interceptor.AuthInterceptor
import com.example.frontend.repository.FriendsRepository
import com.example.frontend.utilities.BASE_URL
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
fun provideRetrofit(@ApplicationContext context: Context): Retrofit {
val authInterceptor = AuthInterceptor(context)

val okHttpClient = OkHttpClient.Builder()
.addInterceptor(authInterceptor)
.build()

return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

@Provides
fun provideFriendsRepository(api: FriendService): FriendsRepository {
return FriendsRepository(api)
}

@Provides
fun provideFriendAPI(retrofit: Retrofit): FriendService {
return retrofit.create(FriendService::class.java)
}

@Provides
fun provideUserService(retrofit: Retrofit): UserService {
return retrofit.create(UserService::class.java)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.frontend.di

import android.content.Context
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
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


/*
* 지리정보 관련 데이터를 제공하는 모듈
*/
@Module
@InstallIn(SingletonComponent::class)
object LocationModule {

@Provides
@Singleton
fun provideFusedLocationProviderClient(@ApplicationContext context: Context): FusedLocationProviderClient {
return LocationServices.getFusedLocationProviderClient(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.frontend.model

data class CheckInModel(
val latitude: Double,
val longitude: Double,
)
Loading