Skip to content
Open
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
18 changes: 18 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Properties

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
Expand All @@ -18,6 +20,15 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

// defaultConfig 블록 안에 추가
val properties = Properties()
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
properties.load(localPropertiesFile.inputStream())
}
val baseUrl = properties.getProperty("BASE_URL")
buildConfigField("String", "BASE_URL", "\"$baseUrl\"")

}

buildTypes {
Expand All @@ -38,6 +49,7 @@ android {
}
buildFeatures {
compose = true
buildConfig = true
}
}

Expand Down Expand Up @@ -68,10 +80,16 @@ dependencies {

// Coroutines
implementation(libs.kotlinx.coroutines.android)
implementation(libs.retrofit2.retrofit)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)

//retro & okhttp
implementation(libs.retrofit.converter.gson)
implementation(libs.okhttp)
implementation(libs.okhttp.logging.interceptor)
}
23 changes: 19 additions & 4 deletions app/src/main/java/com/example/kuit6_android_api/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.example.kuit6_android_api

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
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.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.content.ContextCompat
import androidx.navigation.compose.rememberNavController
Expand All @@ -32,6 +38,7 @@ class MainActivity : ComponentActivity() {
}
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -40,15 +47,23 @@ class MainActivity : ComponentActivity() {

setContent {
KUIT6_Android_APITheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background

val snackBarState = remember { SnackbarHostState() }

Scaffold(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
snackbarHost = {
SnackbarHost(snackBarState)
}
) {
val navController = rememberNavController()

NavGraph(
navController = navController,
startDestination = PostListRoute
startDestination = PostListRoute,
snackBarState = snackBarState
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.kuit6_android_api.data.api

import com.example.kuit6_android_api.data.model.response.BaseResponse
import com.example.kuit6_android_api.data.model.response.PostResponse
import com.example.kuit6_android_api.data.model.request.PostCreateRequest
import okhttp3.MultipartBody
import retrofit2.http.*

interface ApiService {
@GET("/api/posts")
suspend fun getPosts(): BaseResponse<List<PostResponse>>

@POST("/api/posts")
suspend fun createPost(
@Query("author") author: String = "시헌",
@Body request: PostCreateRequest
): BaseResponse<PostResponse>

@DELETE("/api/posts/{id}")
suspend fun deletePost(
@Path("id") id: Long
): BaseResponse<Unit> // data에 빈 객체 반환

@GET("/api/posts/{id}")
suspend fun getPostDetail(
@Path("id") id: Long
): BaseResponse<PostResponse>

// 업데이트 요청
@PUT("/api/posts/{id}")
suspend fun updatePost(
@Path("id") id: Long,
@Body request: PostCreateRequest // 수정된 내용 전달
): BaseResponse<PostResponse>

@Multipart
@POST("/api/images/upload")
suspend fun uploadImage(
@Part file: MultipartBody.Part
): BaseResponse<Map<String, String>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.kuit6_android_api.data.api

import com.example.kuit6_android_api.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitClient {

private val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build()

private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()

val apiService: ApiService = retrofit.create(ApiService::class.java)
}
17 changes: 0 additions & 17 deletions app/src/main/java/com/example/kuit6_android_api/data/model/Post.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.kuit6_android_api.data.model.request

import kotlinx.serialization.SerialName

data class PostCreateRequest(
@SerialName(value = "title") val title:String,
@SerialName(value = "content") val content:String,
@SerialName(value = "imageUrl") val imageUrl:String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.kuit6_android_api.data.model.response

import kotlinx.serialization.SerialName

data class AuthorResponse (
@SerialName(value = "id") val id : Long,
@SerialName(value = "username") val username: String,
@SerialName(value = "profileImageUrl") val profileImageUrl:String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.kuit6_android_api.data.model.response

import kotlinx.serialization.SerialName

data class BaseResponse<T>(
@SerialName(value = "success") val success : Boolean,
@SerialName(value = "message") val message : String?,
@SerialName(value = "data") val data: T?,
@SerialName(value = "timestamp") val timestamp: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.kuit6_android_api.data.model.response

import kotlinx.serialization.SerialName

data class PostResponse(
@SerialName(value = "id") val id : Long,
@SerialName(value = "title") val title:String,
@SerialName(value = "content") val content : String,
@SerialName(value = "imageUrl") val imageUrl:String?,
@SerialName(value = "author") val author: AuthorResponse,
@SerialName(value = "createdAt") val createdAt:String,
@SerialName(value = "updatedAt") val updatedAt:String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.kuit6_android_api.ui.navigation

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -13,7 +15,8 @@ import com.example.kuit6_android_api.ui.post.screen.PostListScreen
@Composable
fun NavGraph(
navController: NavHostController,
startDestination: Any = PostListRoute
startDestination: Any = PostListRoute,
snackBarState: SnackbarHostState
) {
NavHost(
navController = navController,
Expand All @@ -40,7 +43,8 @@ fun NavGraph(
},
onEditClick = { postId ->
navController.navigate(PostEditRoute(postId))
}
},
snackBarState = snackBarState
)
}

Expand All @@ -51,7 +55,8 @@ fun NavGraph(
},
onPostCreated = {
navController.popBackStack()
}
},
snackBarState = snackBarState
)
}

Expand All @@ -65,7 +70,8 @@ fun NavGraph(
},
onPostUpdated = {
navController.popBackStack()
}
},
snackBarState = snackBarState
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.kuit6_android_api.data.model.Author
import com.example.kuit6_android_api.data.model.Post
import com.example.kuit6_android_api.data.model.response.AuthorResponse
import com.example.kuit6_android_api.data.model.response.PostResponse
import com.example.kuit6_android_api.util.formatDateTime

@Composable
fun PostItem(
post: Post,
post: PostResponse,
onClick: () -> Unit
) {
Card(
Expand Down Expand Up @@ -153,12 +153,12 @@ fun PostItem(
fun PostItemPreview() {
MaterialTheme {
PostItem(
post = Post(
post = PostResponse(
id = 1,
title = "샘플 게시글 제목",
content = "이것은 샘플 게시글 내용입니다. 미리보기에서는 두 줄까지만 표시됩니다.",
imageUrl = null,
author = Author(1, "testuser", null),
author = AuthorResponse(1, "testuser", null),
createdAt = "2025-10-03T12:00:00",
updatedAt = "2025-10-03T12:00:00"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,41 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.kuit6_android_api.ui.post.viewmodel.PostViewModel
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PostCreateScreen(
onNavigateBack: () -> Unit,
onPostCreated: () -> Unit,
snackBarState: SnackbarHostState,
viewModel: PostViewModel = viewModel()
) {
val context = LocalContext.current
var author by remember { mutableStateOf("") }
var title by remember { mutableStateOf("") }
var content by remember { mutableStateOf("") }
var selectedImageUri by remember { mutableStateOf<Uri?>(null) }
val scope = rememberCoroutineScope()

val imagePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent()
Expand Down Expand Up @@ -187,9 +195,10 @@ fun PostCreateScreen(

Button(
onClick = {
val finalAuthor = author.ifBlank { "anonymous" }
val finalAuthor = author
viewModel.createPost(finalAuthor, title, content, null) {
onPostCreated()
scope.launch { snackBarState.showSnackbar("게시글이 작성되었습니다.") }
}
},
modifier = Modifier
Expand Down Expand Up @@ -226,7 +235,8 @@ fun PostCreateScreenPreview() {
MaterialTheme {
PostCreateScreen(
onNavigateBack = {},
onPostCreated = {}
onPostCreated = {},
snackBarState = remember { SnackbarHostState() }
)
}
}
Loading