Skip to content

Instantly share code, notes, and snippets.

View benhaxe's full-sized avatar

Masebinu Benjamin benhaxe

  • Akure, Ondo state
View GitHub Profile
@benhaxe
benhaxe / GraphQL.dart
Created September 9, 2021 15:10
GraphQL tips
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:gp_flutter_core/config/service/gql_config.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
/// The service responsible for networking requests
class GpGQLClient {
@benhaxe
benhaxe / time_based_logout.dart
Last active May 20, 2020 13:05
Trigger an action by observing user interaction, in our case we want to auto log a user out if he stops interacting with the app within a duration using listener.
/// Define top level functions at any suitable location "I did in a separate dart file"
Timer sessionTimer;
/// intialize a timer to trigger a function after a period of inactiveness
void initializeTimer() {
sessionTimer =
Timer.periodic(const Duration(minutes: 5), (_) => UserUtil.logOut());
}
class TextPage extends StatelessWidget {
final String endpoint;
final String selectedSubject;
final String searchQuery;
TextPage(
{Key key,
@required this.endpoint,
@required this.selectedSubject,
@required this.searchQuery})
@benhaxe
benhaxe / AddStackAndSchildren.dart
Created December 5, 2018 14:40
MovieDetailsSteps
import 'package:flutter/material.dart';
import 'dart:ui' as UI;
class MovieDetail extends StatelessWidget{
final movie;
var image_url = 'https://image.tmdb.org/t/p/w500/';
MovieDetail(this.movie);
Color mainColor = const Color(0xff3C3261);
@override
@benhaxe
benhaxe / SnackBar.java
Created December 17, 2017 20:00
Best practice for displaying short information to user in replace of using the Toast
// Advised to set the root tag of your layout to the coordinate layout and give it a id
// So i will just comment the layout file code
/*
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cl_forSnackBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@benhaxe
benhaxe / Presence.java
Created November 23, 2017 13:06
Still on firebase real time database, the code helps track when a user is online or not and also the last time a user is seen.
// since I can connect from multiple devices, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myConnectionsRef = database.getReference("users/joe/connections");
// stores the timestamp of my last disconnect (the last time I was seen online)
final DatabaseReference lastOnlineRef = database.getReference("/users/joe/lastOnline");
final DatabaseReference connectedRef = database.getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@benhaxe
benhaxe / ConnectionState.java
Created November 23, 2017 12:23
Still on firebase database, this codes checks for the state of a user in the firebase real tile database state.
DatabaseReferene connectRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectRef.addValueEventListener(new ValueEventListener() {
@override
public void onDataCHanged(DataSnapshot dataSnapshot){
boolean isConnected = dataSnapshot.getValue(Boolean.class);
if(connected){
System.out.println("Your are now connected");
}else{
System.out.println("Your are disconnected");
}
@benhaxe
benhaxe / NewPost.java
Last active November 22, 2017 16:56
Still on firebase database, the code shows how to update 2 nodes in the database structure. Cloned from the firebase post app.
/*Please feel free to check the firbase real time databse documentation */
public class NewPost(String userId, String username, String title, String body){
// Get[key] used to store the post at the node ["posts"]
// [push] create the post at node["posts"]
String key = mDatabase.child("posts").push.getKey();
Post post = new Post(userId, username, title, body);
//[Map] is a key value object in java
// follow the link here: [https://goo.gl/e2XHLa] you should see the code where the method[toMap] is declared.
Map<String, Object> postValues = post.toMap();
@benhaxe
benhaxe / User.java
Last active November 21, 2017 16:48
A short code to add user to fire base database, with expanation
public class User(){
public String username;
public String email;
public void User(){
}
public void User(String username, String email){
this.username = username;
this.email = email;
}
@benhaxe
benhaxe / FirebaseAuthentication.java
Created November 20, 2017 13:50
How to authenticate users using fire base authentication
FirebaseAuth authUser = FirebaseAuth.getInstance();
authUser.signInWithEmailAndPassword(userEmail, userPassword).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Task task) {
if (task.isSuccessful()) {
FirebaseUser user = task.getResult().getUser();
String userEmail = user.getEmail();
}
}
});