From 7758dab9fcf2a32b5ac8f673834c2225a089f6f6 Mon Sep 17 00:00:00 2001 From: Vladimir Nazarov Date: Sat, 7 May 2022 01:00:23 +0300 Subject: [PATCH 1/2] 18 Task TMS AN-14 --- .idea/.gitignore | 3 + app/src/main/res/layout/activity_main.xml | 304 +++++++++++++++++++++- 2 files changed, 298 insertions(+), 9 deletions(-) create mode 100644 .idea/.gitignore 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/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..ba30731 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,304 @@ - + android:layout_height="match_parent"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:layout_gravity="center"> + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file + \ No newline at end of file From fd41dbf54bb9a91c830beef176bf275db2f607a1 Mon Sep 17 00:00:00 2001 From: Vladimir Nazarov Date: Fri, 13 May 2022 23:23:03 +0300 Subject: [PATCH 2/2] 23 Task TMS AN-14 --- .idea/.name | 1 + .idea/compiler.xml | 6 + .idea/deploymentTargetDropDown.xml | 17 + .idea/gradle.xml | 20 ++ .idea/misc.xml | 12 + .idea/vcs.xml | 6 + app/build.gradle | 7 + app/src/main/AndroidManifest.xml | 2 + .../tmstemp/AnimationListenerEndView.kt | 20 ++ .../com/github/krottv/tmstemp/MainActivity.kt | 36 ++ .../java/com/github/krottv/tmstemp/Message.kt | 5 + .../github/krottv/tmstemp/MessageFragment.kt | 145 ++++++++ .../krottv/tmstemp/MessageListViewModel.kt | 30 ++ .../krottv/tmstemp/MessageRemoteDataSource.kt | 5 + .../tmstemp/MessageRemoteDataSourceFake.kt | 32 ++ app/src/main/res/anim/fade_out.xml | 9 + app/src/main/res/layout/activity_main.xml | 321 ++---------------- app/src/main/res/layout/list_item_message.xml | 75 ++++ .../main/res/layout/message_recycle_view.xml | 6 + app/src/main/res/values/strings.xml | 4 +- app/src/main/res/values/themes.xml | 4 +- settings.gradle | 1 + 22 files changed, 471 insertions(+), 293 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/deploymentTargetDropDown.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/github/krottv/tmstemp/AnimationListenerEndView.kt create mode 100644 app/src/main/java/com/github/krottv/tmstemp/Message.kt create mode 100644 app/src/main/java/com/github/krottv/tmstemp/MessageFragment.kt create mode 100644 app/src/main/java/com/github/krottv/tmstemp/MessageListViewModel.kt create mode 100644 app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSource.kt create mode 100644 app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSourceFake.kt create mode 100644 app/src/main/res/anim/fade_out.xml create mode 100644 app/src/main/res/layout/list_item_message.xml create mode 100644 app/src/main/res/layout/message_recycle_view.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..0f301f5 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +TmsTemp \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..659bf43 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..c067a6f --- /dev/null +++ b/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..5a9a809 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a751f1b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ 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/build.gradle b/app/build.gradle index 93d31ce..a7d2962 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,8 +36,15 @@ dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0' + + implementation 'com.github.ybq:Android-SpinKit:1.4.0' + implementation("io.coil-kt:coil:2.0.0-rc03") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a551aff..6d5c2b0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + () + + private val messageListViewModel: MessageListViewModel by lazy{ + ViewModelProviders.of(this).get(MessageListViewModel::class.java) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val view = inflater.inflate(R.layout.message_recycle_view, container, false) + + msgRecyclerView = view.findViewById(R.id.msg_recycler_view) as RecyclerView + msgRecyclerView.layoutManager = LinearLayoutManager(context) + + messageListViewModel.loadData() + updateMessages() + + return view + } + + private inner class MsgHolder(view: View): RecyclerView.ViewHolder(view), View.OnClickListener{ + + private lateinit var message: Message + private val mainText: TextView = itemView.findViewById(R.id.textView) + private val subText: TextView = itemView.findViewById(R.id.textView2) + private val img1: ImageView = itemView.findViewById(R.id.img1) + private val img2: ImageView = itemView.findViewById(R.id.img2) + + fun bind(message: Message) { + this.message = message + mainText.text = this.message.mainText + subText.text = this.message.subText + val link = this.message.imgLink + + img1.load(link){ + crossfade(true) + transformations(CircleCropTransformation()) + } + + img2.load(link){ + crossfade(true) + transformations(CircleCropTransformation()) + } + } + + init { + itemView.setOnClickListener(this) + } + + override fun onClick(view: View) { + Toast.makeText(context, mainText.text, Toast.LENGTH_SHORT).show() + } + } + + private inner class MessageAdapter(var messages: List): RecyclerView.Adapter(){ + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MsgHolder { + + val view = layoutInflater.inflate(R.layout.list_item_message, parent, false) + return MsgHolder(view) + } + + override fun getItemCount(): Int = messages.size + + override fun onBindViewHolder(holder: MsgHolder, position: Int) { + val message = messages[position] + holder.bind(message) + } + } + + companion object{ + fun newInstance(): MessageFragment { + return MessageFragment() + } + } + + private fun updateMessages(){ + lifecycleScope.launchWhenStarted { + messageListViewModel.state.collect{ + if (it != null){ + if (it.isSuccess){ + messages = it.getOrThrow() + adapter = MessageAdapter(messages) + msgRecyclerView.adapter = adapter + } + } + } + } + } + + fun deleteOne(){ + if (msgRecyclerView.isAnimating){ + msgRecyclerView.itemAnimator?.isRunning(RecyclerView.ItemAnimator.ItemAnimatorFinishedListener{}) + return + } else { + this.messages.removeAt(0) + adapter?.notifyItemRemoved(0) + } + } + + fun checkEmpty(): Boolean{ + return this.messages.size == 0 + } +} + diff --git a/app/src/main/java/com/github/krottv/tmstemp/MessageListViewModel.kt b/app/src/main/java/com/github/krottv/tmstemp/MessageListViewModel.kt new file mode 100644 index 0000000..127d9d1 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/MessageListViewModel.kt @@ -0,0 +1,30 @@ +package com.tms.android.ffthtask + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +class MessageListViewModel: ViewModel() { + + val messageRemoteDataSource: MessageRemoteDataSourceFake = MessageRemoteDataSourceFake() + + private val _state = MutableStateFlow>?>(null) + val state: StateFlow>?> = _state + + fun loadData(){ + viewModelScope.launch(Dispatchers.IO){ + + val result = try{ + Result.success(messageRemoteDataSource.getMessages()) + } catch (t: Throwable){ + Result.failure(t) + } + + _state.emit(result) + + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSource.kt b/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSource.kt new file mode 100644 index 0000000..1672373 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSource.kt @@ -0,0 +1,5 @@ +package com.tms.android.ffthtask + +interface MessageRemoteDataSource { + suspend fun getMessages(): List +} \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSourceFake.kt b/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSourceFake.kt new file mode 100644 index 0000000..85ea33c --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/MessageRemoteDataSourceFake.kt @@ -0,0 +1,32 @@ +package com.tms.android.ffthtask + +import kotlinx.coroutines.delay + +class MessageRemoteDataSourceFake: MessageRemoteDataSource { + + override suspend fun getMessages(): MutableList { + val messages = mutableListOf() + for(index in 1..1000){ + val msg = Message() + msg.mainText = "Some txt $index" + msg.subText = "Какой-то текст Какой-то текст Какой-то текст Какой-то текст" + when { + index % 3 == 0 -> { + msg.imgLink = "https://images.unsplash.com/photo-1575439047055-83e6174df3b9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=470&q=80" + } + index % 5 == 0 -> { + msg.imgLink = "https://images.unsplash.com/photo-1563452965085-2e77e5bf2607?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=470&q=80" + } + index % 7 == 0 -> { + msg.imgLink = "https://images.unsplash.com/photo-1568127861543-b0c0696c735f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=470&q=80" + } + else -> { + msg.imgLink = "https://images.unsplash.com/photo-1505062351414-586330b076f2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1080&q=80" + } + } + messages += msg + } + delay(2000L) + return messages + } +} \ No newline at end of file diff --git a/app/src/main/res/anim/fade_out.xml b/app/src/main/res/anim/fade_out.xml new file mode 100644 index 0000000..415b8ea --- /dev/null +++ b/app/src/main/res/anim/fade_out.xml @@ -0,0 +1,9 @@ + + + + + + \ 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 ba30731..4f0f0d6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,304 +1,45 @@ - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_height="560dp" > - - - - - - - - - - - - - - - - - + android:layout_gravity="center" + app:SpinKit_Color="#bfb6b6" /> - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + +