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

[fix]作品詳細画面の通信部分を修正 #121

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
/gradle.properties
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
// app/gradle.propertiesをつくり、以下の3行を追加してください
// SERVER_IP=10.124.xx.xxx
// SERVER_PORT=xxxx
// USER_TOKEN="xxxxxx"
buildConfigField("String", "SERVER_IP", "\"${SERVER_IP}\"")
buildConfigField("int", "SERVER_PORT", "${SERVER_PORT}")
buildConfigField("String", "USER_TOKEN", "${USER_TOKEN}")
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.example.funcy_portfolio_android.model.data

import com.google.gson.annotations.SerializedName

data class WorkData(
val title: String,
val description: String,
val images: List<ImageData>,
val URL: String,
val movie_url: String,
@SerializedName("work_url")
val workUrl: String,
@SerializedName("movie_url")
val movieUrl: String,
val tags: List<TagData>,
val group: String?,
val security: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.content.Context
import com.example.funcy_portfolio_android.model.data.AuthData
import com.example.funcy_portfolio_android.model.data.SignupData
import com.example.funcy_portfolio_android.model.localDataSource.SharedPref
import com.example.funcy_portfolio_android.network.ApiService
import com.example.funcy_portfolio_android.network.ConnectFuncyApi

class UserRepository {
private val service = ApiService.service
private val service = ConnectFuncyApi.ApiService.service

fun saveUserId(context: Context, key: String, value: String) = SharedPref(context = context).saveSharedPrefString(key, value)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.funcy_portfolio_android.model.repository

import com.example.funcy_portfolio_android.model.data.WorkData
import com.example.funcy_portfolio_android.network.ApiService
import com.example.funcy_portfolio_android.network.ConnectFuncyApi
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import java.io.IOException

class WorkRepository() {
private val service = ApiService.service
private val service = ConnectFuncyApi.ApiService.service

private val userToken = com.example.funcy_portfolio_android.BuildConfig.USER_TOKEN

suspend fun registerWork(work: WorkData): String? {
val data = work
Expand All @@ -26,7 +28,24 @@ class WorkRepository() {
return res
}

suspend fun getWorkDetail(token: String, workId:String) = service.getWorkDetail(token = token, workId = workId)
suspend fun getWorkDetail(workId: String): WorkData {

val response = service.getWorkDetail(token = userToken, workId = workId)
var workData: WorkData? = null
if (response.isSuccessful) {
workData = response.body()
}
return workData ?: WorkData(
title = "",
description = "",
images = listOf(),
workUrl = "",
movieUrl = "",
tags = listOf(),
group = null,
security = 0
)
}

suspend fun getWork(token: String) = service.getWorks(token = token)
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
package com.example.funcy_portfolio_android.network

import com.example.funcy_portfolio_android.BuildConfig
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

private val URL = "http://192.168.3.17:9000/"
data class ServerConfig(
val serverIP: String,
val serverPort: Int
)

private val logger = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }
class ConnectFuncyApi {

private val httpClient = OkHttpClient.Builder()
.addInterceptor(logger)
.build()
// サーバーのURLを取得するための関数
// debug環境で動く
private fun getServerURL(): String {
val properties = readServerConfig()
val serverIP: String = properties.serverIP
val serverPort: Int = properties.serverPort

val gson = GsonBuilder()
.serializeNulls()
.create()
return "http://$serverIP:$serverPort/"
}

private fun readServerConfig(): ServerConfig {
return ServerConfig(BuildConfig.SERVER_IP, BuildConfig.SERVER_PORT)
}

private val logger =
HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }

private val httpClient = OkHttpClient.Builder()
.addInterceptor(logger)
.build()

val gson = GsonBuilder()
.serializeNulls()
.create()

private val retrofit = Retrofit.Builder()
.baseUrl(URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
private val retrofit = Retrofit.Builder()
.baseUrl(getServerURL())
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()

object ApiService {
val service: FuncyApi by lazy {
retrofit.create(FuncyApi::class.java)
object ApiService {
val service: FuncyApi by lazy {
ConnectFuncyApi().retrofit.create(FuncyApi::class.java)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface FuncyApi {
suspend fun getWorkDetail(
@Header("token") token: String,
@Path("workID") workId: String
): WorkData
): Response<WorkData>

//登録データ送信
@POST("sign/up")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.example.funcy_portfolio_android.ui.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.funcy_portfolio_android.R
import com.example.funcy_portfolio_android.databinding.FragmentMainBinding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,23 @@ class WorkDetailViewModel : ViewModel() {
//仮置きのテキスト
_userName.value = "田中太郎"

getWorkFromNetwork("Token1", "w1")
// 詳細画面のworkIdはここに入れてください
getWorkFromNetwork(workId = null)
}


private fun getWorkFromNetwork(token: String, workId: String) {
private fun getWorkFromNetwork(workId: String?) {
viewModelScope.launch {
_workDetailStatus.value = WorkApiStatus.LOADING
try {
_work.value = workRepository.getWorkDetail(token, workId)
Log.e(TAG, "アクセス成功")
_workDetailStatus.value = WorkApiStatus.DONE
if (workId == null) {
Log.e(TAG, "アクセス失敗 workId not found エラー")
_workDetailStatus.value = WorkApiStatus.ERROR
} else {
_work.value = workRepository.getWorkDetail(workId)
Log.e(TAG, "アクセス成功")
_workDetailStatus.value = WorkApiStatus.DONE
}
} catch (e: Exception) {
Log.e(TAG, "アクセス失敗 エラー:$e")
_workDetailStatus.value = WorkApiStatus.ERROR
Expand All @@ -90,8 +96,8 @@ class WorkDetailViewModel : ViewModel() {
_images.value = workValue.images
_explanation.value = workValue.description
_tagList.value = workValue.tags
_youtubeUrl.value = workValue.movie_url
_githubUrl.value = workValue.URL
_youtubeUrl.value = workValue.movieUrl
_githubUrl.value = workValue.workUrl
}

//Web遷移系の処理//////////////////////////////////
Expand Down