From 446a38d0c0a3074555591c91769ac89f5469ca19 Mon Sep 17 00:00:00 2001 From: kkyoungchan Date: Mon, 7 Jul 2025 16:25:51 +0900 Subject: [PATCH 1/2] assignment11 --- .idea/.gitignore | 3 + .idea/.name | 1 + .idea/AndroidProjectSystem.xml | 6 ++ .idea/compiler.xml | 6 ++ .idea/deploymentTargetSelector.xml | 10 +++ .idea/encodings.xml | 6 ++ .idea/gradle.xml | 19 ++++ .idea/migrations.xml | 10 +++ .idea/misc.xml | 10 +++ .idea/runConfigurations.xml | 17 ++++ .idea/vcs.xml | 6 ++ .../example/bcsd_android_2025_1/LapAdapter.kt | 39 ++++++++ .../example/bcsd_android_2025_1/LapItem.kt | 3 + .../bcsd_android_2025_1/MainActivity.kt | 89 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 57 ++++++++++-- app/src/main/res/layout/item_lap.xml | 9 ++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 6 ++ 18 files changed, 291 insertions(+), 7 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/AndroidProjectSystem.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/deploymentTargetSelector.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/migrations.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/example/bcsd_android_2025_1/LapAdapter.kt create mode 100644 app/src/main/java/com/example/bcsd_android_2025_1/LapItem.kt create mode 100644 app/src/main/res/layout/item_lap.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..c4e4683 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +BCSD_Android_2025-1 \ No newline at end of file diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..74dd639 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/bcsd_android_2025_1/LapAdapter.kt b/app/src/main/java/com/example/bcsd_android_2025_1/LapAdapter.kt new file mode 100644 index 0000000..de08281 --- /dev/null +++ b/app/src/main/java/com/example/bcsd_android_2025_1/LapAdapter.kt @@ -0,0 +1,39 @@ +package com.example.bcsd_android_2025_1 + +import android.view.LayoutInflater +import android.view.View +import android.widget.TextView +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView + +class LapAdapter : RecyclerView.Adapter() { + + private val lapList = mutableListOf() + + class LapViewHolder(View: View) : RecyclerView.ViewHolder(View) { + val lapText: TextView = View.findViewById(R.id.text_lap) + } + + fun addLap(time: String) { + lapList.add(0, LapItem(time)) + notifyItemInserted(0) + } + + fun clearLaps() { + lapList.clear() + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LapViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.item_lap, parent, false) + return LapViewHolder(view) + } + + override fun onBindViewHolder(holder: LapViewHolder, position: Int) { + holder.lapText.text = lapList[position].time + } + + override fun getItemCount(): Int { + return lapList.size + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bcsd_android_2025_1/LapItem.kt b/app/src/main/java/com/example/bcsd_android_2025_1/LapItem.kt new file mode 100644 index 0000000..70de622 --- /dev/null +++ b/app/src/main/java/com/example/bcsd_android_2025_1/LapItem.kt @@ -0,0 +1,3 @@ +package com.example.bcsd_android_2025_1 + +data class LapItem(val time : String) \ No newline at end of file diff --git a/app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt b/app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt index 3ffa0eb..7406c48 100644 --- a/app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt +++ b/app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt @@ -1,14 +1,103 @@ package com.example.bcsd_android_2025_1 import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.widget.Button +import android.widget.TextView import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { + + private lateinit var timerTextView: TextView + private lateinit var startPauseButton: Button + private lateinit var stopButton: Button + private lateinit var lapButton: Button + private lateinit var lapRecyclerView: RecyclerView + private lateinit var lapAdapter: LapAdapter + + private var startTime = 0L + private var pauseOffset = 0L + private var isRunning = false + private var handler = Handler(Looper.getMainLooper()) + + private val updateTimer = object : Runnable { + override fun run() { + val elapsed = SystemClock.elapsedRealtime() - startTime + timerTextView.text = nowTime(elapsed) + handler.postDelayed(this, 10) + } + } + private fun nowTime(ms: Long): String { + val minutes = (ms / 60000).toInt() + val seconds = ((ms % 60000) / 1000).toInt() + val millseconds = ((ms % 1000) / 10).toInt() + val timeString = getString(R.string.now_time, minutes, seconds, millseconds) + return timeString + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + timerTextView = findViewById(R.id.textview_timer) + startPauseButton = findViewById(R.id.button_start_pause) + stopButton = findViewById(R.id.button_stop) + lapButton = findViewById(R.id.button_lap) + lapRecyclerView = findViewById(R.id.lap_recyclerview) + lapAdapter = LapAdapter() + + lapRecyclerView.adapter = lapAdapter + lapRecyclerView.layoutManager = LinearLayoutManager(this) + + startPauseButton.setOnClickListener { + if (isRunning) { + pauseTimer() + } else { + startTimer() + } + } + stopButton.setOnClickListener { + stopTimer() + } + lapButton.setOnClickListener { + if (isRunning) { + val currentTime = SystemClock.elapsedRealtime() - startTime + val lapTime = nowTime(currentTime) + + lapAdapter.addLap(lapTime) + lapRecyclerView.scrollToPosition(0) + } + } + } + + private fun startTimer() { + startTime = SystemClock.elapsedRealtime() - pauseOffset + handler.post(updateTimer) + isRunning = true + startPauseButton.text = getString(R.string.text_pause) + } + + private fun pauseTimer() { + pauseOffset = SystemClock.elapsedRealtime() - startTime + handler.removeCallbacks(updateTimer) + isRunning = false + startPauseButton.text = getString(R.string.text_start) + } + + private fun stopTimer() { + handler.removeCallbacks(updateTimer) + startTime = 0L + pauseOffset = 0L + isRunning = false + timerTextView.text = getString(R.string.start_time) + startPauseButton.text = getString(R.string.text_start) + lapAdapter.clearLaps() } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 311f3cb..23acf34 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,19 +1,62 @@ + android:padding="16dp"> + +