Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Dev2 #6

Open
wants to merge 22 commits into
base: master
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
68 changes: 64 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'

id 'com.google.dagger.hilt.android'
id 'kotlin-kapt'

id 'org.jetbrains.kotlin.plugin.serialization' version "1.8.10"
}

kotlin {
jvmToolchain {
(System.getenv("JAVA_HOME") ?: "/path/to/jdk11") as File
}
}

android {
namespace 'ru.otus.basicarchitecture'
compileSdk 33
compileSdk 34

buildFeatures {
viewBinding true
}

defaultConfig {
applicationId "ru.otus.basicarchitecture"
minSdk 24
targetSdk 33
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -27,17 +42,62 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions.jvmTarget = '1.8'
}

kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
implementation "com.squareup.retrofit2:converter-gson:2.6.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.12.6"
implementation 'com.google.code.gson:gson:2.8.9'

implementation 'com.google.dagger:hilt-android:2.46.1'
kapt 'com.google.dagger:hilt-compiler:2.46.1'

implementation 'androidx.navigation:navigation-runtime-ktx:2.6.0'
def nav_version = "2.6.0"

// Kotlin
implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation("androidx.navigation:navigation-ui-ktx:$nav_version")

// Feature module Support
implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version")

// Testing Navigation
androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")

// Jetpack Compose Integration
implementation("androidx.navigation:navigation-compose:$nav_version")


implementation 'androidx.fragment:fragment-ktx:1.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'

implementation 'androidx.appcompat:appcompat:1.7.0-alpha03'
implementation 'com.google.android.material:material:1.11.0-alpha01'

implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
20 changes: 17 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -11,10 +13,22 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BasicArchitecture"
tools:targetApi="31">
tools:targetApi="33"
android:name="_appLication">

<activity
android:theme="@style/Primary.Fr1"
android:name=".MainActivity"
android:exported="false" />
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


</application>

</manifest>
</manifest>

34 changes: 34 additions & 0 deletions app/src/main/java/ru/otus/WizardCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.otus

import dagger.hilt.android.scopes.ActivityRetainedScoped
import java.time.LocalDate
import javax.inject.Inject


@ActivityRetainedScoped //долго живет(иначе не переживет поворот?)
class WizardCache @Inject constructor() {

//Fragment1
var name: String = ""
var surName: String = ""
var bd: LocalDate? = null

//*************************************

//Fragment2
var address: String = ""

//*************************************

//Fragment3
var tags: Map<String, Boolean> = mapOf(
"Kotlin" to false,
"Android" to false,
"Swift" to false,
"Ios" to false,
"C/C++" to false,
"Java" to false,
"Ruby" to false,

)
}
80 changes: 80 additions & 0 deletions app/src/main/java/ru/otus/basicarchitecture/Fragment1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ru.otus.basicarchitecture

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import dagger.hilt.android.AndroidEntryPoint
import ru.otus.basicarchitecture.databinding.Fragment1Binding

import ru.otus.basicarchitecture.viewModel.viewModelFR1


@AndroidEntryPoint
class Fragment1 : Fragment() {

private var binding: Fragment1Binding? = null

/**
* Удобная функция, чтобы не проверять каждый раз на нулл
*/
private inline fun withBinding(block: Fragment1Binding.() -> Unit) {
binding?.block()
}

private val dataModel: viewModelFR1 by viewModels() // Время жизни модели - фрагмент!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
binding = Fragment1Binding.inflate(layoutInflater, container, false)
return binding!!.root
}

override fun onDestroyView() {
// Смотри сюда: https://developer.android.com/topic/libraries/view-binding#fragments
super.onDestroyView()
binding = null
}

@SuppressLint("ResourceType")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

withBinding {
dataModel.viewState.observe(viewLifecycleOwner) { state ->
goToFR2.isEnabled = state.dataOK //должна блокировать или не блокировать кнопку
viewName.setTextKeepState(state.name.orEmpty()) // загрузка из viewModel элементов в view
viewSurName.setTextKeepState(state.surName.orEmpty()) //
viewData.setTextKeepState(state.data.orEmpty()) //
errorMessage.setTextKeepState(state.dataError.orEmpty())
}

goToFR2.setOnClickListener{
// ОЧЕНЬ не рекомендуется прокидывать контекст активити в модель
// Помним, что модель живет ДОЛЬШЕ активити.
findNavController().navigate(R.id.action_fragment1_to_fragment2)
}

viewName.doOnTextChanged { text, _, _, _ -> //какая то стандартная лямбда
// В модель записываем каждое изменение текста
dataModel.setName(text.toString())
}
viewSurName.doOnTextChanged { text, _, _, _ ->
dataModel.setSurname(text.toString())
}
viewData.doOnTextChanged { text, _, _, _ ->
dataModel.setDate(text.toString())
}
}
}



}
104 changes: 104 additions & 0 deletions app/src/main/java/ru/otus/basicarchitecture/Fragment2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ru.otus.basicarchitecture

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListAdapter
import android.widget.Toast
import androidx.compose.ui.graphics.Color
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import dagger.hilt.android.AndroidEntryPoint
import ru.otus.basicarchitecture.databinding.Fragment2Binding
import ru.otus.basicarchitecture.viewModel.viewModelFR2
import java.util.SplittableRandom

@AndroidEntryPoint
class Fragment2 : Fragment() {

private var binding: Fragment2Binding? = null

private inline fun withBinding(block: Fragment2Binding.() -> Unit) {
binding?.block()
}

private val dataModel: viewModelFR2 by viewModels()


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?, ): View {
binding = Fragment2Binding.inflate(layoutInflater, container, false)
return binding!!.root
}

override fun onDestroyView() {

super.onDestroyView()
binding = null
}


@SuppressLint("ResourceType")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)



withBinding {
// загрузка из viewModel элементов в view
dataModel.viewState.observe(viewLifecycleOwner) { state ->

val adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_spinner_dropdown_item, state.variantAddress)
LIST.threshold = 2
LIST.setAdapter(adapter)



// viewCountry.setTextKeepState(state.address.orEmpty())
// inrernetAdress.setTextKeepState(state.variantAddress.joinToString("\n"))
// viewCity.setTextKeepState(state.city.orEmpty())
// viewAdress.setTextKeepState(state.address.orEmpty())



LIST.setTextKeepState(state.address.orEmpty())
}




goToFR3.setOnClickListener{
// ОЧЕНЬ не рекомендуется прокидывать контекст активити в модель
// Помним, что модель живет ДОЛЬШЕ активити.
findNavController().navigate(R.id.action_fragment2_to_fragment3)
}

LIST.doOnTextChanged{text, _, _, _ ->
dataModel.setAddress(text.toString())
}

// viewCountry.doOnTextChanged { text, _, _, _ ->
// // В модель записываем каждое изменение текста
// dataModel.setAddress(text.toString())
// }
// viewCity.doOnTextChanged { text, _, _, _ ->
// dataModel.setCity(text.toString())
// }
// viewAdress.doOnTextChanged { text, _, _, _ ->
// dataModel.setAddress(text.toString())
// }
}

}


}
Loading