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
File renamed without changes.
3 changes: 3 additions & 0 deletions Assignment_10/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion newProject/.idea/misc.xml → Assignment_10/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}

dependencies {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.assignment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.assignment.databinding.ItemLapBinding

class LapAdapter(private val lapTimes: MutableList<String>) : RecyclerView.Adapter<LapAdapter.ViewHolder>() {

class ViewHolder(binding: ItemLapBinding) : RecyclerView.ViewHolder(binding.root) {
val lapTimeTextView = binding.lapTimeTextView
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ItemLapBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.lapTimeTextView.text = lapTimes[position]
}

override fun getItemCount() = lapTimes.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.example.assignment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.assignment.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var lapAdapter: LapAdapter
private val lapTimes = mutableListOf<String>()
private lateinit var binding: ActivityMainBinding

private lateinit var handler: Handler
private var isRunning = false
private var startTime = 0L
private var pausedTime = 0L

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

lapAdapter = LapAdapter(lapTimes)
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = lapAdapter

handler = Handler(Looper.getMainLooper())

binding.startPauseButton.setOnClickListener { toggleTimer() }
binding.stopButton.setOnClickListener { stopTimer()}
binding.lapButton.setOnClickListener { recordLap() }
}

private val updateTimer = object : Runnable {
override fun run() {
val currentTime = SystemClock.elapsedRealtime()
pausedTime = currentTime - startTime
updateTimerText()
handler.postDelayed(this, 10)
}
}

private fun toggleTimer() {
val startPauseButton = binding.startPauseButton
if (!isRunning) {
startTime = SystemClock.elapsedRealtime() - pausedTime
handler.post(updateTimer)
startPauseButton.text = getString(R.string.pause)
} else {
handler.removeCallbacks(updateTimer)
startPauseButton.text = getString(R.string.start)
}
isRunning = !isRunning
}

@SuppressLint("NotifyDataSetChanged")
private fun stopTimer() {
handler.removeCallbacks(updateTimer)
pausedTime = 0L
isRunning = false
binding.startPauseButton.text = getString(R.string.start)
lapTimes.clear()
lapAdapter.notifyDataSetChanged()
updateTimerText()
}

private fun recordLap() {
if (isRunning) {
lapTimes.add(0, formatTime(pausedTime))
lapAdapter.notifyItemInserted(0)
binding.recyclerView.scrollToPosition(0)
}
}

private fun updateTimerText() {
binding.timerTextView.text = formatTime(pausedTime)
}

private fun formatTime(millis: Long): String {
val ms = (millis % 1000) / 10
val seconds = (millis / 1000) % 60
val minutes = (millis / (1000 * 60)) % 60
return "%02d : %02d : %02d".format(minutes, seconds, ms)
}

override fun onDestroy() {
handler.removeCallbacks(updateTimer)
super.onDestroy()
}
}
57 changes: 57 additions & 0 deletions Assignment_10/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/timer_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timer_text"
android:textSize="32sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">

<Button
android:id="@+id/start_pause_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>

<Button
android:id="@+id/stop_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/stop"
android:layout_marginEnd="10dp"/>

<Button
android:id="@+id/lap_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/lap"
android:layout_marginEnd="10dp"/>
</LinearLayout>
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
17 changes: 17 additions & 0 deletions Assignment_10/app/src/main/res/layout/item_lap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
android:id="@+id/lap_time_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 10 additions & 0 deletions Assignment_10/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<resources>
<string name="app_name">assignment</string>

<!-- 타이머 텍스트 -->
<string name="timer_text">00 : 00 : 00</string>
<string name="start">Start</string>
<string name="pause">Pause</string>
<string name="stop">Stop</string>
<string name="lap">Lap</string>
</resources>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

This file was deleted.

19 changes: 0 additions & 19 deletions newProject/app/src/main/res/layout/activity_main.xml

This file was deleted.

3 changes: 0 additions & 3 deletions newProject/app/src/main/res/values/strings.xml

This file was deleted.