Skip to content

Commit

Permalink
Help: displaying the help page now works
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Apr 4, 2020
1 parent 14f27d4 commit 31d80be
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 25 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ android {
dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
def room_version = "2.2.5"
def coroutine_version = "1.3.0"
def coroutine_version = "1.3.4"
def nav_version = "2.3.0-alpha04"
def work_version = "2.3.4"
def epoxy_version = "3.9.0"
def splitties_version = "3.0.0-alpha06"
def markwonVersion = "4.3.1"

implementation fileTree(include: ['*.jar'], dir: 'libs')

Expand All @@ -79,6 +80,8 @@ dependencies {
implementation 'com.heinrichreimersoftware:material-intro:2.0.0'
implementation "com.airbnb.android:epoxy:$epoxy_version"
implementation "com.airbnb.android:epoxy-databinding:$epoxy_version"
implementation "io.noties.markwon:core:$markwonVersion"
implementation "com.squareup.okhttp3:okhttp:4.4.0"

// splitties
implementation "com.louiscad.splitties:splitties-bitflags:$splitties_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.sds100.keymapper.data

import android.content.Context
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.util.FileUtils
import io.github.sds100.keymapper.util.NetworkUtils
import io.github.sds100.keymapper.util.result.*
import splitties.resources.appStr
import java.io.File

/**
* Created by sds100 on 04/04/2020.
*/
class FileRepository private constructor(private val mContext: Context) {
companion object {
@Volatile
private var instance: FileRepository? = null

fun getInstance(context: Context) =
instance ?: synchronized(this) {
instance ?: FileRepository(context.applicationContext).also { instance = it }
}

private const val FILE_NAME_HELP = "help.md"
}

suspend fun getHelpMarkdown(): Result<String> {
val path = FileUtils.getPathToFileInAppData(mContext, FILE_NAME_HELP)

return NetworkUtils.downloadFile(appStr(R.string.url_help), path)
.otherwise {
val file = File(path)

if (file.exists() && file.readText().isNotBlank()) {
Success(file)
} else {
FileNotExists()
}
}
.then { Success(it.readText()) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.sds100.keymapper.data.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import io.github.sds100.keymapper.data.FileRepository
import io.github.sds100.keymapper.ui.callback.ProgressCallback
import io.github.sds100.keymapper.util.result.DownloadFailed
import io.github.sds100.keymapper.util.result.Failure
import io.github.sds100.keymapper.util.result.Result
import kotlinx.coroutines.launch

/**
* Created by sds100 on 04/04/2020.
*/

class HelpViewModel(private val repository: FileRepository) : ViewModel(), ProgressCallback {

override val loadingContent = MutableLiveData(false)

val markdownText = MutableLiveData<Result<String>>()

init {
refreshIfFailed()
}

fun refreshIfFailed() {
if (markdownText.value == null || markdownText.value is Failure) {
viewModelScope.launch {
loadingContent.value = true

markdownText.value = repository.getHelpMarkdown()

loadingContent.value = false
}
}
}

class Factory(
private val mRepository: FileRepository
) : ViewModelProvider.Factory {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>) =
HelpViewModel(mRepository) as T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.sds100.keymapper.databinding.FragmentSettingsBinding
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.observe
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.data.viewmodel.HelpViewModel
import io.github.sds100.keymapper.databinding.FragmentHelpBinding
import io.github.sds100.keymapper.util.InjectorUtils
import io.github.sds100.keymapper.util.result.onFailure
import io.github.sds100.keymapper.util.result.onSuccess
import splitties.toast.toast

class HelpFragment : Fragment() {
class HelpFragment : BottomSheetDialogFragment() {

private val mViewModel by activityViewModels<HelpViewModel> {
InjectorUtils.provideHelpViewModel(requireContext())
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
FragmentSettingsBinding.inflate(inflater, container, false).apply {
FragmentHelpBinding.inflate(inflater, container, false).apply {

lifecycleOwner = viewLifecycleOwner
progressCallback = mViewModel

mViewModel.refreshIfFailed()

mViewModel.markdownText.observe(viewLifecycleOwner) { result ->
result.onSuccess {
markdown = it
}.onFailure {
findNavController().navigateUp()
toast(R.string.error_download_failed)
}
}

return this.root
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ import com.google.android.material.textfield.TextInputLayout
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.data.model.*
import io.github.sds100.keymapper.ui.callback.ErrorClickCallback
import io.noties.markwon.AbstractMarkwonPlugin
import io.noties.markwon.Markwon
import io.noties.markwon.core.MarkwonTheme
import splitties.resources.appDrawable
import splitties.resources.appStr
import splitties.resources.styledColor
import splitties.resources.styledColorSL


/**
* Created by sds100 on 25/01/2020.
*/

@BindingAdapter("app:markdown")
fun TextView.markdown(markdown: String?) {
markdown ?: return

Markwon.create(context).apply {
setMarkdown(this@markdown, markdown)
}
}

@BindingAdapter("app:tintType")
fun AppCompatImageView.tintType(tintType: TintType?) {
tintType ?: clearColorFilter()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/util/InjectorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.sds100.keymapper.util

import android.content.Context
import com.example.architecturetest.data.KeymapRepository
import io.github.sds100.keymapper.data.FileRepository
import io.github.sds100.keymapper.data.SystemRepository
import io.github.sds100.keymapper.data.db.AppDatabase
import io.github.sds100.keymapper.data.viewmodel.*
Expand All @@ -20,6 +21,10 @@ object InjectorUtils {
return SystemRepository.getInstance(context)
}

private fun getFileRepository(context: Context): FileRepository {
return FileRepository.getInstance(context)
}

fun provideAppListViewModel(context: Context): AppListViewModel.Factory {
val repository = getSystemRepository(context)
return AppListViewModel.Factory(repository)
Expand Down Expand Up @@ -59,6 +64,11 @@ object InjectorUtils {
return SystemActionListViewModel.Factory()
}

fun provideHelpViewModel(context: Context): HelpViewModel.Factory {
val repository = getFileRepository(context)
return HelpViewModel.Factory(repository)
}

fun provideConfigKeymapViewModel(
context: Context,
id: Long
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/util/NetworkUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.sds100.keymapper.util

import io.github.sds100.keymapper.util.result.DownloadFailed
import io.github.sds100.keymapper.util.result.Result
import io.github.sds100.keymapper.util.result.Success
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.IOException
import java.io.File

/**
* Created by sds100 on 04/04/2020.
*/
object NetworkUtils {

suspend fun downloadFile(url: String, filePath: String): Result<File> = withContext(Dispatchers.IO) {
val httpClient = OkHttpClient()
val request = Request.Builder().url(url).build()

return@withContext try {
val response = httpClient.newCall(request).execute()

if (response.isSuccessful && response.body != null) {
val text = response.body!!.string()
val file = File(filePath)
file.writeText(text)

Success(file)

} else {
DownloadFailed()
}
} catch (e: IOException) {
DownloadFailed()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ class GoogleAppNotFound : RecoverableFailure(appStr(R.string.error_google_app_no

class FrontFlashNotFound : Failure(appStr(R.string.error_front_flash_not_found))
class BackFlashNotFound : Failure(appStr(R.string.error_back_flash_not_found))
class ImeNotFound(id: String) : Failure(appStr(R.string.error_ime_not_found, id))
class ImeNotFound(id: String) : Failure(appStr(R.string.error_ime_not_found, id))
class DownloadFailed() : Failure(appStr(R.string.error_download_failed))
class FileNotExists() : Failure(appStr(R.string.error_file_not_exists))
24 changes: 24 additions & 0 deletions app/src/main/res/anim/slide_in_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2018 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@integer/slide"
android:fromXDelta="0%"
android:fromYDelta="-100%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
22 changes: 22 additions & 0 deletions app/src/main/res/anim/slide_out_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2018 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="-100%"
android:duration="@integer/slide"/>
</set>
Loading

0 comments on commit 31d80be

Please sign in to comment.