forked from keymapperorg/KeyMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Help: displaying the help page now works
- Loading branch information
Showing
14 changed files
with
303 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/io/github/sds100/keymapper/data/FileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/io/github/sds100/keymapper/data/viewmodel/HelpViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/java/io/github/sds100/keymapper/util/NetworkUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.