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

[코드랩] Feature internet filter #88

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions GuessTheWord-Starter/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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

Expand All @@ -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? {

Expand All @@ -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
Expand Up @@ -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
Expand All @@ -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?,
Expand All @@ -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")
}
}
20 changes: 20 additions & 0 deletions MarsRealEstate-Starter/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -59,4 +68,15 @@ 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"

implementation "com.github.bumptech.glide:glide:$version_glide"
}
2 changes: 2 additions & 0 deletions MarsRealEstate-Starter/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,54 @@

package com.example.android.marsrealestate

import android.view.View
import android.widget.ImageView
import androidx.core.net.toUri
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.android.marsrealestate.network.MarsProperty
import com.example.android.marsrealestate.overview.MarsApiStatus
import com.example.android.marsrealestate.overview.PhotoGridAdapter

@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {
imgUrl?.let {
val imgUri = it.toUri().buildUpon().scheme("https").build()
Glide.with(this.context)
.load(imgUri)
.apply(
RequestOptions()
.placeholder(R.drawable.loading_animation)
.error(R.drawable.ic_broken_image)
)
.into(this)
}
}

/**
* When there is no Mars property data (data is null), hide the [RecyclerView], otherwise show it.
*/
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<MarsProperty>?) {
val adapter = recyclerView.adapter as PhotoGridAdapter
adapter.submitList(data)
}

@BindingAdapter("marsApiStatus")
fun bindStatus(statusImageView: ImageView, status: MarsApiStatus?) {
when (status) {
MarsApiStatus.LOADING -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.loading_animation)
}
MarsApiStatus.ERROR -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.ic_connection_error)
}
MarsApiStatus.DONE -> {
statusImageView.visibility = View.GONE
}
}
}
Loading