Jetpack Compose????....

This commit is contained in:
sockenklaus
2022-07-15 23:52:58 +02:00
parent 82b85c20c8
commit 1cbdf9a442
17 changed files with 216 additions and 167 deletions

View File

@@ -0,0 +1,23 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="previewFile" value="true" />
</inspection_tool>
</profile>
</component>

View File

@@ -49,9 +49,9 @@ android {
dependencies {
implementation 'androidx.room:room-runtime:2.5.0-alpha02'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation "androidx.compose.ui:ui:1.3.0-alpha01"
implementation "androidx.compose.ui:ui-tooling-preview:1.3.0-alpha01"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
annotationProcessor 'androidx.room:room-compiler:2.5.0-alpha02'
implementation 'androidx.room:room-rxjava3:2.5.0-alpha02'
@@ -62,6 +62,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0'
@@ -69,15 +70,15 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.activity:activity-compose:1.5.0'
implementation 'androidx.compose.material:material:1.2.0-rc03'
implementation 'androidx.compose.animation:animation:1.2.0-rc03'
implementation 'androidx.compose.ui:ui-tooling:1.2.0-rc03'
implementation 'androidx.compose.material:material:1.3.0-alpha01'
implementation 'androidx.compose.animation:animation:1.3.0-alpha01'
implementation 'androidx.compose.ui:ui-tooling:1.3.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.0'
implementation 'com.google.android.material:compose-theme-adapter:1.1.14'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.2.0-rc03'
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.3.0-alpha01'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:1.1.1"
}

View File

@@ -1,71 +0,0 @@
package com.sockenklaus.batterytracker
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.text.input.TextFieldValue
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
class AddBattery : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BatteryTrackerTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Row() {
Text("Battery ID")
}
}
}
}
}
}
@Composable
fun myTextField(){
var batteryId by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue("example", TextRange(0,7)))
}
OutlinedTextField(
value = batteryId,
onValueChange = {batteryId = it},
label = { Text("Battery ID") }
)
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
BatteryTrackerTheme {
Greeting("Android")
}
}

View File

@@ -1,62 +0,0 @@
package com.sockenklaus.batterytracker.ui
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.sockenklaus.batterytracker.R
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [AddBatteryFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AddBatteryFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_battery, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AddBatteryFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AddBatteryFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

View File

@@ -0,0 +1,147 @@
package com.sockenklaus.batterytracker.ui.fragments
import android.inputmethodservice.Keyboard
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import com.sockenklaus.batterytracker.R
import com.sockenklaus.batterytracker.databinding.FragmentAddBatteryBinding
import com.sockenklaus.batterytracker.ui.theme.BatteryTrackerTheme
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [AddBatteryFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AddBatteryFragment : Fragment() {
// TODO: Rename and change types of parameters
private var _binding: FragmentAddBatteryBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentAddBatteryBinding.inflate(inflater, container, false)
val view = binding.root
binding.composeAddBattery.setContent {
BatteryTrackerTheme {
var batteryId by remember { mutableStateOf("") }
var declaredCapacity by remember { mutableStateOf("")}
ConstraintLayout {
val ( labelBatteryId, textBatteryId, labelDeclaredCapacity, textDeclaredCapacity, fab ) = createRefs()
val outer_margin = 24.dp
val inner_margin = 16.dp
Text(
stringResource(R.string.battery_id),
Modifier.constrainAs(labelBatteryId) {
end.linkTo(labelDeclaredCapacity.end)
baseline.linkTo(textBatteryId.baseline)
}
)
// TODO: Stop baseline from moving!!!!
OutlinedTextField(
value = batteryId,
onValueChange = { batteryId = it },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
label = { Text(stringResource(R.string.hint_enter_battery_id)) },
modifier = Modifier
.constrainAs(textBatteryId) {
top.linkTo(parent.top, margin = outer_margin)
start.linkTo(textDeclaredCapacity.start)
end.linkTo(parent.end, margin = outer_margin)
width = Dimension.fillToConstraints
}
.wrapContentHeight()
)
Text(
stringResource(R.string.declared_capacity),
Modifier
.constrainAs(labelDeclaredCapacity) {
baseline.linkTo(textDeclaredCapacity.baseline)
start.linkTo(parent.start, margin = outer_margin)
}
.wrapContentSize()
)
OutlinedTextField(
value = declaredCapacity,
onValueChange = { declaredCapacity = verifyDecimal(declaredCapacity, it) },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
label = { Text(stringResource(R.string.hint_enter_declared_capacity)) },
modifier = Modifier
.constrainAs(textDeclaredCapacity) {
top.linkTo(textBatteryId.bottom, margin = inner_margin)
start.linkTo(labelDeclaredCapacity.end, margin = inner_margin)
end.linkTo(parent.end, margin = outer_margin)
width = Dimension.fillToConstraints
}
)
}
}
}
// Inflate the layout for this fragment
return view
}
private fun verifyDecimal(originalValue: String, it: String): String {
val reg = Regex("^[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\$")
return if(it.matches(reg) || it.isEmpty()) it
else originalValue
}
/*companion object {
*//**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AddBatteryFragment.
*//*
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AddBatteryFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}*/
}

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.add_charge
package com.sockenklaus.batterytracker.ui.fragments.add_charge
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.add_charge
package com.sockenklaus.batterytracker.ui.fragments.add_charge
import androidx.lifecycle.ViewModel

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.home
package com.sockenklaus.batterytracker.ui.fragments.home
import android.os.Bundle
import android.view.LayoutInflater

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.home
package com.sockenklaus.batterytracker.ui.fragments.home
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.settings
package com.sockenklaus.batterytracker.ui.fragments.settings
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle

View File

@@ -1,4 +1,4 @@
package com.sockenklaus.batterytracker.ui.settings
package com.sockenklaus.batterytracker.ui.fragments.settings
import androidx.lifecycle.ViewModel

View File

@@ -1,10 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.compose.ui.platform.ComposeView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.fragments.AddBatteryFragment"
android:id="@+id/compose_add_battery"
/>
<!--<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.AddBatteryFragment">
tools:context=".ui.fragments.AddBatteryFragment">
<TextView
android:id="@+id/text_battery_id"
@@ -13,7 +26,7 @@
android:layout_marginStart="16dp"
android:text="@string/battery_id"
app:layout_constraintBaseline_toBaselineOf="@+id/battery_id"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintEnd_toEndOf="@+id/text_declared_capacity" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/battery_id"
@@ -48,19 +61,19 @@
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="16dp"
android:hint="@string/hint_enter_declared_capacity"
app:layout_constraintStart_toEndOf="@id/text_declared_capacity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/text_declared_capacity"
app:layout_constraintTop_toBottomOf="@id/battery_id">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
@@ -77,4 +90,4 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/declared_capacity" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>-->

View File

@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.add_charge.AddChargeFragment">
tools:context=".ui.fragments.add_charge.AddChargeFragment">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edit_battery_id"
@@ -44,17 +44,17 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edit_charge"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:hint="@string/hint_charge"
android:hint="@string/hint_charge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/edit_date"
app:layout_constraintTop_toBottomOf="@+id/edit_date"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
app:layout_constraintTop_toBottomOf="@+id/edit_date">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"

View File

@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
tools:context=".ui.fragments.home.HomeFragment">
<TextView
android:id="@+id/text_home"

View File

@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.settings.SettingsFragment">
tools:context=".ui.fragments.settings.SettingsFragment">
<TextView
android:layout_width="wrap_content"

View File

@@ -7,25 +7,25 @@
<fragment
android:id="@+id/nav_home"
android:name="com.sockenklaus.batterytracker.ui.home.HomeFragment"
android:name="com.sockenklaus.batterytracker.ui.fragments.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_add_charge"
android:name="com.sockenklaus.batterytracker.ui.add_charge.AddChargeFragment"
android:name="com.sockenklaus.batterytracker.ui.fragments.add_charge.AddChargeFragment"
android:label="@string/menu_add_charge"
tools:layout="@layout/fragment_add_charge" />
<fragment
android:id="@+id/nav_add_battery"
android:name="com.sockenklaus.batterytracker.ui.AddBatteryFragment"
android:name="com.sockenklaus.batterytracker.ui.fragments.AddBatteryFragment"
android:label="@string/menu_add_battery"
tools:layout="@layout/fragment_add_battery" />
<fragment
android:id="@+id/nav_settings"
android:name="com.sockenklaus.batterytracker.ui.settings.SettingsFragment"
android:name="com.sockenklaus.batterytracker.ui.fragments.settings.SettingsFragment"
android:label="@string/action_settings"
tools:layout="@layout/fragment_settings" />
</navigation>

View File

@@ -1,7 +1,5 @@
buildscript {
ext {
compose_version = '1.1.0-beta01'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false