Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

[코드랩] Retrofit #87

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 GuessTheWord-Starter/app/build.gradle
Original file line number Diff line number Diff line change
@@ -56,4 +56,7 @@ dependencies {
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"

//ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
}
Original file line number Diff line number Diff line change
@@ -17,11 +17,15 @@
package com.example.android.guesstheword.screens.game

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.GameFragmentBinding

@@ -30,17 +34,10 @@ import com.example.android.guesstheword.databinding.GameFragmentBinding
*/
class GameFragment : Fragment() {

// The current word
private var word = ""

// The current score
private var score = 0

// The list of words - the front of the list is the next word to guess
private lateinit var wordList: MutableList<String>

private lateinit var binding: GameFragmentBinding

private lateinit var viewModel: GameViewModel

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

@@ -52,79 +49,49 @@ class GameFragment : Fragment() {
false
)

resetList()
nextWord()
Log.i("GameFragment", "Called ViewModelProvider.get")
viewModel = ViewModelProvider(this).get(GameViewModel::class.java)

binding.correctButton.setOnClickListener { onCorrect() }
binding.skipButton.setOnClickListener { onSkip() }
binding.endGameButton.setOnClickListener { onEndGame() }
updateScoreText()
updateWordText()
return binding.root

}

/**
* Resets the list of words and randomizes the order
*/
private fun resetList() {
wordList = mutableListOf(
"queen",
"hospital",
"basketball",
"cat",
"change",
"snail",
"soup",
"calendar",
"sad",
"desk",
"guitar",
"home",
"railway",
"zebra",
"jelly",
"car",
"crow",
"trade",
"bag",
"roll",
"bubble"
)
wordList.shuffle()
}

/** Methods for buttons presses **/
/** Methods for button click handlers **/

private fun onSkip() {
score--
nextWord()
viewModel.onSkip()
updateWordText()
updateScoreText()
}

private fun onCorrect() {
score++
nextWord()
}

/**
* Moves to the next word in the list
*/
private fun nextWord() {
if (!wordList.isEmpty()) {
//Select and remove a word from the list
word = wordList.removeAt(0)
}
viewModel.onCorrect()
updateWordText()
updateScoreText()
}


/** Methods for updating the UI **/

private fun updateWordText() {
binding.wordText.text = word
binding.wordText.text = viewModel.word
}

private fun updateScoreText() {
binding.scoreText.text = score.toString()
binding.scoreText.text = viewModel.score.toString()
}

private fun onEndGame() {
gameFinished()
}

private fun gameFinished() {
Toast.makeText(activity, "Game has just finished", Toast.LENGTH_SHORT).show()
val action = GameFragmentDirections.actionGameToScore()
action.score = viewModel.score
NavHostFragment.findNavController(this).navigate(action)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.android.guesstheword.screens.game

import android.util.Log
import androidx.lifecycle.ViewModel

class GameViewModel : ViewModel() {

// The current word
var word = ""

// The current score
var score = 0

// The list of words - the front of the list is the next word to guess
private lateinit var wordList: MutableList<String>

/**
* Resets the list of words and randomizes the order
*/
fun resetList() {
wordList = mutableListOf(
"queen",
"hospital",
"basketball",
"cat",
"change",
"snail",
"soup",
"calendar",
"sad",
"desk",
"guitar",
"home",
"railway",
"zebra",
"jelly",
"car",
"crow",
"trade",
"bag",
"roll",
"bubble"
)
wordList.shuffle()
}


init {
resetList()
nextWord()
Log.i("GameViewModel", "GameViewModel created!")
}

/**
* Moves to the next word in the list
*/
private fun nextWord() {
if (!wordList.isEmpty()) {
//Select and remove a word from the list
word = wordList.removeAt(0)
}

}

/** Methods for buttons presses **/
fun onSkip() {
score--
nextWord()
}

fun onCorrect() {
score++
nextWord()
}

override fun onCleared() {
super.onCleared()
Log.i("GameViewModel", "GameViewModel destroyed!!")
}
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.navArgs
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.ScoreFragmentBinding
@@ -31,6 +32,9 @@ import com.example.android.guesstheword.databinding.ScoreFragmentBinding
*/
class ScoreFragment : Fragment() {

private lateinit var viewModel: ScoreViewModel
private lateinit var viewModelFactory: ScoreViewModelFactory

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@@ -45,6 +49,10 @@ class ScoreFragment : Fragment() {
false
)

viewModelFactory = ScoreViewModelFactory(ScoreFragmentArgs.fromBundle(requireArguments()).score)
viewModel = ViewModelProvider(this, viewModelFactory).get(ScoreViewModel::class.java)

binding.scoreText.text = viewModel.score.toString()
return binding.root
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.android.guesstheword.screens.score

import android.util.Log
import androidx.lifecycle.ViewModel

class ScoreViewModel(finalScore: Int) : ViewModel() {
// The final score
var score = finalScore

init {
Log.i("ScoreViewModel", "Final score is $finalScore")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.android.guesstheword.screens.score

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class ScoreViewModelFactory(private val finalScore: Int) : ViewModelProvider.Factory {

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if(modelClass.isAssignableFrom(ScoreViewModel::class.java)) {
return ScoreViewModel(finalScore) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
18 changes: 18 additions & 0 deletions MarsRealEstate-Starter/app/build.gradle
Original file line number Diff line number Diff line change
@@ -39,6 +39,15 @@ android {
buildFeatures {
dataBinding true
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
@@ -59,4 +68,13 @@ dependencies {

// Core with Ktx
implementation "androidx.core:core-ktx:$version_core"

// Retrofit
implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
implementation "com.squareup.retrofit2:converter-scalars:$version_retrofit"

implementation "com.squareup.moshi:moshi-kotlin:$version_moshi"
implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
implementation "com.squareup.retrofit2:converter-scalars:$version_retrofit"
implementation "com.squareup.retrofit2:converter-moshi:$version_retrofit"
}
2 changes: 2 additions & 0 deletions MarsRealEstate-Starter/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.marsrealestate">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Original file line number Diff line number Diff line change
@@ -17,4 +17,31 @@

package com.example.android.marsrealestate.network

private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com/"
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET

private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"

private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()

private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()

interface MarsApiService {
@GET("/realestate")
suspend fun getProperties(): List<MarsProperty>
}

object MarsApi {
val retrofitService: MarsApiService by lazy {
retrofit.create(MarsApiService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -17,4 +17,11 @@

package com.example.android.marsrealestate.network

class MarsProperty()
import com.squareup.moshi.Json

data class MarsProperty(
val id: String,
@Json(name = "img_src") val img_src: String,
val type: String,
val price: Double
)
Loading