Skip to content

Instantly share code, notes, and snippets.

View juliuscanute's full-sized avatar
💭
I may be slow to respond.

juliuscanute

💭
I may be slow to respond.
View GitHub Profile
@juliuscanute
juliuscanute / Startup.kt
Created July 4, 2019 00:58
Dependency Injection
object Startup {
var dictionary: Module = module(override = true) {
single { ... }
}
init {
injectDependencies()
}
}
@juliuscanute
juliuscanute / DictionaryController.kt
Created July 4, 2019 01:08
Dictionary Controller
class DictionaryController : KoinComponent {
private val repository: RepositoryInterface by inject()
fun getNumberOfPagesInDictionary(): Any {
return try {
repository.getNumberOfPagesInDictionary()
} catch (e: PageRetrievalException) {
ErrorMesssage(message = e.errorMessage, type = ErrorType.PAGE_RETRIVAL_ERROR)
}
@juliuscanute
juliuscanute / DictionaryHandlerTest.kt
Last active July 4, 2019 01:23
Dictionary Handler Test
class DictionaryHandlerTest {
@Test
fun testGetTotalPagesInDictionary() {
val handler = DictionaryTotalPageHandler()
val gson = Gson()
loadKoinModules(module(override = true) {
single { MockRepository() as RepositoryInterface }
})
@juliuscanute
juliuscanute / Startup.kt
Created July 4, 2019 05:58
Azure Cosmos DB Connection
object Startup {
var dictionary: Module = module(override = true) {
single { api.data.repository.DictionaryRepository() as api.data.repository.RepositoryInterface }
single {
com.microsoft.azure.documentdb.DocumentClient(java.lang.System.getenv("HOST"),
System.getenv("MASTER_KEY"),
com.microsoft.azure.documentdb.ConnectionPolicy.GetDefault(),
com.microsoft.azure.documentdb.ConsistencyLevel.Session)
}
}
@juliuscanute
juliuscanute / DictionaryRepository.kt
Created July 4, 2019 06:05
Dictionary Repository
class DictionaryRepository : RepositoryInterface, KoinComponent {
companion object {
val RECORDS_PER_PAGE = 20
}
val client: DocumentClient by inject()
override fun getNumberOfPagesInDictionary(): Page {
var page: Page
try {
@juliuscanute
juliuscanute / DictionaryRepository.kt
Created July 4, 2019 06:57
Search For Word in Dictionary
val findSpec = SqlQuerySpec().apply {
queryText = "SELECT Dictionary.word AS word, Dictionary.meaning AS meaning FROM Dictionary WHERE Dictionary.partition = @tag AND contains(Dictionary.search, @query) OFFSET @offset LIMIT @records_per_page"
parameters = SqlParameterCollection().apply {
add(SqlParameter("@tag", query.first().toUpperCase().toString()))
add(SqlParameter("@query", query.toLowerCase()))
add(SqlParameter("@offset", offset))
add(SqlParameter("@records_per_page", RECORDS_PER_PAGE))
}
}
val countSpec = SqlQuerySpec().apply {
# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
@juliuscanute
juliuscanute / DictionaryModule.kt
Created July 9, 2019 06:51
Dictionary Module - DI
private const val BASE_URL = "https://dictionary-sample.azurewebsites.net/"
const val PAGE_SIZE = 20
val dictionaryModule = module {
single { createApiClient() }
single { DictionaryRepository(get()) as Repository }
single { PagedList.Config.Builder().setPageSize(PAGE_SIZE).setEnablePlaceholders(false).build() }
single { MutableLiveData<Event<NetworkState>>() }
factory { DictionaryAllWordsDataSourceFactory(get(), get()) }
factory { DictionarySearchWordsDataSourceFactory(get(), get()) }
@juliuscanute
juliuscanute / DictionaryApi.kt
Created July 10, 2019 00:14
Dictionary API
interface DictionaryApi {
@GET("/api/v1/dictionary/pages")
fun getNumberOfPagesInDictionary(): Call<Page>
@GET("/api/v1/dictionary/page/{pageNo}")
fun getWordsInDictionaryPage(@Path("pageNo") pageNo: Int): Call<Dictionary>
@GET("/api/v1/dictionary")
fun searchForWordsInDictionary(@Query("pageNo") pageNo: Int, @Query("word") word: String): Call<SearchResult>
}
@juliuscanute
juliuscanute / MeaningAdapter.kt
Last active July 10, 2019 00:33
Meaning Adapter
class MeaningAdapter : PagedListAdapter<Meaning, RecyclerView.ViewHolder>(REPO_COMPARATOR) {
//..
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fragment_meaning, parent, false)
return DictionaryViewHolder(view)
}