forked from nickbutcher/plaid
-
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.
Introduce a ViewModel to DribbbleShot and move logic.
Fixes nickbutcher#382
- Loading branch information
1 parent
9c386b2
commit e22be0c
Showing
18 changed files
with
552 additions
and
124 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.plaidapp.dribbble | ||
|
||
import android.content.Context | ||
import io.plaidapp.core.provideCoroutinesContextProvider | ||
import io.plaidapp.dribbble.domain.DribbbleImageUriProvider | ||
import io.plaidapp.dribbble.domain.GetShareShotInfoUseCase | ||
import io.plaidapp.dribbble.ui.DribbbleViewModelFactory | ||
|
||
/** | ||
* File providing different dependencies. | ||
* | ||
* Once we have a dependency injection framework or a service locator, this should be removed. | ||
*/ | ||
|
||
fun provideViewModelFactory(context: Context) = | ||
DribbbleViewModelFactory( | ||
provideCoroutinesContextProvider(), | ||
provideGetShareShotInfoUseCase(context) | ||
) | ||
|
||
fun provideGetShareShotInfoUseCase(context: Context) = | ||
GetShareShotInfoUseCase(provideDribbbleImageUriProvider(context)) | ||
|
||
fun provideDribbbleImageUriProvider(context: Context) = | ||
DribbbleImageUriProvider(context) |
50 changes: 50 additions & 0 deletions
50
dribbble/src/main/java/io/plaidapp/dribbble/domain/DribbbleImageUriProvider.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,50 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.plaidapp.dribbble.domain | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.support.annotation.WorkerThread | ||
import android.support.v4.content.FileProvider | ||
import com.bumptech.glide.Glide | ||
import io.plaidapp.core.dribbble.data.api.model.Images | ||
import io.plaidapp.dribbble.BuildConfig | ||
import java.io.File | ||
|
||
/** | ||
* A class responsible for resolving an image as identified by Url into a sharable [Uri]. | ||
*/ | ||
class DribbbleImageUriProvider(context: Context) { | ||
|
||
// Only hold the app context to avoid leaks | ||
private val appContext = context.applicationContext | ||
|
||
@WorkerThread | ||
operator fun invoke(url: String, size: Images.ImageSize): Uri { | ||
// Retrieve the image from Glide (hopefully cached) as a File | ||
val file = Glide.with(appContext) | ||
.asFile() | ||
.load(url) | ||
.submit(size.width, size.height) | ||
.get() | ||
// Glide cache uses an unfriendly & extension-less name, massage it based on the original | ||
val fileName = url.substring(url.lastIndexOf('/') + 1) | ||
val renamed = File(file.parent, fileName) | ||
file.renameTo(renamed) | ||
return FileProvider.getUriForFile(appContext, BuildConfig.FILES_AUTHORITY, renamed) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
dribbble/src/main/java/io/plaidapp/dribbble/domain/GetShareShotInfoUseCase.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,55 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.plaidapp.dribbble.domain | ||
|
||
import android.net.Uri | ||
import android.support.annotation.WorkerThread | ||
import io.plaidapp.core.dribbble.data.api.model.Shot | ||
|
||
/** | ||
* A UseCase which prepares the information required to share a shot. | ||
*/ | ||
class GetShareShotInfoUseCase(private val imageUriProvider: DribbbleImageUriProvider) { | ||
|
||
@WorkerThread | ||
operator fun invoke(shot: Shot): ShareShotInfo { | ||
val url = shot.images.best() ?: throw IllegalArgumentException() | ||
val uri = imageUriProvider(url, shot.images.bestSize()) | ||
val text = "“${shot.title}” by ${shot.user.name}\n${shot.url}" | ||
val mime = getImageMimeType(url) | ||
return ShareShotInfo(uri, shot.title, text, mime) | ||
} | ||
|
||
private fun getImageMimeType(fileName: String): String { | ||
if (fileName.endsWith(".png")) { | ||
return "image/png" | ||
} else if (fileName.endsWith(".gif")) { | ||
return "image/gif" | ||
} | ||
return "image/jpeg" | ||
} | ||
} | ||
|
||
/** | ||
* Models information about a shot needed to share it. | ||
*/ | ||
data class ShareShotInfo( | ||
val imageUri: Uri, | ||
val title: String, | ||
val shareText: CharSequence, | ||
val mimeType: String | ||
) |
43 changes: 43 additions & 0 deletions
43
dribbble/src/main/java/io/plaidapp/dribbble/ui/DribbbleViewModelFactory.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,43 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.plaidapp.dribbble.ui | ||
|
||
import android.arch.lifecycle.ViewModel | ||
import android.arch.lifecycle.ViewModelProvider | ||
import io.plaidapp.core.data.CoroutinesContextProvider | ||
import io.plaidapp.dribbble.domain.GetShareShotInfoUseCase | ||
import io.plaidapp.dribbble.ui.shot.DribbbleShotViewModel | ||
|
||
/** | ||
* Factory for Dribbble [ViewModel]s | ||
*/ | ||
class DribbbleViewModelFactory( | ||
private val contextProvider: CoroutinesContextProvider, | ||
private val getShareShotInfoUseCase: GetShareShotInfoUseCase | ||
) : ViewModelProvider.Factory { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(DribbbleShotViewModel::class.java)) { | ||
return DribbbleShotViewModel( | ||
contextProvider, | ||
getShareShotInfoUseCase | ||
) as T | ||
} | ||
throw IllegalArgumentException("Unknown ViewModel class") | ||
} | ||
} |
95 changes: 0 additions & 95 deletions
95
dribbble/src/main/java/io/plaidapp/dribbble/ui/ShareDribbbleImageTask.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.