Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

마지막 #3

Open
wants to merge 3 commits into
base: hyuckkim
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions MyTodoApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions MyTodoApp/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
71 changes: 71 additions & 0 deletions MyTodoApp/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.example.mytodoapp'
compileSdk 33

defaultConfig {
applicationId "com.example.mytodoapp"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'

implementation 'androidx.glance:glance-appwidget:1.0.0-alpha05'
implementation 'androidx.glance:glance-wear-tiles:1.0.0-alpha05'

implementation "androidx.datastore:datastore-preferences:1.0.0"
}
21 changes: 21 additions & 0 deletions MyTodoApp/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.mytodoapp

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.mytodoapp", appContext.packageName)
}
}
36 changes: 36 additions & 0 deletions MyTodoApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyTodoApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyTodoApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SimpleCounterWidgetReceiver" android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/counter_widget" />
</receiver>
</application>

</manifest>
175 changes: 175 additions & 0 deletions MyTodoApp/app/src/main/java/com/example/mytodoapp/CounterWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.example.mytodoapp

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.glance.*
import androidx.glance.action.ActionParameters
import androidx.glance.action.actionParametersOf
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.action.ActionCallback
import androidx.glance.appwidget.action.actionRunCallback
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.lazy.LazyColumn
import androidx.glance.appwidget.state.updateAppWidgetState
import androidx.glance.layout.Alignment
import androidx.glance.layout.Box
import androidx.glance.layout.Column
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.height
import androidx.glance.layout.padding
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider

object CounterWidget : GlanceAppWidget() {
override val sizeMode = SizeMode.Single
val TODO_LIST_ID = ActionParameters.Key<Int>("todo_list_id")

val jsonTodoList = stringPreferencesKey("json")

@Composable
override fun Content() {
val data = currentState(key = jsonTodoList) ?: ""
val dataList = remember(data) {
data.split("\n").filter { it.isNotEmpty() }
}
Column(
modifier = GlanceModifier
.fillMaxSize()
.background(Color.DarkGray),
verticalAlignment = Alignment.Vertical.CenterVertically,
horizontalAlignment = Alignment.Horizontal.CenterHorizontally
) {
ContentTitle()
LazyColumn(
modifier = GlanceModifier
.fillMaxSize()
.background(Color.LightGray)
) {
items(count = dataList.size) {
ContentColumnItem(index = it, text = dataList[it])
}
}
}
}

@Composable
fun ContentTitle() {
Box(
modifier = GlanceModifier
.fillMaxWidth()
.height(50.dp)
) {
Box(
modifier = GlanceModifier.fillMaxSize(),
contentAlignment = Alignment.CenterStart
) {
Text(
text = "My Todo App",
style = TextStyle(
fontWeight = FontWeight.Medium,
color = ColorProvider(Color.White),
fontSize = 26.sp
)
)
}
Box(
modifier = GlanceModifier.fillMaxSize(),
contentAlignment = Alignment.CenterEnd
) {
Button(
text = "Refresh",
onClick = actionRunCallback<TodoRefreshActionCallback>()
)
}
}
}

@Composable
fun ContentColumnItem(index: Int, text: String) {
Box(
modifier = GlanceModifier
.height(40.dp)
.fillMaxWidth()
.background(Color.White)
.padding(1.dp)
.cornerRadius(3.dp)
) {
Box(
modifier = GlanceModifier.fillMaxSize(),
contentAlignment = Alignment.CenterStart
) {
Text(
text,
style = TextStyle(
fontWeight = FontWeight.Medium,
color = ColorProvider(Color.Black),
fontSize = 16.sp
)
)
}
Box(
modifier = GlanceModifier.fillMaxSize(),
contentAlignment = Alignment.CenterEnd
) {
Button(
text = "✓",
onClick = actionRunCallback<TodoClearActionCallback>(
actionParametersOf(TODO_LIST_ID to index)
)
)
}
}
}
}

class SimpleCounterWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget
get() = CounterWidget
}

class TodoRefreshActionCallback : ActionCallback {
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters
) {
val v = TodoList.getString(context)
updateAppWidgetState(context, glanceId) { prefs ->
prefs[CounterWidget.jsonTodoList] = v
CounterWidget.update(context, glanceId)
}
updateAppWidgetState(context, glanceId) {
CounterWidget.update(context, glanceId)
}
}
}

class TodoClearActionCallback : ActionCallback {
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters
) {
val todoId = parameters.getOrDefault(CounterWidget.TODO_LIST_ID, 0)
TodoList.remove(context, todoId)
val v = TodoList.getString(context)

updateAppWidgetState(context, glanceId) { prefs ->
prefs[CounterWidget.jsonTodoList] = v
CounterWidget.update(context, glanceId)
}
updateAppWidgetState(context, glanceId) {
CounterWidget.update(context, glanceId)
}
}
}
Loading