[ The imperialist Japanese military must officially acknowledge and apologize for the coercion of young women into sexual slaves. / 幼い女性を性の奴隷にした帝國主義日本軍の?制性を公式に認め、謝罪しなければならない. ]

사용자

ID:
PW:

INDEX
01.게시판
게시판 [72]
02.File Book
File 자료실 [96]
Site Link [14]
개인폴더 [200]
03.Field Book
Altibase [19]
Tibero [30]
MS-SQL [28]
MySQL [47]
DB2 [79]
ORACLE [3099]
PSQL [135]
CUBRID [5]
기타정보 [170]
운영체제 [613]
04.Q/A Book
Q/A [53]
05.방명록
방명록 [54]
레벨업 [37]
구인/구직 [2]

기념일
Search
LINK
+ 가족 Hompy
+ DNSEver
IOTN :: Field Book :: 운영체제

 회원가입

Android 갤럭시 워치 STOPWATCH
최길호 [LIST]   2024-04-30 14:44:15, 조회 : 22

따라하기
https://youtu.be/irIGZj1YON8

// MainActivity.kt

package com.dbtech.gilho.presentation

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Pause
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.Stop
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.wear.compose.material.Button
import androidx.wear.compose.material.ButtonDefaults
import androidx.wear.compose.material.Icon
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.TimeText
import androidx.wear.compose.material.TimeTextDefaults

// https://youtu.be/irIGZj1YON8
// C:\Users\gilho\AppData\Local\Android\Sdk\platform-tools\adb pair 192.168.137.3:34743
// C:\Users\gilho\AppData\Local\Android\Sdk\platform-tools\adb connect 192.168.137.3:40165
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val viewModel = viewModel<StopWatchViewModel>()
            val timerState by viewModel.timerState.collectAsStateWithLifecycle()
            val stopWatchText by viewModel.stopWatchText.collectAsStateWithLifecycle()
            TimeText( // 상단 현재 시간 표시
                timeTextStyle = TimeTextDefaults.timeTextStyle(
//                fontSize = 10.sp // 크기
                )
            )
            StopWatch( // STOP WATCH
                state = timerState,
                text = stopWatchText,
                onToggleRunning = viewModel::toggleIsRunning,
                onReset = viewModel::resetTimer,
                modifier = Modifier.fillMaxSize()
            )
        }
    }
}

@Composable
private fun StopWatch(
    state: TimerState,
    text: String,
    onToggleRunning: () -> Unit,
    onReset: () -> Unit,
    modifier: Modifier = Modifier
) {
    Column( // 컬럼 추가
        modifier = modifier
            .background(MaterialTheme.colors.background), // 워치 백그라운드 컬러
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // Time Text
        Text(
            text = "길호 STOPWATCH",
            fontSize = if (state != TimerState.RESET) // 실행 하면 폰트 크기를 크게 한다.
            { 10.sp } else { 30.sp }, // 중지 하면 작게
            textAlign = TextAlign.Center
        )
        Spacer(modifier = Modifier.height(10.dp)) // 위 아래 간격
        Text(
            text = text,
            fontSize = if (state != TimerState.RESET) { 30.sp } else { 10.sp },
            fontWeight = FontWeight.SemiBold,
            color = Color.White,
            textAlign = TextAlign.Center
        )
        Spacer(modifier = Modifier.height(8.dp)) // 위 아래 간격
        Row( // 1 | 2 나눈다
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center
        ) {
            // 1 play 버튼
            Button(onClick = onToggleRunning) {
                Icon(
                    imageVector = if (state == TimerState.RUNNING) {
                        // implementation("androidx.compose.material:material-icons-extended:1.6.6")
                        Icons.Default.Pause
                    } else {
                        Icons.Default.PlayArrow
                    },
                    contentDescription = null
                )
            }
            Spacer(modifier = Modifier.width(8.dp)) // 옆 사이 간격
            // 2 stop 버튼
            Button(
                onClick = onReset,
                enabled = state != TimerState.RESET,
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = MaterialTheme.colors.surface //  투명 변경
                )
            ) {
                Icon(
                    imageVector = Icons.Default.Stop,
                    contentDescription = null
                )
            }
        }
    }
}


// StopWatchViewModel.kt
@file:OptIn(ExperimentalCoroutinesApi::class)

package com.dbtech.gilho.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import java.time.LocalTime
import java.time.format.DateTimeFormatter

class StopWatchViewModel: ViewModel() {
    private val _elapsedTime = MutableStateFlow(0L)
    private val _timerState = MutableStateFlow(TimerState.RESET)
    val timerState = _timerState.asStateFlow()
    private  val formatter = DateTimeFormatter.ofPattern("HH:mm:ss:SS")
    var stopWatchText = _elapsedTime
        .map{ millis ->
            LocalTime.ofNanoOfDay(millis * 1_000_000).format(formatter)
        }
        .stateIn(
            viewModelScope,
            SharingStarted.WhileSubscribed(5000),
            "00:00:00:00"
        )
    init {
        _timerState
            .flatMapLatest { timerState ->
                getTimerFlow(
                    isRunning = timerState == TimerState.RUNNING
                )
            }
            .onEach { timeDiff ->
                _elapsedTime.update { it + timeDiff }
            }
            .launchIn(viewModelScope)
    }
    fun toggleIsRunning() {
        when(timerState.value) {
            TimerState.RUNNING -> _timerState.update { TimerState.PAUSED }
            TimerState.PAUSED,
            TimerState.RESET -> _timerState.update { TimerState.RUNNING }
        }
    }
    fun resetTimer() {
        _timerState.update { TimerState.RESET }
        _elapsedTime.update { 0L }
    }
    private fun getTimerFlow(isRunning: Boolean): Flow<Long> {
        return flow {
            var startMillis = System.currentTimeMillis()
            while (isRunning){
                val currentMillis = System.currentTimeMillis()
                val timeDiff = if ( currentMillis > startMillis ) {
                    currentMillis - startMillis
                } else 0L
                emit(timeDiff)
                startMillis = System.currentTimeMillis()
                delay(10L)
            }
        }
    }
}


// TimerState.kt
package com.dbtech.gilho.presentation

enum class TimerState {
    RUNNING, PAUSED, RESET
}


// build.gradle.kts (:wear)
dependencies {
....
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
    implementation("androidx.compose.material:material-icons-extended:1.6.6")
}
3.238.121.7


  LIST

제목 작성자 작성일 조회
Linux  NIC add cli / nmcli device show / nmtui    최길호 2024/07/04 14
Linux  disk hdd 순서 꼬임 확인 조치    최길호 2024/07/03 13
Linux  localectl set-locale LANG=en_US.UTF-8 / Non UTF-8 locale (ANSI_X3.4-1968) is not supported    최길호 2024/06/05 11
Linux  proxy Server / Client / 프록시 서버    최길호 2024/05/31 26
Android  git 연결/제거 [ 안드로이드 스튜디오 ]    최길호 2024/05/03 23
Android  Android 갤럭시 워치 STOPWATCH    최길호 2024/04/30 22
Windows  시스템 오류 86이(가) 생겼습니다. / net use / xcopy / forfiles    최길호 2024/02/15 30
UNIX  strace truss tusc / unix trace    최길호 2024/01/10 56
Solaris  WARNING: Start sector of partition 0 is not aligned to physical block size (4096 bytes) of the disk. This can cause I/O performance degradation.    최길호 2023/12/11 44
Linux  dnf install anydesk    최길호 2023/12/07 46
UNIX  arch filesystem 100% full 예방 / archive log 10개씩 제거 / df find rm    최길호 2023/11/23 64
UNIX  vi 화면 나누기 [ :vs :sp :new Ctrl+w w ]    최길호 2023/11/11 42
UNIX  shell, seq grep sed 연속된 숫자중. 끊어진 숫자들 찾아라.    최길호 2023/11/08 47
Android  ntfy.sh / 푸시 알림 보내기    최길호 2023/09/11 63
Android  Android 앱 자동 종료 방지    최길호 2023/08/25 47
Android  Android 부팅 자동 실행    최길호 2023/08/25 55
Android  mp4 play [ 워치 online play test ]    최길호 2023/07/21 79
Android  BluetoothLog.apk [ Bluetooth 연결,종료,시간,위치 로그 기록 ]     최길호 2023/07/20 37
Android  갤럭시 워치 apk 설치     최길호 2023/07/20 67
Android  org.postgresql.util.PSQLException: Something unusual has occurred    최길호 2023/07/04 36

    목록보기   다음페이지 1 [2][3][4][5][6][7][8][9][10]..[31]   [다음 10개]
       

Copyright 1999-2024 Zeroboard / skin by 최길호(gilho.kr@gmail.com)
최근 댓글
2016/06/09
201606 추가 grid/runcluvfy.sh...
by 최길호
2016/05/09
lg tab full screen VNCSERVE...
by 최길호
2013/10/17
rsync -avH /data/*

by 최길호
최근 게시물
07/22
[ORACLE]
ogg Rolling over remote file [....
by 최길호
07/15
[ORACLE]
PRKF-1125 : multiple values sp....
by 최길호
07/04
[운영체제]
NIC add cli / nmcli device sho....
by 최길호